Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Lec_15_OOP_ObjectsAsParametersFriendClasses.pdf
1. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Women University of Azad Jammu and Kashmir Bagh
Department of Computer Sciences and Information Technology, Women University of Azad Jammu and Kashmir Bagh
www.wuajk.edu.pk
Object Oriented Programming
CS-3201
Dr. Anees Qumar Abbasi
aneesabbasi.wuajk@gmail.com
2. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Objects as Function Parameters
& Return Type
Friend Classes & Functions
3. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Object as Function Parameters
• Objects can also be passed as parameters to member functions.
• The method of passing objects to a functions as parameters is as
passing other simple variables.
• It will easily be understand by following example.
4. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Example: Object as Parameter
void add(Travel p)
{
Travel t;
t.km=km+p.km;
t.hr=hr+p.hr;
cout<<“Total traveling
is ”<<t.km<<“ kilometers
in
”<<t.hr<<“ hours”<<endl;
}
};
void main()
{
Travel my, your;
my.get();
my.show();
your.get();
your.show();
my.add(your);
getch();
}
class Travel
{
private:
int km, hr;
public:
Travel()
{
km=hr=0;
}
void get()
{
cout<<“Enter Kilometers
traveled”;cin>>km;
cout<<“Enter Hours
traveled”;cin>>hr;
}
void show() {
cout<<“You traveled
”<<km<<“ in ”<<hr<<“
hours”<<endl;
}
5. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
How Program Works
void add(Travel p) {
Travel t;
t.km = km + p.km;
t.hr = hr + p.hr;
}
• Data members of temporary object t
• Data members of temporary object my
• Data members of temporary object p
• The above program declares to objects of class Travel and inputs data in both objects.
• The add() function accepts an object of type Travel as parameter.
• It adds the values of data members of the parameter object and the values of calling object’s data members
and displays the result.
• The working of member function add() is as follows:
Function call in main
my.add(your);
6. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Returning Objects from Member Function
• The method of returning an object from member function is same as
returning a simple variable.
• If a member function returns an object, its return type should be the
same as the type of object to be returned.
7. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Example: Object as Parameter
Travel add(Travel p)
{
Travel t;
t.km=km+p.km;
t.hr=hr+p.hr;
return t;
}
};
void main()
{
Travel my, your, r;
my.get();
my.show();
your.get();
your.show();
r = my.add(your);
cout<<“Total travelling is as
follow:n”;
r.show();
getch();
}
class Travel
{
private:
int km, hr;
public:
Travel()
{
km=hr=0;
}
void get()
{
cout<<“Enter Kilometers
traveled”;cin>>km;
cout<<“Enter Hours
traveled”;cin>>hr;
}
void show() {
Cout<<“You traveled
”<<km<<“ in ”<<hr<<“
hours”<<endl;
}
8. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
• The add() function in the above program accepts a parameter object
of type Travel.
• It adds the contents of parameter and calling object and stores the
result in a temporary object.
• The function then returns the whole object back to main() function
that is stored in r.
• The program finally displays the result using r.show() statement.
How Program Works
9. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Friend Classes
A friend class can access private and protected members of other classes in
which it is declared as a friend. It is sometimes useful to allow a particular
class to access private and protected members of other classes.
We can declare a friend class in C++ by using the friend keyword.
friend class class_name; // declared in the base class
10. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Example: Friend Class
class GFG {
private:
int private_variable;
protected:
int protected_variable;
public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}
// friend class declaration
friend class F;
};
// Here, class F is declared as a
friend inside class GFG. Therefore,
F is a friend of class GFG. Class F
can access the private members of
class GFG.
class F {
public:
void display(GFG& t)
{
cout << "The value of Private Variable
= "<< t.private_variable << endl;
cout << "The value of Protected
Variable = "<< t.protected_variable;
}
};
// Driver code
int main()
{
GFG g;
F fri;
fri.display(g);
return 0;
} Output:
The value of Private Variable = 10
The value of Protected Variable = 99
11. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Friend Function
Like a friend class, a friend function can be granted special access to private and
protected members of a class in C++. They are the non-member functions that can
access and manipulate the private and protected members of the class for they are
declared as friends.
A friend function can be:
1. A global function
2. A member function of another class
12. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Friend Function
Syntax:
friend return_type function_name (arguments); // for a global function
or friend return_type
class_name::function_name (arguments);// for a member function of other class
13. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Example: Friend Function
1. Global Function as Friend Function
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
// friend function declaration
friend void friendFunction(base&
obj);
};
// friend function definition
void friendFunction(base& obj)
{
cout << "Private Variable:" <<
obj.private_variable<< endl;
cout << "Protected Variable:" <<
obj.protected_variable;
}
int main()
{
base object1;
friendFunction(object1);
return 0;
}
Output:
Private Variable: 10
Protected Variable: 99
14. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Example: Friend Function
2. Member Function of Another Class a Friend Function
class anotherClass {
public:
void memberFunction();
};
// base class for which friend is
declared
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{ private_variable = 10;
protected_variable = 99; }
// friend function declaration
friend void
anotherClass::memberFunction();
};
// friend function definition
void anotherClass::memberFunction(base&
obj)
{
cout << "Private Variable: " <<
obj.private_variable<< endl;
cout << "Protected Variable: " <<
obj.protected_variable;
}
Void main()
{
base object1;
anotherClass object2;
object2.memberFunction(object1);
}
Output:
Private Variable: 10
Protected Variable: 99
15. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Features of Friend Functions
q A friend function is a special function in C++ that in spite of not being a member
function of a class has the privilege to access the private and protected data of a
class.
q A friend function is a non-member function or ordinary function of a class, which is
declared as a friend using the keyword “friend” inside the class. By declaring a
function as a friend, all the access permissions are given to the function.
q The keyword “friend” is placed only in the function declaration of the friend function
and not in the function definition or call.
q A friend function is called like an ordinary function. It cannot be called using the
object name and dot operator. However, it may accept the object as an argument
whose value it wants to access.
q A friend function can be declared in any section of the class i.e. public or private or
protected.
16. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Friend Functions
Advantages of Friend Functions
• A friend function is able to access members without the need of inheriting the
class.
• The friend function acts as a bridge between two classes by accessing their
private data.
• It can be used to increase the versatility of overloaded operators.
• It can be declared either in the public or private or protected part of the class.
Disadvantages of Friend Functions
• Friend functions have access to private members of a class from outside the class
which violates the law of data hiding.
• Friend functions cannot do any run-time polymorphism in their members.
17. Department of Computer Sciences & Information Technology
Dr. Anees Qumar Abbasi- Object-Oriented Programming
Friend Functions & Classes
v Friends should be used only for limited purposes. Too many functions or external
classes are declared as friends of a class with protected or private data access
lessens the value of encapsulation of separate classes in object-oriented
programming.
v Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of
A automatically.
v Friendship is not inherited.
v The concept of friends is not in Java.