Вы находитесь на странице: 1из 4

Riphah International University

I-14 Main Campus


Faculty of Computing

Class: Fall-2019 Subject: OOP


Course Code: CS2104 Class/Lab Instructor: Awais Nawaz

-------------------- LAB 08 --------------------


Inheritance
Learning Objective:
After the completion of this lab student will be able to:
1. What is Inheritance?
2. What is multilevel & multiple inheritance?
Example:
Header Files:
#include "iostream"
#include "string"

using namespace std;

class studentInfo
{
public:
string name;
int age;
string gender;

public:
void getInfo();
void putInfo();
};

class stdResult : public studentInfo


{
public:
int totalM;
double percentage;
string Grade;
public:

void getResult();
void putResult();
void res();
};

Source Code:
#include "Header.h"
#include "iostream"
#include "string"

void studentInfo::getInfo()
{
cout << "Enter Student Basic Information" << endl;
cout << "Enter Name" << endl;
getline(cin,name);
cout << "Enter Age" << endl;
cin >> age;
cout << "Enter Gender" << endl;
cin.ignore();
getline(cin, gender);
}

void studentInfo::putInfo()
{
cout << "Name\t" << name << endl;
cout << "Age\t" << age << endl;
cout << "Gender\t" << gender << endl;
}

void stdResult::getResult()
{
cout << "Enter Student Marks \t" << endl;
cin >> totalM;
percentage = (totalM * 100) / 500;
cout << "Student Percentage \t" << percentage<<endl;
cout << "Enter Student Grade \t" << endl;
cin.ignore();
getline(cin, Grade);
cout << endl << endl << endl << endl << endl << endl;
}

void stdResult::putResult()
{
cout << "Student Marks \t "<<totalM<<endl;
cout << "Student Percentage \t" << percentage << endl;
cout << "Student Grade \t" << Grade << endl;
}

void stdResult::res()
{
cout << "Student Name \t " << name << endl;
cout << "Student Age \t " << age << endl;
cout << "Student Gender \t " << gender << endl;
cout << "Student Marks \t " << totalM << endl;
cout << "Student Percentage \t" << percentage << endl;
cout << "Student Grade \t" << Grade << endl;
}

Main Code:
#include "Header.h"
#include "iostream"
#include "string"

using namespace std;

int main()
{
stdResult std;
std.getInfo();
std.getResult();
std.res();

return 0;
}
Lab Task 1:
Derive a class called Employee2 from the Employee class in the program. This new class
should add a type double data item called compensation, and also an enum type called
period to indicate whether the employee is paid hourly, weekly, or monthly. For simplicity
you can change the manager, scientist, and labourer classes so they are derived from
employee2 instead of employee. However, note that in many circumstances it might be
more in the spirit of OOP to create a separate base class called compensation and three
new classes manager, scientist, and labourer, and use multiple inheritance to derive these
three classes from the original manager, scientist, and labourer classes and from
compensation. This way none of the original classes needs to be modified.

Lab Task 2:
Design a Ship class...A print function that displays the ship’s name and the year it was built.
Design a CruiseShip class that is derived from the Ship class. A print function that overrides
the print function in the base class. CruiseShip class’s print function should display only the
ship’s name and maximum # passengers. Design a CargoShip class that is derived from the
Ship class...A print function that overrides the print function in the base class. CargoShip
class’s print function displays only ship’s name and ship’s cargo capacity. Create two
objects of each type. The first object (for each type) to be created using the constructor
that takes values for all of the data members. The second object must be created using the
default constructor and mutators methods must be used to set the values of the data
members. The program then should call each object’s print function to display the data
stored in them.

Вам также может понравиться