Class Hierarchies: 1. Objectives
Class Hierarchies: 1. Objectives
Class Hierarchies
1. Objectives
To familiarize the students with class hierarchies, multiple inheritance and multilevel
inheritance .
2. Outcome
After this lab the students should be able to differentiate between public and private
inheritance. They should learn the role of constructor and destructors in derived classes. They
should be able to implement multilevel inheritance and multiple inheritance.
3. Introduction
3.1. Public and private inheritance
The public keyword in the inheritance syntax means that publicly accessible members
inherited from the base class stay publicly accessible in the derived class. But for private
keyword in the inheritance syntax means that accessible members inherited from the base
become private members of derived class.
3.2. Constructors and Destructors in Derived Classes
Derived classes do not inherit constructors or destructors from their base classes, but they do
call the constructor and destructor of base classes. When an object of a derived class is
created, the base class’s constructor is executed first, followed by the derived class’s
constructor. When an object of a derived class is destroyed, its destructor is called first, then
that of the base class.
3.3. Multiple inheritance and Multi-level inheritance
Classes can be derived from other classes that are already derived from other classes. This creates
multi-level Inheritance
Syntax:
class A
{ public:
int x;
};
class B : public A
{ public:
int y;
};
class C : public B { };
A Class can be derived from more than one base class. This is called Multiple
inheritance.
Syntax:
class A
{ };
class B
{ };
class C: public A, public B
{ };
4. Examples
4.1. This example demonstrates the Public and Private Inheritance between the
classes.
#include <iostream>
class base
{
private:
int pridataA; //private data member
protected:
int prodataA; //protected data member
public:
int pubdataA; //public data member
};
class student
{ protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{ protected:
int sm; // sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{ int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal \t:"<<tot;
cout<<"\n\tAverage : "<<avg;
}
};
int main()
{
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
4.3.This example demonstrates the concept of multilevel inheritance between the classes.
#include<iostream>
using namespace std;
class base1
{
protected:
int number1;
public:
void showA()
{
cout<<"enter a value for first number:"<<endl;
cin>>number1;
}
};
class base2:public base1
{ protected:
int number2;
public:
void showB()
{
cout<<"enter value for second number:"; cin>>number2;
}
};
class derive: public base2
{
public:
void showC()
{
showA(); //accessing base1's function
showB(); //accessing base2's function
cout<<"number1*number2 ="<<number1*number2;
}
};
int main()
{ derive ob1;
ob1.showC();
cout<<endl;
system("pause");
}
5. Lab Tasks
5.1. Create a Class named base which has two data members. Then derive a class derived1
from base class which has one data members. Derive a class derived2 from derived1.
i. Write functions for each class to get and display values.
ii. Write main() function to create object of derived2 and through that object
access the data member of base class and derived1 class
5.2. Create a class Person having name, age and gender as its data members. Create another
class Employee which has employername and dailywages as it data member. From these
two classes derive another class teacher which contains teacher grade as data member.
i. Write set and get functions to enter and display the data members.
ii. Write main function to implement these classes. Enter the teacher data to show
multiple inheritance.
5.3. Consider the following classes
#include <iostream.h>
class Date
{
int day;
int month;
int year;
public:
Date();
~Date();
void display(); // displays the date
Date get(); // accesses the date
members
void set(); // sets the date members
};
class Time
{
int hour;
int minute;
int second;
public:
Time();
~Time();
void display(); // displays the time
Time get(); // accesses the time members
void set(); // sets the time members
};
6. Home Tasks
6.1. An organization has two types of employees: regular and adhoc. Regular employees
get a salary which is basic + DA + HRA where DA is 10% of basic and HRA is 30% of
basic.Adhoc employees are daily wagers who get a salary which is equal to Number *
Wage.
(i) Define the classes shown in the following class hierarchy diagram:
(ii) Define the constructors. When a regular employee is created, basic must be a
parameter.
When adhoc employee is created wage must be a parameter.
(iii) Define the destructors.
(iv) Define the member functions for each class. The member function days ( ) updates
number of the Adhoc employee.