Object Oriented Programming Lab-08 (Inheritance and Friend Functions)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Object Oriented Programming

Lab Manual

Inheritance and Friend Functions

Objectives

The objective of this Lab is to get familiar students with the C++ implementation of inheritance.Why
we use inheritance and where we have to use this concept.

Inheritance:

Inheritance in C++ takes place between classes. When one class inherits from another, the derived class
inherits the variables and functions of the base class. These variables and functions become part of the
derived class. It is denoted by an arrow symbol () whose head points of Derived class.

Modes of Inheritance

Implementation of Inheritance in C++ Programming

class Person
{
... .. ...
};

1
class MathsTeacher : public Person
{
... .. ...
};

class Footballer : public Person


{
.... .. ...
};

Friend Function In C++

A friend function in C++ is a function that is preceded by the keyword “friend”. When the function is
declared as a friend, then it can access the private and protected data members of the class.

A friend function is declared inside the class with a friend keyword preceding as shown
below.

class className{

……

friend return TypefunctionName(arg list);


};

As shown above, the friend function is declared inside the class whose private and protected data
members are to be accessed. The function can be defined anywhere in the code file and we need not
use the keyword friend or the scope resolution, operator.

There are some points to remember while implementing friend functions in our program:

 A friend function can be declared in the private or public section of the class.
 It can be called like a normal function without using the object.
 A friend function is not in the scope of the class, of which it is a friend.
 A friend function is not invoked using the class object as it is not in the scope of the class.
 A friend function cannot access the private and protected data members of the class directly. It
needs to make use of a class object and then access the members using the dot operator.
 A friend function can be a global function or a member of another class.

Example Of A Friend Function

Let us implement a programming Example to better understand the usage of Friend


Function.

1 #include <iostream>

2
2 #include <string>
3 using namespace std;
4 class Rectangle{
5    int length, breadth;
6   
7    public:
8    Rectangle (int length, int breadth):length(length),breadth(breadth)
9    {}
10    friend void calcArea(sample s); //friend function declaration
11   
12 };
13 //friend function definition
14 void calcArea(Rectangle s){
15    cout<<"The Wall is covering the Area of = "<<s.length * s.breadth;
16    }
17 int main()
18    {
19       Rectangle aWall(10,15);
20       calcArea(s);
21   
22       return 0;
23 }
Output:
Area = 150

In the above program, we have a class Rectangle with private members length and breadth. We
have a public constructor that initializes the values of length and breadth. Next, we have a friend
function “calcArea” that calculates the area by taking length and breadth into account.

Note that calcArea is a friend function and is a not part of the class. In the main function, after
creating an object of the class sample, we pass it to the calcArea function that calculates area and
displays the value.

3
Lab Tasks

Task 1

Write a program in which class A is base class for class B. While Class C is derived from class
B, and class D is derived from class E. Provide explicit implementation of default constructors
and destructors. Write a test program which initializes each class object and shows execution
order of constructor and destructor by printing appropriate messages. Each class must have one
data member of type int.

Task 2

Modify the above classes and provide overloaded constructor implementation for each class.
Use base class initialization for this purpose. Also declare a function friendA as friend of class
A and declare a function friendD as friend of class D

4
Task 3:

Declare a class Person. Person must have name and DOB as its private data members, Data
member DOB must be an object of Date class which must be created before creating the class
Person.

Date class should have three data member as: Day, Month and Year all of type int.

Declare a class Student. Derive Student class from Person class publically. Student has ID(may
be of type int) and array of 5 courses as its private data member. So there must be a class of
Course created before Student class. To calculate the grade of student there must be a member
function CalculateGrade() int Student class.

Class Course must have two data members, int type marks and string type courseName.

Must explicitly write copy constructor for Person, Student, Course and Date as well.

Your classes must incorporate the following main().

int main()
{
int ID = 102;
string Name = “Muhammad”;
Date dateOfBirth(22,12,1998);
Course CourseList[5] = { Course("Marketing",77), Course("OOP",85), Course("English",65),
Course("Probability", 87), Course("DLD", 71)};
Student Muhammad (Name, ID, CourseList, dateOfBirth);
CalculateGrade(Muhammad);
}

You might also like