Lab 8
Lab 8
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).
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 :
Object-Oriented Programming
}
void set_data() //member function to set data variable to zero
{
hours=0;minutes=0;seconds=0;
}
};
Object-Oriented Programming
• OUTPUT
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
Object-Oriented Programming
cout<<endl<<endl;
return 0;
}
• OUTPUT :
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;
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 :
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
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
Object-Oriented Programming
inheritance and
multiple inheritan
----------------------------------------------------------------------------------