0% found this document useful (0 votes)
29 views15 pages

Oop Lab 9

The document outlines a lab journal for Object Oriented Programming, detailing three exercises involving the creation of abstract classes and derived classes for faculty and flight management systems. It includes code implementations for calculating salaries of permanent and visiting faculty and managing flight details for domestic and international flights. Each exercise is designed to reinforce concepts of inheritance, polymorphism, and dynamic memory allocation in C++.

Uploaded by

4nkf2h6m2n
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)
29 views15 pages

Oop Lab 9

The document outlines a lab journal for Object Oriented Programming, detailing three exercises involving the creation of abstract classes and derived classes for faculty and flight management systems. It includes code implementations for calculating salaries of permanent and visiting faculty and managing flight details for domestic and international flights. Each exercise is designed to reinforce concepts of inheritance, polymorphism, and dynamic memory allocation in C++.

Uploaded by

4nkf2h6m2n
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/ 15

Oop lab 9

Muhammad Junaid

01-135211-053

Zarmeen Fatima

01-135211-086

Eman Shoukat

01-135202-025

Bilal Ahmad

01-135202-022
Lab Journal – Lab 9

Exercise 1

Create an abstract class Faculty with fields name and ID.


Provide a pure virtual function salary().Give zero
argument and parameterized constructors.
Derive a class Permanent Faculty from Faculty. The class
has additional attributes years of service and basic pay.
Give zero argument and parameterized constructors. The
salary of permanent faculty is computed as the sum of
basic pay, medical allowance and house rent. Medical
allowance is 10% of basic pay and house rent is 25% of
the basic pay.

Derive another class Visiting Faculty from Faculty. The


class has attributes per hour rate and number of hours
taught. Give zero argument and parameterized
constructors. The salary of visiting faculty is computed by
multiplying the per hour rate with the number of hours
taught.
Write a program to declare two pointers of class Faculty.
Create an object each of visiting and permanent faculty,
assign their addresses to pointers of base class, set the
values of data members and call the salary function for
each.

Code :

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class faculty
{
protected:
string name;
int id;
public:
faculty();
faculty(string n, int i);
virtual void display();
virtual double salary() = 0;
};

Object Oriented Programming Page 1


Lab Journal – Lab 9

class permanent_faculty :public faculty


{
private:
int service_year;
int basic_pay;
public:
permanent_faculty();
permanent_faculty(int, int, string, int);
double salary();
void display();
};
class visiting_faculty :public faculty
{
private:
int hour_rate;
int hour_taught;
public:
visiting_faculty();
visiting_faculty(int, int, string, int);
double salary();
void display();
};
faculty::faculty()
{
name = " ";
id = 0;
}
faculty::faculty(string n, int i)
{
name = n;
id = i;
}
void faculty::display()
{
cout << "NAME :" << name << endl;
cout << "id :" << id << endl;
}
permanent_faculty::permanent_faculty()
{
service_year = 0;
basic_pay = 0;
}
permanent_faculty::permanent_faculty(int s, int b, string n, int i) : faculty(n,
i)
{
service_year = s;
basic_pay = b;
}
double permanent_faculty::salary()
{
double totalpay = basic_pay + (basic_pay*0.1) + (basic_pay*0.25);
return totalpay;
}
void permanent_faculty::display()
{
faculty::display();
cout << "service period is :" << service_year << endl;
cout << "basic pay is :" << basic_pay << endl;

Object Oriented Programming Page 2


Lab Journal – Lab 9

}
visiting_faculty::visiting_faculty()
{
hour_rate = 0;
hour_taught = 0;
}
visiting_faculty::visiting_faculty(int r, int t, string n, int i) :faculty(n, i)
{
hour_rate = r;
hour_taught = t;
}
double visiting_faculty::salary()
{
int total = hour_rate*hour_taught;
return total;
}
void visiting_faculty::display()
{
faculty::display();
cout << "hourly rate :" << hour_rate << endl;
cout << "No of hour taught :" << hour_taught << endl;
}
int main()
{
faculty *ptr1, *ptr2;
permanent_faculty p(5, 55000, "Junaid ", 2018);
visiting_faculty v(4000, 3, "Rafay", 2019);
ptr1 = &p;
ptr2 = &v;
ptr1->display();
cout << "salary of permanent faculty :" << ptr1->salary() << endl;
cout << endl << endl;
ptr2->display();
cout << "salary of visiting faculty :" << ptr2->salary() << endl;
_getch();
return 0;
}

Output:

Object Oriented Programming Page 3


Lab Journal – Lab 9

Exercise 2

Modify the above program to declare an array of pointers to Faculty.


Using dynamic memory allocation, create an object of permanent or
visiting faculty as indicated by the user (Get user choice).
Once the user has entered data for faculty, call the salary method for
each object and display the salary.

Code :

#include<conio.h>
#include<iostream>
#include<string>
using namespace std;

class faculty
{

Object Oriented Programming Page 4


Lab Journal – Lab 9

protected:
string name;
int ID;
public:
faculty()
{
name = " ";
ID = 0;
}
faculty(string n, int id)
{
name = n;
ID = id;
}
virtual void salary() = 0 {}

};

class permanentfaculty :public faculty


{
protected:
int YearOService;
double basicpay;
public:
permanentfaculty()
{
YearOService = 0;
basicpay = 0;
}
permanentfaculty(string n, int id, int yos, double bp)

{
name = n;
ID = id;
YearOService = yos;
basicpay = bp;
}
void salary()
{
double salary;
double medicalallow;
double houserent;

medicalallow = (10 / 100) * basicpay;


houserent = (25 / 100) * basicpay;
salary = houserent + medicalallow + basicpay;
cout << "Your Salary is " << salary;
}
};
class visitingfaculty :public faculty
{
protected:
double phrate;
double nht;
public:

Object Oriented Programming Page 5


Lab Journal – Lab 9

visitingfaculty()
{
name = " ";
ID = 0;
phrate = 0;
nht = 0;

}
visitingfaculty(string n, int id, int rate, int NHT)
{
name = " ";
ID = 0;
phrate = rate;
nht = NHT;

}
void salary()
{
double salary;

salary = phrate * nht;


cout << endl << "The Salary is " << salary;

};
int main()
{
int k = 0;
faculty* fptr1;
faculty* fptr2;
permanentfaculty pf(" Hassaan ", 561, 7, 75000);
visitingfaculty vf(" Hadid ", 725, 6500, 2);
fptr1 = &pf;
fptr2 = &vf;
fptr1->salary();
fptr2->salary();
faculty* arr[50];
char choice;
int l;
string n;
int i;
double h;
double r;
int yos;
double pay;
cout << "Enter Type of Faculy ";
cin >> choice;
do
{
cout << "Enter your name ";
cin >> n;
cout << "Enter your ID ";
cin >> i;
cout << "Enter Years of service ";
cin >> yos;
cout << "Enter Basic Pay ";

Object Oriented Programming Page 6


Lab Journal – Lab 9

cin >> pay;


double rate;
int hourstaught;
cout << " Enter rate per hour ";
cin >> rate;
cout << "No. hours taught ";
cin >> hourstaught;

if (choice == 'p')
{
arr[k] = new permanentfaculty(n, i, yos, pay);
}
else
{
arr[k] = new visitingfaculty(n, i, rate, hourstaught);

}
k++;

cout << " Do you want to continue?";


cin >> choice;
} while (choice == 'y');
for (int l = 0; l < k; l++) {
arr[l]->salary();
}
_getch();
}

Output :

Object Oriented Programming Page 7


Lab Journal – Lab 9

Exercise 3

Create a class date with data members day, month and year. Class
should have constructors, setDate and display function.
Create a class time with data members hour, minute and second. Class
should have constructors, setTime and display function.
Create a class Flight having information on departure and arrival dates
and times, origin and destination. Date and time should be declared
using composition. The class should have constructors, a function to
input the values of data members and another function to display the
data members.
Derive classes Domestic flight and International flight from the class
Flight. The class Domestic Flight should have a data member to store
the allowed baggage limit while the class International Flight should
have a data member to store whether the Flight is direct or not. Provide
their constructors and override the input and display methods in the
derived classes.
In the main program, declare an array of 5 pointers to Flight, create
objects of Domestic and International Flights and call the input and

Object Oriented Programming Page 8


Lab Journal – Lab 9

display methods.

Code:

Flight.h:

#pragma once

#include<iostream>

#include<conio.h>

#include<string>

using namespace std;

class flight

string departure;

string arrival;

string origin;

string destination;

public:

virtual void input();

virtual void display();

};

class domestic_flight :public flight

Object Oriented Programming Page 9


Lab Journal – Lab 9

int baggage_limit;

public:

void input();

void display();

};

class international_flight :public domestic_flight

string direct;

public:

void input();

void display();

};

Flight.cpp:

#include "flight.h"

void flight::input()

cout << "enter departure time and date :";

getline(cin, departure);

cout << "enter arrival time and date :";

getline(cin, arrival);

Object Oriented Programming Page 10


Lab Journal – Lab 9

cout << "enter origin :";

getline(cin, origin);

cout << "enter destination :";

getline(cin, destination);

void flight::display()

cout << "departure date and time :" << departure << endl;

cout << "arrival time and date :" << arrival << endl;

cout << "origin :" << origin << endl;

cout << "destination :" << destination << endl;

void domestic_flight::input()

cout << "\t\t\t**enter domestic flight details\t\t\t" << endl;

flight::input();

cout << "enter baggage limit : ";

cin >> baggage_limit;

Object Oriented Programming Page 11


Lab Journal – Lab 9

void domestic_flight::display()

flight::display();

cout << "Allowed baggage limit is :" << baggage_limit << endl;

void international_flight::input()

cout << "\t\t\t**enter international flight details\t\t\t" << endl;

cin.ignore();

flight::input();

cout << "enter flight direct? y/n :";

getline(cin, direct);

void international_flight::display()

flight::display();

cout << "flight status direct?? :" << direct << endl;

Object Oriented Programming Page 12


Lab Journal – Lab 9

Source.cpp:

#include "flight.h"

int main()

flight *ptr[2];

domestic_flight df;

international_flight interf;

ptr[0] = &df;

ptr[1] = &interf;

for (int i = 0; i < 2; i++)

ptr[i]->input();

cout << endl << endl << endl;

ptr[i]->display();

cout << endl;

_getch();

return 0;

Object Oriented Programming Page 13


Lab Journal – Lab 9

Program the aforementioned exercises and get them checked by your instructor.

S No. Exercise Checked By:


1. Exercise 1
2. Exercise 2
3. Exercise 3

+++++++++++++++++++++++++

Object Oriented Programming Page 14

You might also like