OOP Lab Report-8
OOP Lab Report-8
OOP Lab Report-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.
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()
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;
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;
Output:
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;
Output:
(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:
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
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()
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.