KEMBAR78
Object Oriented Programming 29 10 | PDF | Subroutine | Object Oriented Programming
0% found this document useful (0 votes)
67 views18 pages

Object Oriented Programming 29 10

This document discusses object-oriented programming concepts like passing objects to functions, returning objects from functions, friend functions, and friend classes. It provides examples of passing class objects as arguments to functions, returning a class object from a function, and declaring friend functions and classes to access private members of other classes.

Uploaded by

Sulaman Sadiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views18 pages

Object Oriented Programming 29 10

This document discusses object-oriented programming concepts like passing objects to functions, returning objects from functions, friend functions, and friend classes. It provides examples of passing class objects as arguments to functions, returning a class object from a function, and declaring friend functions and classes to access private members of other classes.

Uploaded by

Sulaman Sadiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Object-Oriented Programming

(OOP)
Passing Object to Function

 Class Objects can be passed to functions


as arguments
class Complex
{
private:
int real, imag;
public:
Complex(): real(0), imag(0) { }
void Read()
{ cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
void Add(Complex comp1,Complex comp2)
{
real=comp1.real+comp2.real;
imag=comp1.imag+comp2.imag;
} void Display() { cout<<"Sum="<<real<<"+"<<imag<<"i"; }
};
int main()
{
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3.Add(c1,c2);
c3.Display();
return 0;
}
 Returning a class object from function
class Complex
{
private:
int real, imag;
public:
Complex(): real(0), imag(0) { }
void Read()
{ cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
Complex Add(Complex comp2)
{
Complex temp;
temp.real=real+comp2.real;
temp.imag=imag+comp2.imag;
return temp;
}
void Display() { cout<<"Sum="<<real<<"+"<<imag<<"i"; }
};
int main()
{
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3=c1.Add(c2);
c3.Display();
return 0;
}
Friend Functions
Consider the following class:
class X{
private:
int a, b;
public:
void MemberFunction();

}
Friend Functions

Global function:
void DoSomething(X obj)
{
obj.a = 3; //Error
obj.b = 4; //Error
}
Friend Functions

In order to access the member variables


of the class, function definition must be
made a friend
Example
function:
class X
{
private:
int a, b;
public:

friend void DoSomething(X obj);
};
Now the function DoSomething can
access data members of class X
Friend Functions

►Prototypes of friend functions appear in


the class definition

►But friend functions are NOT member


functions
Friend Functions
► Friendfunctions can be placed anywhere in the class
without any effect
► Access specifiers don’t affect friend functions or classes
class X{
...
private:
friend void DoSomething(X);
public:
friend void DoAnything(X);
...
};
Friend Functions
►While the definition of the friend function is:

void DoSomething(X obj)


{
obj.a = 3; // No Error
obj.b = 4; // No Error

}
►friend keyword is not given in definition
Friend Functions
►Ifkeyword friend is used in the function
definition, it’s a syntax error

//Error…

friend void DoSomething(X obj)


{

}
Friend Classes
• Similarly, one class can also be made
friend of another class:
class X{
friend class Y;

};

• Member functions of class Y can access


private data members of class X
Friend Classes
class Y{
private:
int y_var1, y_var2;
X objX;
public:
void setX(){
objX.x_var1 = 1;
}
};
Friend Classes

int main()
{
Y objY;
objY.setX();
return 0;
}

You might also like