Today’s LectureToday’s Lecture
 ConstructorConstructor
 DestructorsDestructors
 Constructor OverloadingConstructor Overloading
 Friend functionFriend function
 InheritanceInheritance
 Single InheritanceSingle Inheritance
 Multiple Inheritances.Multiple Inheritances.
Abbas AjmalAbbas Ajmal
0314995850003149958500
ConstructorsConstructors
 A constructor is a member function of a class that is called andA constructor is a member function of a class that is called and
executed automatically when an object of that class is created.executed automatically when an object of that class is created.
 The name of the constructor function is the same as the nameThe name of the constructor function is the same as the name
of the class itself.of the class itself.
 A constructor function may have arguments but it cannot returnA constructor function may have arguments but it cannot return
any value.any value.
Example: ConstructorsExample: Constructors
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class test {class test {
public:public:
test(){test(){
cout<<"Welcome"<<endl;cout<<"Welcome"<<endl;
}}
};};
main(){main(){
test a,b,c;test a,b,c;
}}
Example ExplanationExample Explanation
 In the above program, the class "test" contains memberIn the above program, the class "test" contains member
function "test".function "test".
 This member function is the constructor functionThis member function is the constructor function
because name of this function and the name of thebecause name of this function and the name of the
class are same.class are same.
 When the member function "test" is executed, it printsWhen the member function "test" is executed, it prints
"Welcome" on the computer in the program."Welcome" on the computer in the program.
Example Explanation (cont..)Example Explanation (cont..)
 In the above program three objects (a, b c) of the classIn the above program three objects (a, b c) of the class
"test" are created."test" are created.
 Each time an object of the class "test" is created, theEach time an object of the class "test" is created, the
constructor is executed and the word "Welcome" isconstructor is executed and the word "Welcome" is
printed on the computer screen.printed on the computer screen.
 Since three objects are created, the word Welcome isSince three objects are created, the word Welcome is
printed three times.printed three times.
Initializing Data Using ConstructorsInitializing Data Using Constructors
 The constructor functions are normally used to initializeThe constructor functions are normally used to initialize
values in data members of a class when the program isvalues in data members of a class when the program is
executed.executed.
 This type of initialization is called the automaticThis type of initialization is called the automatic
initialization.initialization.
Example :Example : Initializing Data Using ConstructorsInitializing Data Using Constructors
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class sumclass sum
{{
private:private:
int n, m, s;int n, m, s;
public:public:
sum(int x, int y){sum(int x, int y){
n=x;m=y;s=n+m;n=x;m=y;s=n+m;
}void psum(){}void psum(){
cout<<"Sum of "<<n<<“cout<<"Sum of "<<n<<“
and "<<m<<" isand "<<m<<" is
"<<s<<endl;"<<s<<endl;
}};}};
main ( )main ( )
{{
sum a(16,10), b(2,3);sum a(16,10), b(2,3);
a. psum () ;a. psum () ;
b.psum() ; }b.psum() ; }
 In the above program when the object “a” of the class “sum” isIn the above program when the object “a” of the class “sum” is
created, the control shifts to the constructor function “sum” .created, the control shifts to the constructor function “sum” .
 The constructor function “sum” assigns values to variables nThe constructor function “sum” assigns values to variables n
and m and its also calculates their sum.and m and its also calculates their sum.
 Thus when “psum” function is executed by the object “a”, theThus when “psum” function is executed by the object “a”, the
values assigned to the data members of the object by thevalues assigned to the data members of the object by the
constructor are printed.constructor are printed.
 Similarly when the object b is executed, the constructorSimilarly when the object b is executed, the constructor
function is automatically called.function is automatically called.
Example ExplanationExample Explanation
 More than one constructor functions can be defined in oneMore than one constructor functions can be defined in one
class. When more than one constructor functions are defined,class. When more than one constructor functions are defined,
each constructor defined with a different set of parameters.each constructor defined with a different set of parameters.
 Defining more than one constructor with different set ofDefining more than one constructor with different set of
parameters is called constructor overloading.parameters is called constructor overloading.
 Constructor overloading is used to initialize different values toConstructor overloading is used to initialize different values to
class objects.class objects.
Constructor OverloadingConstructor Overloading
 When a program that uses the constructor overloading isWhen a program that uses the constructor overloading is
compiled, C++ compiler checks the number of parameters,compiled, C++ compiler checks the number of parameters,
their order and data types and marks them differently.their order and data types and marks them differently.
 When an object of the class is created, the correspondingWhen an object of the class is created, the corresponding
constructor that matches the number of parameters of theconstructor that matches the number of parameters of the
object function is executed.object function is executed.
 In the coming example two constructor functions are definedIn the coming example two constructor functions are defined
in class "sum" as shown on the next slide:in class "sum" as shown on the next slide:
Constructor Overloading (cont..)Constructor Overloading (cont..)
Example: Constructor OverloadingExample: Constructor Overloading
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class sum {class sum {
public:public:
sum(int l, int m, int n){sum(int l, int m, int n){
cout<<"sum of three values iscout<<"sum of three values is
"<<(l+m+n)<<endl;}"<<(l+m+n)<<endl;}
sum (int l, int m){sum (int l, int m){
cout<<"sum of two values iscout<<"sum of two values is
"<<(l+m)<<endl;"<<(l+m)<<endl;
}};}};
main(){main(){
sum (2,3), sum (1,2,3);sum (2,3), sum (1,2,3);
}}
 When the program is executed, the object x is created first andWhen the program is executed, the object x is created first and
then the sum constructor function that has only two integerthen the sum constructor function that has only two integer
type parameters is executed.type parameters is executed.
 Then the y object is created. It has three parameters of integerThen the y object is created. It has three parameters of integer
type.type.
 So the constructor function that has three arguments of integerSo the constructor function that has three arguments of integer
type is executed.type is executed.
Example ExplanationExample Explanation
Write a program to define two constructors to find outWrite a program to define two constructors to find out
the maximum values.the maximum values.
Due Date: 6/4/2015Due Date: 6/4/2015
AssignmentAssignment
 When an object is destroyed a special member function of thatWhen an object is destroyed a special member function of that
class is executed automatically.class is executed automatically.
 This member function is known as the destructor function orThis member function is known as the destructor function or
destructor.destructor.
 The destructor function has the same name as the name or aThe destructor function has the same name as the name or a
class but a tilde sign (~) is written before its name.class but a tilde sign (~) is written before its name.
 It is executed automatically when an object comes to the endIt is executed automatically when an object comes to the end
of its life.of its life.
 Like constructors, destructors do not return any value. TheyLike constructors, destructors do not return any value. They
also do not take any arguments.also do not take any arguments.
DestructorsDestructors
 For example, a local object is destroyed when all theFor example, a local object is destroyed when all the
statements of the function in which it is declared are executed.statements of the function in which it is declared are executed.
 So at the end of the function, the destructor function isSo at the end of the function, the destructor function is
executed.executed.
 Similarly, global objects (objects that are declared beforeSimilarly, global objects (objects that are declared before
main function) or static objects are destroyed at the end ofmain function) or static objects are destroyed at the end of
main function.main function.
 The lifetime of these objects end when the program executionThe lifetime of these objects end when the program execution
ends. So at the end of program the destructor function isends. So at the end of program the destructor function is
executed.executed.
DestructorsDestructors (cont..)(cont..)
 The destructor functions are commonly used to free theThe destructor functions are commonly used to free the
memory that was allocated for objects.memory that was allocated for objects.
 The example on the next slide explains the concept ofThe example on the next slide explains the concept of
constructors and destructor.constructors and destructor.
DestructorsDestructors (cont..)(cont..)
Example: Constructor and DestructorExample: Constructor and Destructor
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class prgclass prg
{{
public:public:
prg () {prg () {
cout<<"This is constructorcout<<"This is constructor
function"<<endl;}function"<<endl;}
~prg (){~prg (){
cout <<"This is destructorcout <<"This is destructor
function"<<endl; } };function"<<endl; } };
int main()int main()
{{
prg x;prg x;
int a, b;int a, b;
a = 10; b = 20;a = 10; b = 20;
cout<<"Sum of two numberscout<<"Sum of two numbers
is"<<(a+b)<<endl;is"<<(a+b)<<endl;
}}
Object can be passed as arguments to member functions. WhenObject can be passed as arguments to member functions. When
an object is passed as an argument to a member function:an object is passed as an argument to a member function:
 Only the name of the object is written in the argument.Only the name of the object is written in the argument.
 The number of parameters and their types must be defined inThe number of parameters and their types must be defined in
the member function to which the object to be passed. Thethe member function to which the object to be passed. The
objects that are passed are treated local for the memberobjects that are passed are treated local for the member
function and are destroyed when the control returns to thefunction and are destroyed when the control returns to the
calling function.calling function.
Passing Objects as an argument to a functionPassing Objects as an argument to a function
Example: Passing Objects as an argument to a functionExample: Passing Objects as an argument to a function
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class test {class test {
private:private:
char name [15];char name [15];
public:public:
void get(void){void get(void){
cout<< "enter your name? ";cout<< "enter your name? ";
cin >>name;};cin >>name;};
void print (test s)void print (test s)
{{
cout <<"Namecout <<"Name
is:"<<s.name<<endl;}};is:"<<s.name<<endl;}};
main()main()
{{
test temp, abc;test temp, abc;
temp.get();temp.get();
abc.print(temp);}abc.print(temp);}
 In the above program, the class "test" has one data memberIn the above program, the class "test" has one data member
"name" of string type and two member functions "get" and"name" of string type and two member functions "get" and
"print". The "print" function has an argument of class "test""print". The "print" function has an argument of class "test"
type.type.
 The objects "temp" and "abc" are declared of class "test". TheThe objects "temp" and "abc" are declared of class "test". The
member function gets the name in object "temp" and store itmember function gets the name in object "temp" and store it
into the data member "name".into the data member "name".
 The member function "print" for object "abc” is called byThe member function "print" for object "abc” is called by
passing argument of object "temp". When the control shifts topassing argument of object "temp". When the control shifts to
the member function "print", a copy of "temp" is created as athe member function "print", a copy of "temp" is created as a
local object in the print function with name "s".local object in the print function with name "s".
Example ExplanationExample Explanation
 A friend function can access all members of a class in which it is declared.A friend function can access all members of a class in which it is declared.
 It can also access private and protected members of all other classes evenIt can also access private and protected members of all other classes even
though it is not a member function of those classes.though it is not a member function of those classes.
 It is a non-member function. It is normally used when a function has toIt is a non-member function. It is normally used when a function has to
access private data of other classes.access private data of other classes.
 A friend function is defined in a class.A friend function is defined in a class.
 It is possible to declare the friend function as either private or public.It is possible to declare the friend function as either private or public.
 The function can be invoked without the use of an object. The friendThe function can be invoked without the use of an object. The friend
function has its argument as objectsfunction has its argument as objects
Friend FunctionFriend Function
 It is defined by using the keyword “friend” before the name ofIt is defined by using the keyword “friend” before the name of
the function in the function declarator.the function in the function declarator.
 It can be defined anywhere in the class.It can be defined anywhere in the class.
 When a friend function is defined outside the class, it isWhen a friend function is defined outside the class, it is
defined without the scope resolution parameters (::).defined without the scope resolution parameters (::).
 A program example is given on next slide that uses the friendA program example is given on next slide that uses the friend
function.function.
Friend FunctionFriend Function (cont..)(cont..)
Example: Friend FunctionExample: Friend Function
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class exforsysclass exforsys
{{
private:private:
int a,b;int a,b;
public:public:
void test()void test()
{{
a=100;a=100;
b=200;b=200;
}}
friend int compute(exforsys e1);friend int compute(exforsys e1);
//Friend Function Declaration with//Friend Function Declaration with
keyword friend and with the object of classkeyword friend and with the object of class
exforsys to which it is friend passed to itexforsys to which it is friend passed to it
};};
int compute(exforsys e1)int compute(exforsys e1)
{{
//Friend Function Definition which has//Friend Function Definition which has
access to private dataaccess to private data
return int(e1.a+e1.b)-5;return int(e1.a+e1.b)-5;
}}
main()main()
{{
exforsys e;exforsys e;
e.test();e.test();
cout << "The result is:" << compute(e);cout << "The result is:" << compute(e);
//Calling of Friend Function with//Calling of Friend Function with
object as argument.object as argument.
}}
 The function compute() is a non-member function of the class exforsys. InThe function compute() is a non-member function of the class exforsys. In
order to make this function have access to the private data a and b of classorder to make this function have access to the private data a and b of class
exforsys , it is created as a friend function for the class exforsys. As a firstexforsys , it is created as a friend function for the class exforsys. As a first
step, the function compute() is declared as friend in the class exforsys as:step, the function compute() is declared as friend in the class exforsys as:
friend int compute (exforsys e1)friend int compute (exforsys e1)
The keyword friend is placed before the function. The function definition isThe keyword friend is placed before the function. The function definition is
written as a normal function and thus, the function has access to the privatewritten as a normal function and thus, the function has access to the private
data a and b of the class exforsys. It is declared as friend inside the class, thedata a and b of the class exforsys. It is declared as friend inside the class, the
private data values a and b are added, 5 is subtracted from the result, givingprivate data values a and b are added, 5 is subtracted from the result, giving
295 as the result. This is returned by the function and thus the output is 295.295 as the result. This is returned by the function and thus the output is 295.
Example ExplanationExample Explanation

Oop Constructor Destructors Constructor Overloading lecture 2

  • 1.
    Today’s LectureToday’s Lecture ConstructorConstructor  DestructorsDestructors  Constructor OverloadingConstructor Overloading  Friend functionFriend function  InheritanceInheritance  Single InheritanceSingle Inheritance  Multiple Inheritances.Multiple Inheritances. Abbas AjmalAbbas Ajmal 0314995850003149958500
  • 2.
    ConstructorsConstructors  A constructoris a member function of a class that is called andA constructor is a member function of a class that is called and executed automatically when an object of that class is created.executed automatically when an object of that class is created.  The name of the constructor function is the same as the nameThe name of the constructor function is the same as the name of the class itself.of the class itself.  A constructor function may have arguments but it cannot returnA constructor function may have arguments but it cannot return any value.any value.
  • 3.
    Example: ConstructorsExample: Constructors #include<iostream>#include<iostream> usingnamespace std;using namespace std; class test {class test { public:public: test(){test(){ cout<<"Welcome"<<endl;cout<<"Welcome"<<endl; }} };}; main(){main(){ test a,b,c;test a,b,c; }}
  • 4.
    Example ExplanationExample Explanation In the above program, the class "test" contains memberIn the above program, the class "test" contains member function "test".function "test".  This member function is the constructor functionThis member function is the constructor function because name of this function and the name of thebecause name of this function and the name of the class are same.class are same.  When the member function "test" is executed, it printsWhen the member function "test" is executed, it prints "Welcome" on the computer in the program."Welcome" on the computer in the program.
  • 5.
    Example Explanation (cont..)ExampleExplanation (cont..)  In the above program three objects (a, b c) of the classIn the above program three objects (a, b c) of the class "test" are created."test" are created.  Each time an object of the class "test" is created, theEach time an object of the class "test" is created, the constructor is executed and the word "Welcome" isconstructor is executed and the word "Welcome" is printed on the computer screen.printed on the computer screen.  Since three objects are created, the word Welcome isSince three objects are created, the word Welcome is printed three times.printed three times.
  • 6.
    Initializing Data UsingConstructorsInitializing Data Using Constructors  The constructor functions are normally used to initializeThe constructor functions are normally used to initialize values in data members of a class when the program isvalues in data members of a class when the program is executed.executed.  This type of initialization is called the automaticThis type of initialization is called the automatic initialization.initialization.
  • 7.
    Example :Example :Initializing Data Using ConstructorsInitializing Data Using Constructors #include<iostream>#include<iostream> using namespace std;using namespace std; class sumclass sum {{ private:private: int n, m, s;int n, m, s; public:public: sum(int x, int y){sum(int x, int y){ n=x;m=y;s=n+m;n=x;m=y;s=n+m; }void psum(){}void psum(){ cout<<"Sum of "<<n<<“cout<<"Sum of "<<n<<“ and "<<m<<" isand "<<m<<" is "<<s<<endl;"<<s<<endl; }};}}; main ( )main ( ) {{ sum a(16,10), b(2,3);sum a(16,10), b(2,3); a. psum () ;a. psum () ; b.psum() ; }b.psum() ; }
  • 8.
     In theabove program when the object “a” of the class “sum” isIn the above program when the object “a” of the class “sum” is created, the control shifts to the constructor function “sum” .created, the control shifts to the constructor function “sum” .  The constructor function “sum” assigns values to variables nThe constructor function “sum” assigns values to variables n and m and its also calculates their sum.and m and its also calculates their sum.  Thus when “psum” function is executed by the object “a”, theThus when “psum” function is executed by the object “a”, the values assigned to the data members of the object by thevalues assigned to the data members of the object by the constructor are printed.constructor are printed.  Similarly when the object b is executed, the constructorSimilarly when the object b is executed, the constructor function is automatically called.function is automatically called. Example ExplanationExample Explanation
  • 9.
     More thanone constructor functions can be defined in oneMore than one constructor functions can be defined in one class. When more than one constructor functions are defined,class. When more than one constructor functions are defined, each constructor defined with a different set of parameters.each constructor defined with a different set of parameters.  Defining more than one constructor with different set ofDefining more than one constructor with different set of parameters is called constructor overloading.parameters is called constructor overloading.  Constructor overloading is used to initialize different values toConstructor overloading is used to initialize different values to class objects.class objects. Constructor OverloadingConstructor Overloading
  • 10.
     When aprogram that uses the constructor overloading isWhen a program that uses the constructor overloading is compiled, C++ compiler checks the number of parameters,compiled, C++ compiler checks the number of parameters, their order and data types and marks them differently.their order and data types and marks them differently.  When an object of the class is created, the correspondingWhen an object of the class is created, the corresponding constructor that matches the number of parameters of theconstructor that matches the number of parameters of the object function is executed.object function is executed.  In the coming example two constructor functions are definedIn the coming example two constructor functions are defined in class "sum" as shown on the next slide:in class "sum" as shown on the next slide: Constructor Overloading (cont..)Constructor Overloading (cont..)
  • 11.
    Example: Constructor OverloadingExample:Constructor Overloading #include<iostream>#include<iostream> using namespace std;using namespace std; class sum {class sum { public:public: sum(int l, int m, int n){sum(int l, int m, int n){ cout<<"sum of three values iscout<<"sum of three values is "<<(l+m+n)<<endl;}"<<(l+m+n)<<endl;} sum (int l, int m){sum (int l, int m){ cout<<"sum of two values iscout<<"sum of two values is "<<(l+m)<<endl;"<<(l+m)<<endl; }};}}; main(){main(){ sum (2,3), sum (1,2,3);sum (2,3), sum (1,2,3); }}
  • 12.
     When theprogram is executed, the object x is created first andWhen the program is executed, the object x is created first and then the sum constructor function that has only two integerthen the sum constructor function that has only two integer type parameters is executed.type parameters is executed.  Then the y object is created. It has three parameters of integerThen the y object is created. It has three parameters of integer type.type.  So the constructor function that has three arguments of integerSo the constructor function that has three arguments of integer type is executed.type is executed. Example ExplanationExample Explanation
  • 13.
    Write a programto define two constructors to find outWrite a program to define two constructors to find out the maximum values.the maximum values. Due Date: 6/4/2015Due Date: 6/4/2015 AssignmentAssignment
  • 14.
     When anobject is destroyed a special member function of thatWhen an object is destroyed a special member function of that class is executed automatically.class is executed automatically.  This member function is known as the destructor function orThis member function is known as the destructor function or destructor.destructor.  The destructor function has the same name as the name or aThe destructor function has the same name as the name or a class but a tilde sign (~) is written before its name.class but a tilde sign (~) is written before its name.  It is executed automatically when an object comes to the endIt is executed automatically when an object comes to the end of its life.of its life.  Like constructors, destructors do not return any value. TheyLike constructors, destructors do not return any value. They also do not take any arguments.also do not take any arguments. DestructorsDestructors
  • 15.
     For example,a local object is destroyed when all theFor example, a local object is destroyed when all the statements of the function in which it is declared are executed.statements of the function in which it is declared are executed.  So at the end of the function, the destructor function isSo at the end of the function, the destructor function is executed.executed.  Similarly, global objects (objects that are declared beforeSimilarly, global objects (objects that are declared before main function) or static objects are destroyed at the end ofmain function) or static objects are destroyed at the end of main function.main function.  The lifetime of these objects end when the program executionThe lifetime of these objects end when the program execution ends. So at the end of program the destructor function isends. So at the end of program the destructor function is executed.executed. DestructorsDestructors (cont..)(cont..)
  • 16.
     The destructorfunctions are commonly used to free theThe destructor functions are commonly used to free the memory that was allocated for objects.memory that was allocated for objects.  The example on the next slide explains the concept ofThe example on the next slide explains the concept of constructors and destructor.constructors and destructor. DestructorsDestructors (cont..)(cont..)
  • 17.
    Example: Constructor andDestructorExample: Constructor and Destructor #include<iostream>#include<iostream> using namespace std;using namespace std; class prgclass prg {{ public:public: prg () {prg () { cout<<"This is constructorcout<<"This is constructor function"<<endl;}function"<<endl;} ~prg (){~prg (){ cout <<"This is destructorcout <<"This is destructor function"<<endl; } };function"<<endl; } }; int main()int main() {{ prg x;prg x; int a, b;int a, b; a = 10; b = 20;a = 10; b = 20; cout<<"Sum of two numberscout<<"Sum of two numbers is"<<(a+b)<<endl;is"<<(a+b)<<endl; }}
  • 18.
    Object can bepassed as arguments to member functions. WhenObject can be passed as arguments to member functions. When an object is passed as an argument to a member function:an object is passed as an argument to a member function:  Only the name of the object is written in the argument.Only the name of the object is written in the argument.  The number of parameters and their types must be defined inThe number of parameters and their types must be defined in the member function to which the object to be passed. Thethe member function to which the object to be passed. The objects that are passed are treated local for the memberobjects that are passed are treated local for the member function and are destroyed when the control returns to thefunction and are destroyed when the control returns to the calling function.calling function. Passing Objects as an argument to a functionPassing Objects as an argument to a function
  • 19.
    Example: Passing Objectsas an argument to a functionExample: Passing Objects as an argument to a function #include<iostream>#include<iostream> using namespace std;using namespace std; class test {class test { private:private: char name [15];char name [15]; public:public: void get(void){void get(void){ cout<< "enter your name? ";cout<< "enter your name? "; cin >>name;};cin >>name;}; void print (test s)void print (test s) {{ cout <<"Namecout <<"Name is:"<<s.name<<endl;}};is:"<<s.name<<endl;}}; main()main() {{ test temp, abc;test temp, abc; temp.get();temp.get(); abc.print(temp);}abc.print(temp);}
  • 20.
     In theabove program, the class "test" has one data memberIn the above program, the class "test" has one data member "name" of string type and two member functions "get" and"name" of string type and two member functions "get" and "print". The "print" function has an argument of class "test""print". The "print" function has an argument of class "test" type.type.  The objects "temp" and "abc" are declared of class "test". TheThe objects "temp" and "abc" are declared of class "test". The member function gets the name in object "temp" and store itmember function gets the name in object "temp" and store it into the data member "name".into the data member "name".  The member function "print" for object "abc” is called byThe member function "print" for object "abc” is called by passing argument of object "temp". When the control shifts topassing argument of object "temp". When the control shifts to the member function "print", a copy of "temp" is created as athe member function "print", a copy of "temp" is created as a local object in the print function with name "s".local object in the print function with name "s". Example ExplanationExample Explanation
  • 21.
     A friendfunction can access all members of a class in which it is declared.A friend function can access all members of a class in which it is declared.  It can also access private and protected members of all other classes evenIt can also access private and protected members of all other classes even though it is not a member function of those classes.though it is not a member function of those classes.  It is a non-member function. It is normally used when a function has toIt is a non-member function. It is normally used when a function has to access private data of other classes.access private data of other classes.  A friend function is defined in a class.A friend function is defined in a class.  It is possible to declare the friend function as either private or public.It is possible to declare the friend function as either private or public.  The function can be invoked without the use of an object. The friendThe function can be invoked without the use of an object. The friend function has its argument as objectsfunction has its argument as objects Friend FunctionFriend Function
  • 22.
     It isdefined by using the keyword “friend” before the name ofIt is defined by using the keyword “friend” before the name of the function in the function declarator.the function in the function declarator.  It can be defined anywhere in the class.It can be defined anywhere in the class.  When a friend function is defined outside the class, it isWhen a friend function is defined outside the class, it is defined without the scope resolution parameters (::).defined without the scope resolution parameters (::).  A program example is given on next slide that uses the friendA program example is given on next slide that uses the friend function.function. Friend FunctionFriend Function (cont..)(cont..)
  • 23.
    Example: Friend FunctionExample:Friend Function #include<iostream>#include<iostream> using namespace std;using namespace std; class exforsysclass exforsys {{ private:private: int a,b;int a,b; public:public: void test()void test() {{ a=100;a=100; b=200;b=200; }} friend int compute(exforsys e1);friend int compute(exforsys e1); //Friend Function Declaration with//Friend Function Declaration with keyword friend and with the object of classkeyword friend and with the object of class exforsys to which it is friend passed to itexforsys to which it is friend passed to it };}; int compute(exforsys e1)int compute(exforsys e1) {{ //Friend Function Definition which has//Friend Function Definition which has access to private dataaccess to private data return int(e1.a+e1.b)-5;return int(e1.a+e1.b)-5; }} main()main() {{ exforsys e;exforsys e; e.test();e.test(); cout << "The result is:" << compute(e);cout << "The result is:" << compute(e); //Calling of Friend Function with//Calling of Friend Function with object as argument.object as argument. }}
  • 24.
     The functioncompute() is a non-member function of the class exforsys. InThe function compute() is a non-member function of the class exforsys. In order to make this function have access to the private data a and b of classorder to make this function have access to the private data a and b of class exforsys , it is created as a friend function for the class exforsys. As a firstexforsys , it is created as a friend function for the class exforsys. As a first step, the function compute() is declared as friend in the class exforsys as:step, the function compute() is declared as friend in the class exforsys as: friend int compute (exforsys e1)friend int compute (exforsys e1) The keyword friend is placed before the function. The function definition isThe keyword friend is placed before the function. The function definition is written as a normal function and thus, the function has access to the privatewritten as a normal function and thus, the function has access to the private data a and b of the class exforsys. It is declared as friend inside the class, thedata a and b of the class exforsys. It is declared as friend inside the class, the private data values a and b are added, 5 is subtracted from the result, givingprivate data values a and b are added, 5 is subtracted from the result, giving 295 as the result. This is returned by the function and thus the output is 295.295 as the result. This is returned by the function and thus the output is 295. Example ExplanationExample Explanation