You are on page 1of 13

Friend Functions, Constructors and Destructors

Friend Functions
C++ allows a functions to be friendly, allowing the function to have access to the private data of classes These functions might not be member of any of those classes
class myClass { .. public: .. friend void function_name(); }

//friend function declaration

Example
class sample { int a; int b; public: void setvalue(){a=10;b=20;} friend float mean(sample s); }; float mean(sample s) { return float(s.a+s.b)/2.0; } int main() { sample x; x.setvalue(); cout<<Mean value = <<mean(x)<<\n; return 0; }

Friend Functions contd..


Member functions of one class can be friend functions of another class
class X { .. int fun1(): .. }; class Y { friend int X::fun1(); . }; //fun1() of X is friend of Y

All the member functions of a class can be declared as friend functions of another class ---> friend class
class Z { .. friend class X; //all member functions of X are friends to Z .. };

Example
class ABC; //forward declaration int main() class XYZ { { ABC abc; int x; int a,b; public: cout<<Enter value for class ABC: ; void setvalue(int i){x=i;} cin>>a; friend void max(XYZ,ABC); abc.setvalue(a); }; XYZ xyz; class ABC cout<<\nEnter value for class XYZ: ; { cin>>b; int a; xyz.setvalue(b); public: max(xyz,abc); void setvalue(int i){a=I;} return 0; friend void max(XYZ,ABC); } }; void max(XYZ m, ABC n) //Definition of friend { if(m.x>=n.a) cout<< \nXYZ has max value = <<m.x; else cout<<\nABC has max value = <<n.a; }

Friend function can be called by reference as well. Try it yourself.

Constructors

Think about this code .


class student { int roll_number; float marks; public: void insertdata(int a,float b); void displaydata(void); }; void student::insertdata(int a, float b) { roll_number = a; marks = b; } void student::displaydata(void); { cout<<"Roll Number: "<<roll_number<<"\n"; cout<<"Marks: "<<marks<<"\n\n"; }

int main() { int student_roll; float student_marks; student x; cout<<"Enter Roll Number:\n"; cin>>student_roll; cout<<"Enter Marks:\n"; cin>>student_marks; cout<<Student info:\n"; x.insertdata(student_roll, student_marks); x.displaydata(); return 0; }

How are initial values assigned in the given code ? Function call statements are used with the appropriate objects that have already been created These can t be used to initialize the member variables at the time of creation of their objects

By invoking a member function insertdata()

The essence of C++ is that we should be able to initialize a class type variable (object) when it is declared, like initialization of an ordinary variable int m = 20; Also, when a variable of built-in type goes out of scope, the compiler automatically destroys the variable. Think about the objects we have created so far. Does this happen in these objects ? C++ provides a special member function called the constructor which enables an object to initialize itself when it is created, known as automatic initialization of objects C++ also provides another member function called destructor which destroys the objects when they are no longer required

Constructors
A constructor is a special member function whose task is to initialize the objects of its class Constructor has the same name as that of class
class ratio { public: ratio(int n,int d) { num = n; den = d; } void print() { cout << num << '/' << den; } private: int num,den; }; int main() { ratio x(-1,3), y(22,7); cout << "x = "; x.print(); cout << " and y = "; y.print(); }

Characteristics of Constructors
Should be declared in the public section Invoked automatically when objects are created No return types, not even void Can t be inherited, though a derived class can call the base class constructor Can have default arguments Can t be virtual

You might also like