Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
224 views

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

This document discusses inheritance and friend functions in C++. It provides examples of inheritance between classes like MathsTeacher and Footballer inheriting from the Person class. It also defines friend functions as functions that can access private members of a class. An example friend function calcArea() is shown calculating the area of a sample object by accessing its private length and breadth variables. The lab tasks involve implementing inheritance between classes with constructors and destructors, using base class initialization, and defining classes like Student and Date with attributes and a main function to initialize objects and call methods.

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
224 views

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

This document discusses inheritance and friend functions in C++. It provides examples of inheritance between classes like MathsTeacher and Footballer inheriting from the Person class. It also defines friend functions as functions that can access private members of a class. An example friend function calcArea() is shown calculating the area of a sample object by accessing its private length and breadth variables. The lab tasks involve implementing inheritance between classes with constructors and destructors, using base class initialization, and defining classes like Student and Date with attributes and a main function to initialize objects and call methods.

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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 returnType functionName(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.
#include <iostream>
2
#include <string>
using namespace std;
class sample{
   int length, breadth;

   public:
   sample(int length, int breadth):length(length),breadth(breadth)
   {}
   friend void calcArea(sample s); //friend function declaration

};
//friend function definition
void calcArea(sample s){
   cout<<"Area = "<<s.length * s.breadth;
   }

int main()
   {
      sample s(10,15);
      calcArea(s);

      return 0;
}

Output:
Area = 150

In the above program, we have a class sample 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.

Task 2

Modify the above classes and provide base class initialization implementation for each class.

Task 3:

Declare following classes:

Person
 With attributes Name and DoB(an object of Date class).
Course
 which have data members CourseName and CourseMarks
Date
 With attributes Day, Month, Year (We already have done in one of our previous lab
sessions).
Student
 Derived from Person class
 Student class have the attributes of rollNo(integer)
 Array of 5 courses. (Composition)
 DOB as an object of Date class (to save the date of birth of Student)

Important: Focus on the main function and write your classes exactly according to the satisfy the
main() function given below (next page).

4
int main ()
{
Course myCourses [5] = {
Course("Mrktng",77),
Course("OOP",52),
Course("Eng",65),
Course("Prob", 50),
Course("DLD", 71)
};

Date dateOfBirth (13,12,1997);

Student mahmud (“Mahmud”, 102, myCourses, dateOfBirth);


// Name, ID, List of Courses and Date of birth.

Mahmud.calculateGrade();

Return 0;
}

Sample Output for Task 3:


---------------------------------------
Student Name: Mahmud
Student ID: 102
Date of Birth: 13-12-97

Result:

Mrktng : 77
OOP : 52
Eng : 65
Prob : 50
DLD : 71

Total : 315 / 500


Grade : C
---------------------------------------

You might also like