0% found this document useful (0 votes)
73 views14 pages

OOP Lab Report-6

The document describes three tasks related to inheritance in object-oriented programming. Task 1 involves creating classes for publications, books, and tapes that inherit from a base publication class. Task 2 adds a disk class that also inherits from publication. Task 3 creates classes for person data and customer data, with customer data inheriting from person data and adding additional fields. Code examples are provided for each task that demonstrate creating objects and using inherited methods.

Uploaded by

Talha Tufail
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)
73 views14 pages

OOP Lab Report-6

The document describes three tasks related to inheritance in object-oriented programming. Task 1 involves creating classes for publications, books, and tapes that inherit from a base publication class. Task 2 adds a disk class that also inherits from publication. Task 3 creates classes for person data and customer data, with customer data inheriting from person data and adding additional fields. Code examples are provided for each task that demonstrate creating objects and using inherited methods.

Uploaded by

Talha Tufail
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/ 14

Lab#6

Lab Title
Inheritance
1. Objectives:
In this lab we will came to know the concepts and terminologies of inheritance in Object
Oriented Programming.

2. Introduction:
In Object-oriented programming, Inheritance is a way to create a new class by existing class
and adding new members. It allows new class or derived class to link with based class. The
new class can replaced or expend the functionality of based class or existing. Existing class is
base class and new class is derived class. As from name you can inherit base class from
derived class except constructor and destructor.

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


1.3. Task#1
Imagine a publishing company that markets both book and audiocassette versions of its
works. Create a class publication that stores the title (a string) and price (type float) of a
publication. From this class derive two classes: book, which adds a page count (type int), and
tape, which adds a playing time in minutes (type float). Each of these three classes should
have a getdata() function to get its data from the user at the keyboard, and a putdata()
function to display its data.Write a main() program to test the book and tape classes by
creating instances of them, asking the user to fill in data with getdata() , and then displaying
the data with putdata() .

Task Description:
In this task, we simply take a class name as publication in which we take string type title
and float type price then we make get function and put function to get value from user and
put to display it. After that we make two more class one for book count and other for tape
time in which we inherit the get and put function. So now we have two derived classes and
one based class. Calling all function from main with help of object of derived classes.

Code:
#include <iostream>
#include <string>
using namespace std;
class publication
{
private: // stores the title (a string) and price (type float)
of a publication.
string title;
float price;

Object Oriented Programming Page 1


public:
void get_data()
{
cout << "Enter a title of Publication: ";
cin >> title;
cout << "Enter a price of Publication: ";
cin >> price;
}
void put_data() //putdata() function to display its data.
{
cout << "The Publication title: " << title << endl;
cout << "The Publication price: " << price << endl;
}
};
class book :public publication // book, which adds a page
count (type int),
{
private:
int page_count;
public:
void get_data()
{
publication::get_data(); //call publication class
function to get data
cout << "Enter Book Page Count: "; //Acquire book
data from user
cin >> page_count;
}
void put_data()
{
publication::put_data(); //Show Publication data
cout << "Book Page Count: " << page_count <<
endl; //Show book data
}
};
class tape :public publication
{
private:// and tape, which adds a playing time in minutes
(type float)
float playing_time;
public:
void get_data()
{
publication::get_data();

Object Oriented Programming Page 2


cout << "Enter tape playing time: ";
cin >> playing_time;
}
void put_data()
{
publication::put_data();
cout << "Tape's playing time: " << playing_time <<
endl;
}
};
int main()
{
book newBook;
tape newTape;
newBook.get_data();
newTape.get_data();
newBook.put_data();
newTape.put_data();
}
Output: (Before Modifying)

Task#2
Assume that the publisher in task 1 decides to add a third way to distribute books: on
computer disk, for those who like to do their reading on their laptop. Add a disk class that,
like book and tape, is derived from publication. The disk class should incorporate the same
member functions as the other classes. The data item unique to this class is the disk type:
either CD or DVD. You can use an enum type to store this item. The user could select the
appropriate type by typing c or d.

Task Description:
In this task, we simply take a class name publication in which we take string type title and
float type price then we make get function and put function to get value from user and put to
display it. After that we make three more class one for book count, second for tape time and
other for disk type in which we inherit the get and put function. So now we have three

Object Oriented Programming Page 3


derived classes and one based class. Calling all function from main with help of object of
derived classes.

Code:
#include <iostream>
#include <string>
using namespace std;
class publication
{
private: // stores the title (a string) and price (type
float) of a publication.
string title;
float price;
public:
void get_data()
{
cout << "Enter a title of Publication: ";
cin >> title;
cout << "Enter a price of Publication: ";
cin >> price;
}
void put_data() //putdata() function to display its data.
{
cout << "The Publication title: " << title << endl;
cout << "The Publication price: " << price << endl;
}
};
class book :public publication // book, which adds a page
count (type int),
{
private:
int page_count;
public:
void get_data()
{
publication::get_data(); //call publication class
function to get data
cout << "Enter Book Page Count: "; //Acquire book
data from user
cin >> page_count;
}
void put_data()
{
publication::put_data(); //Show Publication data

Object Oriented Programming Page 4


cout << "Book Page Count: " << page_count <<
endl; //Show book data
}
};
class tape :public publication
{
private:// and tape, which adds a playing time in minutes
(type float)
float playing_time;
public:
void get_data()
{
publication::get_data();
cout << "Enter tape playing time: ";
cin >> playing_time;
}
void put_data()
{
publication::put_data();
cout << "Tape's playing time: " << playing_time <<
endl;
}
};
class disk :public publication
{
private:
enum disk_type{CD,DVD};
disk_type chose;
public:
void get_data()
{
char x;
publication::get_data();
cout << "Enter disk type (c or d) for CD and DVD: ";
cin >> x;
if (x == 'c')
chose = CD;
else
chose = DVD;
}
void put_data()
{
publication::put_data();
cout << "Disk type is: ";

Object Oriented Programming Page 5


if (chose == CD)
cout << "CD";
else
cout << "DVD";
}
};
int main()
{
book newBook;
tape newTape;
disk newdisk;
newBook.get_data();
newTape.get_data();
newdisk.get_data();
newBook.put_data();
newTape.put_data();
newdisk.put_data();
}
Output:

Task#3
Design a class namedPersonData with the following member variables:
 lastName
 firstName
 address
 city
 state
 zip
 phone
Write the appropriate accessor and mutator functions for these member variables.
Next, design a class named CustomerData, which is derived from the PersonData
class. The CustomerData class should have the following member variables:

Object Oriented Programming Page 6


 customerNumber
 mailingList

The customerNumber variable will be used to hold a unique integer for each customer.
The mailingList variable should be a bool. It will be set to true if the customer
wishes to be on a mailing list, or false if the customer does not wish to be on a mailing list.
Write appropriate accessor and mutator functions for these member variables.
Demonstrate an object of the CustomerData class in a simple program.

Task Description:
In this task, we simply take a class name as person data in which we take string type
variables 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 customer number and mailing list. Calling
all function from main with help of object of derived classes.

Code:
#include <iostream>
#include <string>
using namespace std;
class Person_Data //Base class
{
private:
string lastName;
string firstName;
string address;
string city;
string state;
string zip;
string phone;
public:
string get_data_lastName() Mutator function
{
return lastName;
}
string get_data_firstName()
{
return firstName;
}
string get_data_address()
{
return address;
}
string get_data_city()
{
return city;

Object Oriented Programming Page 7


}
string get_data_state()
{
return state;
}
string get_data_zip()
{
return zip;
}
string get_data_phone()
{
return phone;
}
void set_Lastname(string newlastName) //Accessor function
{
lastName = newlastName;
}
void set_Firstname(string newfirstName)
{
firstName = newfirstName;
}
void set_Address(string newaddress)
{
address = newaddress;
}
void set_City(string newcity)
{
city = newcity;
}
void set_State(string newstate)
{
state = newstate;
}
void set_Zip(string newzip)
{
zip = newzip;
}
void set_Phone(string newphone)
{
phone = newphone;
}
};
class Customer_data : public Person_Data //Derived class from
person data

Object Oriented Programming Page 8


{
private:
string customerNumber;
bool mailingList;
public:
string get_data_customerNumber() //Mutator
function
{
return customerNumber;
}
bool get_data_mailingList()
{
if(mailingList==true)
return 1;
else
return 0;
}
void set_Customernumber(string newcustomerNumber)
//Accessor function
{
customerNumber = newcustomerNumber;
}
int set_Mailinglist(bool newmailingList)
{
mailingList=newmailingList;
}
};
int main()
{
Customer_data info; //Declaration of object of derive
class
string ltn;
string ftn;
string ad;
string ct;
string st;
string zp;
string pn;
string cmn;
bool ml;
cout << "Last Name: ";
cin >> ltn;
cout << "First Name: ";
cin >> ftn;

Object Oriented Programming Page 9


cout << "Address: ";
cin >> ad;
cout << "City: ";
cin >> ct;
cout << "State: ";
cin >> st;
cout << "Zip Code: ";
cin >> zp;
cout << "Phone: ";
cin >> pn;
cout << "Customer Number: ";
cin >> cmn;
cout << "Do You want to see Mialing List(true/false): ";
cin >> ml;
info.set_Lastname(ltn);
info.set_Firstname(ftn);
info.set_Address(ad);
info.set_City(ct);
info.set_State(st);
info.set_Zip(zp);
info.set_Phone(pn);
info.set_Customernumber(cmn);
info.set_Mailinglist(ml);
cout << "Customer last Name: " <<
info.get_data_lastName() << endl;
cout << "Customer first Name: " <<
info.get_data_firstName() << endl;
cout << "Customer address: " << info.get_data_address()
<< endl;
cout << "Customer city: " << info.get_data_city() <<
endl;
cout << "Customer state: " << info.get_data_state() <<
endl;
cout << "Customer zip code: " << info.get_data_zip() <<
endl;
cout << "Customer phone: " << info.get_data_phone() <<
endl;
cout << "Customer number: " <<
info.get_data_customerNumber() << endl;
cout << "Customer mailing list: " <<
info.get_data_mailingList() << endl;
return 0;
}

Object Oriented Programming Page 10


Output:

4. Post-Lab Tasks:
1.4. Task#1
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. 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.
Hint: use to_string function for converting numeric date to string.

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. Calling all function from main with help of
object of derived classes.

Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee //Base class
{
private:
string Emp_Name;

Object Oriented Programming Page 11


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;
}

Object Oriented Programming Page 12


int get_Shift() const //Mutator function
{
return Shift;
}
double get_Hourly_Pay_Rate() const
{
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 << setprecision(2) << fixed;
cout << "Employee's Hourly Pay Rate: $" <<
info.get_Hourly_Pay_Rate() << endl;
return 0;
}

Object Oriented Programming Page 13


Output:

Conclusion:
After completing this program we came to how inheritance is done between based and derive
class and use of accessor and mutator.

Object Oriented Programming Page 14

You might also like