0% found this document useful (0 votes)
9 views

Friend, Operator Overloading

1. A friend function has access to all private and protected members of the class for which it is declared as a friend. It does not need an object of that class to access members. 2. A friend class has access to all private and protected members of another class. However, it does not inherit from that class. 3. A const member function cannot modify the member variables of its class. Any attempts to modify results in a compiler error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Friend, Operator Overloading

1. A friend function has access to all private and protected members of the class for which it is declared as a friend. It does not need an object of that class to access members. 2. A friend class has access to all private and protected members of another class. However, it does not inherit from that class. 3. A const member function cannot modify the member variables of its class. Any attempts to modify results in a compiler error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

[Type the document title]

1. First, friends can be useful when you are


Friend Function overloading certain types of operators.
It is possible to grant a nonmember function access to 2. Two or more classes may contain members that
the private members of a class by using a friend. A are interrelated relative to other parts of your
friend function has access to all private and protected program.
members of the class for which it is a friend. To declare
a friend function, include its prototype within the class,
preceding it with the keyword friend. Friend Classes

#include <iostream> It is possible for one class to be a friend of another


class myclass class. When this is the case, the friend class and all of
{ its member functions have access to the private
int a, b;
members defined within the other class. For example
public:
friend int sum(myclass x);
void set_ab(int i, int j); // Using a friend class.
}; #include <iostream>
void myclass::set_ab(int i, int j) class TwoValues
{ {
a = i; int a;
b = j; int b;
} public:
// Note: sum() is not a member function of any class. TwoValues(int i, int j)
int sum(myclass x)
{
{
/* Because sum() is a friend of myclass, it can
a = i; b = j;
directly access a and b. */ }
return x.a + x.b; friend class Min;
} };
int main() class Min
{ {
myclass n; public:
n.set_ab(3, 4); int min(TwoValues x);
cout << sum(n); };
return 0;
int Min::min(TwoValues x)
}
{
Note: return x.a < x.b ? x.a : x.b;
the sum() function is not a member of myclass. }
However, it still has full access to its private members. int main()
Also, notice that sum() is called without the use of the {
dot operator. Because it is not a member function, it TwoValues ob(10, 20);
does not need to be (indeed, it may not be) qualified Min m;
with an object's name. cout << m.min(ob);
return 0;
Use of friend function in some circumstances }

In this example, class Min has access to the private


variables a and b declared within the TwoValues class.

It is critical to understand that when one class is a friend


of another, it only has access to names defined within
the other class. It does not inherit the other class.
[Type the document title]

const member function };


A const member function of a class is one ,which can int shared::a; // define a
void shared::show()
not change the member data of the class. The keyword
{
const is used.
cout << "This is static a: " << a;
Any change made is in data member of const member
cout << "\nThis is non-static b: " << b;
function rise to the compiler time error.
cout << "\n";
}
class emp
int main()
{
{
int empno;
shared x, y;
char name[100];
x.set(1, 1); // set a to 1
float salary;
x.show();
public:
y.set(2, 2); // change a to 2
void get();
y.show();
void display() const;
x.show(); /* Here, a has been changed for both x and y
};
because a is shared by both objects. */
void emp:: get()
return 0;
{
}
cin>>empno>>name>>salary;
} This is static a: 1
void emp ::display() const This is non-static b: 1
{ This is static a: 2
cout<<empno<<name<<salary; This is non-static b: 2
This is static a: 2
} This is non-static b: 1
void main()
{
emp e; Static Member Functions
e.get(); A static member function can be called even if no
e.display() objects of the class exist and the static functions are
} accessed using only the class name and the scope
resolution operator ::.
A static member function can only access static data
Static Class Members member, other static member functions and any other
When you precede a member variable's declaration with functions from outside the class.
static, you are telling the compiler that only one copy of
that variable #include <iostream>
Thus, all objects of that class use that same variable. All
static variables are initialized to zero before the first using namespace std;
object is created.
class Box {
When you declare a static data member within a class, public:
you are not defining it. Instead, you must provide a static int objectCount;
global definition for it elsewhere, outside the class.
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
#include <iostream> cout <<"Constructor called." << endl;
class shared length = l;
{ breadth = b;
static int a; height = h;
int b;
public: // Increase every time object is created
void set(int i, int j) {a=i; b=j;} objectCount++;
void show(); }
[Type the document title]

double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}

private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

// Initialize static member of class Box


int Box::objectCount = 0;

int main(void) {
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() <<
endl;

Box Box1(3.3, 1.2, 1.5); // Declare box1


Box Box2(8.5, 6.0, 2.0); // Declare box2

// Print total number of objects after creating object.


cout << "Final Stage Count: " << Box::getCount() <<
endl;

return 0;
}
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

You might also like