Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Lab Journal – Lab 10

Object Oriented Programming

Lab Journal - Lab 10


Name: _________________________________

Enrollment #: _________________________________

Class: _________________________________

Objective

This lab session is intended to provide an overview of friend functions and friend classes in C++.

Exercise 1

Create a class 3D point to model points in 3-dimensional space. Provide appropriate


constructors and get/set methods in the class. Using friend functions, overload the following
operators.
+ operator to add two 3D points.
- operator to subtract two 3D points.
~ operator to find the distance between two points.

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

Object Oriented Programming Page 1


Lab Journal – Lab 10

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.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

3. Exercise 3

+++++++++++++++++++++++++

Object Oriented Programming Page 2

You might also like