Professional Documents
Culture Documents
Object Oriented Programming Lab Journal - Lab 10: Objective
Object Oriented Programming Lab Journal - Lab 10: Objective
Enrollment #: _________________________________
Class: _________________________________
Objective
This lab session is intended to provide an overview of friend functions and friend classes in C++.
Exercise 1
Exercise 2
Develop a class Age which stores age of an individual in years and months. Using friend function,
overload the ‘+’ operator to add an integer to an object of class Age. The integer should be
added to the month field of age. Your program should allow statements like the following.
Age a(20,5) ; //20 years , 5 months
Age b ;
b = a+5 ;//20 years, 10 months
Exercise 3
Create a class MyClass with an integer data member ‘secret’. Provide a constructor and display
method in the class. Create another class, MyFriendClass and make it a friend of the class
MyClass. Provide a function change() in the class MyFriendClass which accepts an object of class
MyClass (by reference) and an integer. The change() function should assign the integer passed
as an argument to the data member ‘secret’ of the object received as input.
Sample Run :
MyClass m(2) ;
MyFriendClass mf ;
mf.change(m,10) ;
m.display() ; //Should print 10
Program the given exercises and get them checked by your instructor.
2. Exercise 2
3. Exercise 3
+++++++++++++++++++++++++