OOP Lab Report-6
OOP Lab Report-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.
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;
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
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
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:
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;
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;
public:
void set_Shift(int a) //Accessor function
{
Shift = a;
}
void set_Hourly_Pay_Rate(double b)
{
Hourly_Pay_Rate = b;
}
Conclusion:
After completing this program we came to how inheritance is done between based and derive
class and use of accessor and mutator.