0% found this document useful (0 votes)
3 views20 pages

Lab 8

This document outlines a lab focused on Object-Oriented Programming in C++, specifically on class hierarchies, multiple and multilevel inheritance. It provides objectives, introductions to inheritance concepts, and detailed tasks including creating classes for Person, Employee, Date, Time, and their interactions. Additionally, it includes coding examples and post-lab tasks to reinforce the concepts learned.

Uploaded by

abdul.59wa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Lab 8

This document outlines a lab focused on Object-Oriented Programming in C++, specifically on class hierarchies, multiple and multilevel inheritance. It provides objectives, introductions to inheritance concepts, and detailed tasks including creating classes for Person, Employee, Date, Time, and their interactions. Additionally, it includes coding examples and post-lab tasks to reinforce the concepts learned.

Uploaded by

abdul.59wa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1

Object-Oriented Programming

LAB-08
Class Hierarchies ( Multiple and Multilevel Inheritance)

1. OBJECTIVES :
 To get familiar with the C++ implementation of inheritance
 To get familiar with class hierarchies in C++.
 To differentiate between multiple and multilevel inheritance in C++.

2. INTRODUCTION:
Multiple and MultilevelInheritance:
Inheritance is one of the features of Object-Oriented Programming, it allows the
child class (derived class) to acquire the properties (the data members) and
functionality (the member functions) of parent class (base class).

Protected Members and class Access Modes


The various ways we can derive classes are known as access modes of class.
These access modes have the following effect:
public: If a derived class is declared in public mode, then the members of the
base class are inherited by the derived class just as they are.
private: In this case, all the members of the base class become private members
in the derived class.
protected: The public members of the base class become protected members in
the derived class. The private members of the base class are always private in the
derived class.

Constructors and destructors in inheritance


Constructor calling always starts with the base class; first base class constructor
is called, and its data members are initialized than derived class constructor is
called and its members are initializedThey are initialized in the order they are
declared. For multiple inheritance order of constructor call is, the base class’s
constructors are called in the order of inheritance and then the derived class’s
constructor.
For multilevel inheritance is same base class constructors are called first and the
derived class constructors are called next in single inheritance.
The order of the destructor is exactly the reverse.

3. IN LAB TASKS
TASK#01
Create a class Person having name, age and gender as its data members. Create another
class Department which has DepartmentName and ProgramName as it data members.
Derive a class Student from class Person and class Department which has student
ID,grade and number of courses as its member variables
. i. Write set and get functions to enter and display the data members.
1

Object-Oriented Programming

ii. Write main function to implement these classes. Enter the student data to show
multiple inheritance.
 TASK CODE :

#include<iostream>
using namespace std;
class Person
{
protected:
string name;
int age;
char gender;
public:
void set(){
cout<<"\n enter name ";cin>>name;
cout<<"\n Enter age ";cin>>age;
cout<<"\n Enter gender ";cin>>gender;
}
void get(){
cout<<"\nAge is "<<age<<"\nName is "<<name<<"\
nGender is "<<gender;
}

};
class Dep:public Person
{
private:
string D_Name;
string program ;
public:
void set(){
Person::set();
cout<<"\nEnter department name ";cin>>D_Name;
cout<<"\nEnter Program name ";cin>>program;
}
void get(){
Person::get();
cout<<"\n Department name is "<<D_Name<<"\
nProgram nae is "<<program;
}

};
int main(){
Dep d;
d.set();
d.get();
}
1

Object-Oriented Programming

Console window

TASK#02
Design a class named Employee. The class should keep the following information
in
•Employee name
•Employee number
•Hire date
Write one or more constructors and the appropriate accessor and mutator
functions for the class. Next, write a class named ProductionWorker that is
derived from the Employee class. The ProductionWorker class should have
member variables to hold the following information:
•Shift (an integer)
•Hourly pay rate (a double ) The workday is divided into two shifts:
day and night. The shift variable will hold an integer value representing the shift
that the employee works. The day shift is shift 1, and the night shift is shift 2. For
night shift hourly rate will be doubled, write down a function called salary to
calculate the total salary of worker. Write one or more constructors and the
appropriate accessor and mutator functions for the class. Demonstrate the classes
by writing a program that uses a ProductionWorker object, ask the user for how
many workers he wants to store the data and then display the recorded data.

 TASK CODE :

#include<iostream>
using namespace std;
1

Object-Oriented Programming

class emp
{
protected:
string name;
int number;
string hiredate;
public:
emp(){
name="";
number=0;
hiredate="";
}
void accessor (){
cout<<"\nenter name ";cin>>name;
cout<<"\n enter number ";cin>>number;
cout<<"\n enter hiredate ";cin>>hiredate;
}
void mutator(){
cout<<"\n\tEnterd information is as follow::\n name
"<<name<<"\n number "<<number<<"\n hiredate "<<hiredate;
}
};
class PW:public emp
{
private:
int shift;
double HP;
int hrs;
public:
void get(){
emp::accessor();
cout<<"\nenter shift time 1 for day/2 for night
";cin>>shift;
cout<<"\nenter hourly pay rate ";cin>>HP;
cout<<"\nenter number of hours ";cin>>hrs;
}
void put(){
cout<<"\n\
t~~~~~~~~~~~~~~~~~~~~~~~~~Employee
Information~~~~~~~~~~~~~~~~~~~~~~~~~";
emp::mutator();
cout<<"\nshift = "<<shift<<"\nHourly pay rate is
"<<HP<<"\nHours of Working are "<<hrs;
if(shift==1){
cout<<"\nEmployee earning is "<<HP*hrs;
}
else if(shift==2){
cout<<"\nEmployee earning is
"<<2*HP*hrs;
}
1

Object-Oriented Programming

}
};
int main(){
PW p;
p.get();
p.put();
}
Console Window

i. TASK #03
Create a class Date having day, month & year as its data members. Create another
class called Time with its data members as hours, minutes & seconds. Write down
the following functions for both classes:
a) void display(); // to displays the data
b) get() function // to accesses the data members
c) void set(); // to sets the values of data members

Define a class DateandTime from above two classes which displays both date and
time.
a) Define an instance object of class Date Time called Watch.
b) Write a main () function that would initialize the values through the
constructor functions, and then allows them to be reset through the set ()
functions. Be sure and display the results following the constructor before
you use the set functions.
c) Through the use of the display () function, the time and date are to be
displayed. Note that the display () functions in all three classes need to be
defined, as well as the constructor and all the access functions.
1

Object-Oriented Programming

• TASK CODE :

#include<iostream>//standard library for C++


#include<cstring>//C++ library for strings
usingnamespace std;
classDate//Declaration of base class Date
{
private: //Not accessible from outside the class
int date,month,year; //declaration of data variable of type int
public: //Accessible from outside the class
Date(int d,intm,int y) //3 argument constructor of base class Date
{ //initializing private data variables
date=d; month=m; year=y;
}
void get() //member function to get data from user
{
cout<<"\tEnter Date : ";cin>>date;
cout<<"\tEnter Month : ";cin>>month;
cout<<"\tEnter Year : ";cin>>year;
}
void set_data() //member function to set data variable to zero
{
date=0;month=0; year=0;
}
void display() //member function to display data stored in variables
{
cout<<"\n\t"<<date<<" / "<<month<<" / "<<year;
}
};
classTime//Declaration of base class Date
{
private: //Not accessible from outside the class
int hours,minutes,seconds;
public: //Accessible from outside the class
Time(int h,intm,int s) //3 argument constructor of base class Time
{//initializing private data variables
hours=h;minutes=m; seconds=s;
}
void get() //member function to get data from user
{
cout<<"\tEnter Hours : ";cin>>hours;
cout<<"\tEnter Minutes: ";cin>>minutes;
cout<<"\tEnter Seconds : ";cin>>seconds;
}
void display() //member function to display data stored in variables
{
if(hours<12) //executes when hours less than 12
{
cout<<"\n\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" AM";
}
else//executes when hours greater than 12
{
cout<<"\n\t"<<hours<<" : "<<minutes<<" : "<<seconds<<" PM";
}
1

Object-Oriented Programming

}
void set_data() //member function to set data variable to zero
{
hours=0;minutes=0;seconds=0;
}
};

classDateTime:publicDate,public Time //Declaration of class DateTime derived publically from


2 base classes
{
public:
DateTime(int u,intv,intw,intx,inty,int z):Date(u,v,w),Time(x,y,z) //6 argument constructor
{ }
void get() //member function to get data from user
{
Date::get(); Time::get(); //calls get function of base class Date and Time
}
void display() //member function to display data stored in variables
{
Date::display(); Time::display(); //calls display function of base class Date and Time
}
void set_data() //member function to set data variable to zero
{
Date::set_data(); Time::set_data(); //calls set_data function of base class Date and Time
}
};
int main()
{
DateTime watch(3,12,2021,13,45,52); //declaration and initialization of derived class object
cout<<"\n\t--------------------- Date and Time ---------------------\n";
watch.display(); //Display function
char choice;
do
{
cout<<"\n\n\n\t----------------- Select an option ---------------------- ";
cout<<"\n\t1. Enter R to reset to 0 \n\t2. Enter S to reset to some value ";
cout<<"\n\t3. Enter E to exit \n\n\tEnter choice ";cin>>choice;
switch(choice)
{
case 'R':
watch.set_data();
cout<<"\n\n\t--------------- Date and Time After reset to 0 --------------------\n";
watch.display();
break;
case 'S':
watch.get();
cout<<"\n\n\t------------ Date and Time After set to new value -------------\n";
watch.display();
break;
}
}
while(choice!='E'); //exits when choice E is entered
cout<<"\n\n\n";
}
1

Object-Oriented Programming

• OUTPUT

4. POST LAB TASKS

i. TASK # 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.
a). Define the classes shown in the following class hierarchy diagram:
1

Object-Oriented Programming

b). Define the constructors. When a regular employee is created, basic must be a
parameter.
When adhoc employee is created wage must be a parameter.
c). Define the destructors.
d). Define the member functions for each class. The member function days ( )
updates number of the Adhoc employee.
e). Write a test program to test the classes.

• TASK CODE :

#include <iostream>
#include <cstring>//Library for strings in C++
usingnamespace std;
classEmployee//Declaration of base class Employee
{
private: //Not accessible from outside the class
string name; int eno;//Data variables declaration
public: //Accessible from outside the class
Employee() //Default argument constructor
{
name=""; eno=0; //Initialize data variable to zero
}
void set_data() //member function to take data
{
cout<<"\tEnter name : ";cin>>name;
cout<<"\tEnter employee number : ";cin>>eno;
}
void show_data() //member function to show data
{
cout<<"\n\tName : "<<name;
cout<<"\n\tEmployee number :"<<eno;
}
void Salary() //member function for salary as mentioned
{ }
};
classRegular :public Employee//declaration of derived class Regular publically from base
class Employee
{
private: //Not accessible from outside the class
float basic; //Data variables declaration
1

Object-Oriented Programming

public: //Accessible from outside the class


Regular(float basic) //1 argument constructor
{
this->basic=basic; //this operator
}
float salary() //member function to calculate salary of Regular employee
{
float DA=basic*0.1; //DA is 10% of basic
float HRA=basic*0.3; //HRA is 30% of basic
return (DA+HRA+basic); //Return calculated salary by summing all
}
void show_data() //To show data
{
Employee::show_data(); //calls show_data function of base class Employee
cout<<"\n\tSalary of Regular employee : "<<salary();
}
};
classAdhoc:public Employee //declaration of derived class Adhocpublically from base
class Employee
{
private: //Not accessible from outside the class
int number;float wage; //data variables declaration
public: //Accessible from outside the class
Adhoc(float wage) //1 argument constructor
{
this->wage=wage;
}
float salary()
{
return (number*wage); //returns calculated salary of Adhoc employee
}
void days(int n) //Member function to update number of adhoc employee
{
this->number=n; //update number variable
}
void show_data() //member function to show data
{
Employee::show_data(); //calls show_data function of base class Employee
cout<<"\n\tSalary of Adhoc employee : "<<salary();
cout<<"\n\tNumber of Adhoc employee : "<<number;
}
};
int main()
{
Regular R_emp(35000);//basic salary is a parameter of R_emp object of class Regular
AdhocA_emp(1000); //wages is a parameter of A_emp object of class Regular

cout<<"\n\t****** Enter data for Regular Employee ******\n";R_emp.set_data();


cout<<"\n\t******* Enter data for Adhoc Employee *******\n";A_emp.set_data();
A_emp.days(5); //update number of adhoc employee
system("pause");
system("cls"); //To clear data from screen
cout<<"\n\t****** Data of Regular Employee ******\n";R_emp.show_data();
cout<<"\n\t****** Data of Adhoc Employee ******\n";A_emp.show_data();
1

Object-Oriented Programming

cout<<endl<<endl;
return 0;
}

• OUTPUT :

After system (“cls”);

ii. TASK # 2
Write a class LocalPhone that contains an attribute phone to store a local telephone
number. The class contains member functions to input and display phone number.
Write a child class NatPhone for national phone numbers that inherits LocPhone
class. It additionally contains an attribute to store city code. It also contains member
functions to input and show the city code. Write another class IntPhone for
international phone numbers that inherit NatPhone class. It additionally contains an
attribute to store country code. It also contains member functions to input and show
1

Object-Oriented Programming

the country code. Test these classes from main() by creating objects of derived classes
and testing functions in a way that clear concept of multi-level Inheritance.

• TASK CODE :

#include<iostream>
usingnamespace std;

classLocalphone//declaration of base class Local phone


{
private: //not accessible from outside the class
int phone;
public: //accessible from outside the class
void get_data() //member function to get data
{
cout<<"\tEnter Phone Number : ";cin>>phone;
}
void show_data() //member function to display data
{
cout<<"\n\tPhone Number : "<<phone;
}
};
classNATphone:publicLocalphone//NATphone class derived fomLocalphone class
{
private: //not accessible from outside the class
int city_code;
public:
void get_data() //member function to get data
{
Localphone::get_data();
cout<<"\tEnter City Code : ";cin>>city_code;
}
void show_data() //member function to display data
{
Localphone::show_data(); // calls sh_owdata function of localphone class
cout<<"\n\tCity Code : "<<city_code;
}
};
classIntphone:publicNATphone//Intphone class derived from NATclass
{
private: //not accessible from outside the class
int country_code;
public:
void get_data() //member function to get data
{
NATphone::get_data(); // calls get_data function of NATphone class
cout<<"\tEnter Country Code : ";cin>>country_code;
}
void show_data() //member function to display data
{
NATphone::show_data(); // calls show_data function of NATphone class
cout<<"\n\tCountry Code : "<<country_code;
}
1

Object-Oriented Programming

};
int main()
{
Localphone obj1; //declaring object of base class Localphone
NATphone obj2; //declaring object of derived class Localphone
Intphone obj3; //declaring object of derived class Localphone
cout<<"\n\t****** ENTER DATA FOR LocalPhone class ******\n";obj1.get_data();
cout<<"\n\t****** ENTER DATA FOR NATphonecLass ******\n";obj2.get_data();
cout<<"\n\t****** ENTER DATA FOR Intphone class ******\n";obj3.get_data();
system("pause");
system("cls");
cout<<"\n\t****** DATA ENTERED FOR LocalPhone class******\n";obj1.show_data();
cout<<"\n\t****** DATA ENTERED FOR NATphone class ******\n";obj2.show_data();
cout<<"\n\t****** DATA ENTERED FOR Intphone class ******\n";obj3.show_data();
}

• OUTPUT :

After system (“cls”);

5. CONCLUSION :
1

Object-Oriented Programming

6. Through this
weshould be able to
differentiate
between public and
privateinheritance.
7. ➢We should also
learn the role of
constructor and
destructors in
derived classes.
8. ➢We should aslo
be able to
implement
multilevel
1

Object-Oriented Programming

inheritance and
multiple inheritance.
 In this lab, we learn how to declare the derived classes along with the access
ofbase class members. We also learn the purpose of protected Access specifier
and working with derived class constructors. And briefly discuss overloading
ofDefault constructors, how it happens.
 Through this we should be able to differentiate between public and
privateinheritance.
 We observed the order of execution of constructor and destructors in multiple and
multilevel inheritance.
 We are able to implement multilevel inheritance and multiple inheritances.
 We know the difference in multiple and multilevel inheritance.

9. Object Oriented
Programming
(CSC241) Page 13
10.
11. Watch.display();
12. cout<<"\nAFTER
SET FUNCTION";
13. Watch.sett(31,0
5,2000,20,30,35);
14. Watch.display();
1

Object-Oriented Programming

15.
16. return 0;
17. }
18. Output:
19.
20.
21.
22. Conclusion:
23. ➢Through this
weshould be able to
differentiate
between public and
privateinheritance.
1

Object-Oriented Programming

24. ➢We should also


learn the role of
constructor and
destructors in
derived classes.
25. ➢We should aslo
be able to
implement
multilevel
inheritance and
multiple inherita
26. Object Oriented
Programming
(CSC241) Page 13
27.
1

Object-Oriented Programming

28. Watch.display();
29. cout<<"\nAFTER
SET FUNCTION";
30. Watch.sett(31,0
5,2000,20,30,35);
31. Watch.display();
32.
33. return 0;
34. }
35. Output:
36.
37.
38.
39. Conclusion:
1

Object-Oriented Programming

40. ➢Through this


weshould be able to
differentiate
between public and
privateinheritance.
41. ➢We should also
learn the role of
constructor and
destructors in
derived classes.
42. ➢We should aslo
be able to
implement
multilevel
1

Object-Oriented Programming

inheritance and
multiple inheritan
----------------------------------------------------------------------------------

You might also like