OOP Lab Report-8

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

Lab#8

Lab Title
Class Hierarchies (Multi-Level & Multiple Inheritance)
1. Objectives:
In this lab we will came to know the about class hierarchies, multiple inheritance and multi-
level inheritance.

2. Introduction:
In Object-oriented programming, Public keyboard means that publicly accessible from base
class to that are using in derived class. Private keyboard means that privately accessible from
base class to that are using in derived class. Derived class don’t have constructor so when we
make derived class object first that object call constructor of base class. Same for destructor
first base class destructor is called then derived class destructor is called. Multi-level
inheritance means you inherit a class from already derived class.

3. In-Lab Tasks: Following are the in-lab tasks:


1.3. Task#1
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.
ii. Write main function to implement these classes. Enter the student data to show multiple
inheritance.

Task Description:
In this task, we simply take a class name as person in which we take sting name, gender and
int age then form get function to get data and show function to show data. Then we make
one more base class employee in which we take int id and float salary then we make same
function to get and show data. After that we use multiple inheritance to inherit two base
class in one derived class. After that calling function from main with derived object.

Code:
#include <iostream>
#include <string.h>
using namespace std;
class Person //Declaration of base class
{
private:
string name,gender;
int age;
public:
void get_data()

Object Oriented Programming Page 1


{
cout<<"Enter Name: ";cin>>name;
cout<<"Enter Age: ";cin>>age;
cout<<"Enter Gender: ";cin>>gender;
}
void show_data()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<" years Old"<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
class Employee //Declaration of base class
{
private:
int ID;
float salary;
public:
void get_data()
{
cout<<"Enter ID: ";cin>>ID;
cout<<"Enter Salary: ";cin>>salary;
}
void show_data()
{
cout<<"ID: "<<ID<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};
class BankManager:public Person,public Employee
{
private:
string BankName;
int branchcode;
public:
void get_data()
{
Person::get_data();
Employee::get_data();
cout<<"Enter Bank Name: ";cin>>BankName;
cout<<"Enter Branch Code: ";cin>>branchcode;
}
void show_data()
{
Person::show_data();
Employee::show_data();
cout<<"Bank Name: "<<BankName<<endl;
cout<<"Branch Code: "<<branchcode<<endl;
}
};
int main()

Object Oriented Programming Page 2


{
BankManager B1; //Declaration of object of derived class
B1.get_data();
B1.show_data();
}
Output:

Task#2
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 Description:
In this task we simply take a class name as employee in which we take string type employee
name, in type employee number and int type hire date then we make get function and set
function to get value from user and set to display it. After that we make one other class for
production worker shift and hourly pay rate then add a total salary calculating function in it.
Calling all function from main with help of object of derived classes.

Code:
#include <iostream>
#include <string>
using namespace std;

Object Oriented Programming Page 3


class Employee //Base class
{
private:
string Emp_Name;
int Emp_Num;
int Hire_Date;
public:
void set_Emp_Name(string x) //Accessor function
{
Emp_Name = x;
}
void set_Emp_Num(int y)
{
Emp_Num = y;
}
void set_Hire_Date(int z)
{
Hire_Date = z;
}
string get_Emp_Name() const //Mutator function
{
return Emp_Name;
}
int get_Emp_Num() const
{
return Emp_Num;
}
int get_Hire_Date() const
{
return Hire_Date;
}
};
class Production_Worker : public Employee //Derive class from
employee
{
private:
int Shift;
double Hourly_Pay_Rate;

public:
void set_Shift(int a) //Accessor function
{
Shift = a;
}
void set_Hourly_Pay_Rate(double b)
{
Hourly_Pay_Rate = b;
}
int get_Shift() const //Mutator function
{
return Shift;

Object Oriented Programming Page 4


}
double Total_Salary()
{
int salary;
if(2==Shift)
{
salary=Hourly_Pay_Rate*2;
return salary;
}
else return Hourly_Pay_Rate;
}
};
int main()
{
Production_Worker info; //Declaration of object of
derived class
char name[100];
int num;
int date;
int shift;
double rate;
cout << "Name of employee: ";
cin.getline(name, 100);
cout << "Number of employee: ";
cin >> num;
cout << "Hire date of employee: ";
cin >> date;
cout << "Does the employee work shift 1 or shift 2? ";
cin >> shift;
cout << "How much does the employee make per hour? ";
cin >> rate;
info.set_Emp_Name(name);
info.set_Emp_Num(num);
info.set_Hire_Date(date);
info.set_Shift(shift);
info.set_Hourly_Pay_Rate(rate);
cout << "Employee Name: " << info.get_Emp_Name() << endl;
cout << "Employee Number: " << info.get_Emp_Num() <<
endl;
cout << "Employee Hire Date: " << info.get_Hire_Date() <<
endl;
cout << "Employee Shift: " << info.get_Shift() << endl;
cout << "Employee's Hourly Pay Rate: $" <<
info.Total_Salary() << endl;
return 0;
}

Output:

Object Oriented Programming Page 5


Task#3
Consider the following classes:
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:
1. void display(); // to displays the data
2. get() function // to accesses the data members
3. void set(); // to sets the values of data members
Define a class DateandTime from above two classes which displays both date and time.
i. Define an instance object of class DateTime called Watch.
ii. 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.
iii. 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.

Task Description:
In this task we simply take a class name as date in which we take date, month and year then get it
from user and displaying. Same take another base class name as time in which we take hours,
minutes and seconds then get it from user and displaying. Then we make a derived class using
multiple inheritance to inherit two base classes in one derived class so we can use both base
classes from one derived class with help of object. Then in main we make a menu for user to
choose one from menu either you want to reset value that is already initialize or he want to set his
own value later you can end it.

Code:
#include <iostream>
#include <string.h>
using namespace std;
class Date //Declaration of base class Date
{
private:
int date,month,year;
public:
Date(int d,int m,int y)
{
date=d; month=m; year=y;

Object Oriented Programming Page 6


}
void get()
{
cout<<"Enter Date: ";cin>>date;
cout<<"Enter Month: ";cin>>month;
cout<<"Enter Year: ";cin>>year;
}
void set_data()
{
date=0; month=0; year=0;
}
void display()
{
cout<<date<<"/"<<month<<"/"<<year<<endl;
}
};
class Time //Declaration of base class Date
{
private:
int hours,minutes,seconds;
public:
Time(int h,int m,int s)
{
hours=h; minutes=m; seconds=s;
}
void get()
{
cout<<"Enter Hours: ";cin>>hours;
cout<<"Enter Minutes: ";cin>>minutes;
cout<<"Enter Seconds: ";cin>>seconds;
}
void display()
{
if(hours<12)
{
cout<<hours<<":"<<minutes<<":"<<seconds<<"
AM"<<endl;
}
else
{
cout<<hours<<":"<<minutes<<":"<<seconds<<"
PM"<<endl;
}
}
void set_data()
{
hours=0; minutes=0; seconds=0;
}
};
class DateTime:public Date,public Time //Declaration of
derived class

Object Oriented Programming Page 7


{
public:
DateTime(int u,int v,int w,int x,int y,int
z):Date(u,v,w),Time(x,y,z)
{ }
void get()
{
Date::get(); Time::get();
}
void display()
{
Date::display(); Time::display();
}
void set_data()
{
Date::set_data(); Time::set_data();
}
};
int main()
{
DateTime watch(24,5,1998,12,45,56); //declaration and
initialization of derived class object
watch.display(); //Display function
char choice;
do
{
cout<<"1.Enter R to reset to 0"<<endl;
cout<<"2.Enter S to reset to some value"<<endl;
cout<<"3.Enter E to exit"<<endl;
cout<<"Enter choice: ";cin>>choice;
switch(choice)
{
case 'R':
watch.set_data();
watch.display();
break;
case 'S':
watch.get();
watch.display();
break;
}
}
while(choice!='E');
}

Output:

Object Oriented Programming Page 8


4. Post-Lab Tasks:
1.4. 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.
(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.
(v) Write a test program to test the classes.

Task Description:
In this task we simply take a class employee in which we take name and employee number
from user and then displaying it. After that we make derived class regular to show salary of
regular employee. Then we use multi-level inheritance to derived a class from already
derived class.then calling it from main.

Code:
#include <iostream>
#include <string.h>
using namespace std;
class Employee //Declaration of base class Employee
{
private:

Object Oriented Programming Page 9


string name; int emp_no;
public:
Employee()
{
name=""; emp_no=0;
}
void set_data()
{
cout<<"Enter name: ";cin>>name;
cout<<"Enter employee number: ";cin>>emp_no;
}
void show_data()
{
cout<<"Name: "<<name<<endl;
cout<<"Employee number: "<<emp_no<<endl;
}
void Salary() //member function for salary as mentioned
{ }
};
class Regular :public Employee//declaration of derived class
{
private:
float basic;
public:
Regular(float basic)
{
this->basic=basic;
}
float salary()
{
float DA=basic*0.1;
float HRA=basic*0.3;
return (DA+HRA+basic);
}
void show_data()
{
Employee::show_data();
cout<<"Salary of Regular employee:
"<<salary()<<endl;
}
};
class Adhoc:public Employee //declaration of derived class
{
private:
int number; float wage;
public:
Adhoc(float wage)
{
this->wage=wage;
}

Object Oriented Programming Page 10


float salary()
{
return (number*wage);
}
void days(int n)
{
this->number=n;
}
void show_data()
{
Employee::show_data();
cout<<"Salary of Adhoc employee:
"<<salary()<<endl;
cout<<"Number of Adhoc employee: "<<number<<endl;
}
};
int main()
{
Regular R_emp(35000);
Adhoc A_emp(1000);
cout<<"Enter data for Regular Employee!\
n";R_emp.set_data();
cout<<"Enter data for Adhoc Employee!\
n";A_emp.set_data();
A_emp.days(5);
cout<<"Data of Regular Employee!\n";R_emp.show_data();
cout<<"Data of Adhoc Employee!\n";A_emp.show_data();
return 0;
}
Output:

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 the country code. Test these classes from main() by creating

Object Oriented Programming Page 11


objects of derived classes and testing functions in a way that clear concept of multi-level
Inheritance.

Task Description:
In this task we simply take a class name as local phone in which we are taking phone number
from user and displaying. After that we make derived class NATphone to show city code.
Then we use multi-level inheritance to derived a class from already derived class.then calling
it from main.

Code:
#include <iostream>
using namespace std;
class Localphone
{
private:
double phone;
public:
void get_data()
{
cout<<"Enter Phone Number: ";cin>>phone;
}
void show_data()
{
cout<<"Phone Number: "<<phone<<endl;
}
};
class NATphone:public Localphone //NATphone class derived fom
Localphone class
{
private:
int city_code;
public:
void get_data()
{
Localphone::get_data();
cout<<"Enter City Code: ";cin>>city_code;
}
void show_data()
{
Localphone::show_data();
cout<<"City Code: "<<city_code<<endl;
}
};
class Intphone:public NATphone//Intphone class derived from
NATclass
{
private:
int country_code;
public:
void get_data()

Object Oriented Programming Page 12


{
NATphone::get_data();
cout<<"Enter Country Code: ";cin>>country_code;
}
void show_data()
{
NATphone::show_data();
cout<<"Country Code: "<<country_code<<endl;
}
};
int main()
{
Localphone obj1;
NATphone obj2;
Intphone obj3;
cout<<"Enter data for LocalPhone class!\
n";obj1.get_data();
cout<<"Enter data for NATphone cLass!\n";obj2.get_data();
cout<<"Enter data for Intphone class\n";obj3.get_data();
cout<<"Data entered for LocalPhone class!\
n";obj1.show_data();
cout<<"Data entered for NATphone class!\
n";obj2.show_data();
cout<<"Data entered for Intphone class!\
n";obj3.show_data();
}
Output:

Conclusion:
After completing this program we came to how multi-level and multiple inheritance is used
and how this help us to maintain whole program.

Object Oriented Programming Page 13

You might also like