Object Oriented Programming Lab: Inheritance and Function Overriding, Lab Reports of Object Oriented Programming

OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.

Typology: Lab Reports

2020/2021

Uploaded on 05/29/2021

Sanakhan9973
Sanakhan9973 🇵🇰

2 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Department of Computing
CS212: Object Oriented Programming
Class: BSCS10 - Section A&B
Lab 11: Inheritance and Function Overriding
Date: May 17, 2021
Instructor: Dr Shams Qazi
Lab Engineer: Ms Shakeela BiBi
NAME: AZKA KHAN
CMS ID: 297896
SECTION A
CS212: Object Oriented Programming Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Object Oriented Programming Lab: Inheritance and Function Overriding and more Lab Reports Object Oriented Programming in PDF only on Docsity!

Department of Computing

CS212: Object Oriented Programming

Class: BSCS10 - Section A&B

Lab 11: Inheritance and Function Overriding

Date: May 17, 2021

Instructor: Dr Shams Qazi

Lab Engineer: Ms Shakeela BiBi

NAME: AZKA KHAN

CMS ID: 297896

SECTION A

Inheritance and Function Overriding

Task 1: Implement the following class hierarchy

1) Add appropriate data member in all the classes

a. Add name and age in both Teacher and Author classes

2) Add default and parameterized constructors and destructor in all the classes

3) Write appropriate overridden getter, setter functions

4) In main program create object of Author class and display all values inherited members from

base classes and its own member

TEACHER

SCHOLAR

AUTHOR

cout << "\t\t\t=> Default constructor for class SCHOLAR was called!" << endl; Number_of_Publications = 0; } //Parameterised Constructor SCHOLAR(int p) { cout << "\t\t\t=> Parameterised constructor for class SCHOLAR was called!" << endl; Number_of_Publications = p; } //Destructor ~SCHOLAR() { cout << "\t\t\t~SCHOLAR was called!" << endl; } //Getter and Setter Functions void set_NumberOfPublication(int P) { Number_of_Publications = P; } int get_NumberOfPublication() { return Number_of_Publications; } //Display Function void print() { cout << "Scholar's Name: " << getName() << endl; cout << "Scholar's Age: " << getAge() << endl; cout << "Scholar's Number of publications: " << Number_of_Publications << endl; } }; class AUTHOR : public SCHOLAR { public: //Data Members string A_name; int A_age; int Number_of_Books; //Default Constructor AUTHOR() { cout << "\t\t\t=> Default constructor for class AUTHOR was called!" << endl; A_name = ""; A_age = 0; Number_of_Books = 0; } //Parameterised Constructor AUTHOR(string n, int a, int p) :SCHOLAR(p)//Calling Parent's Class Parameterised constructor! { cout << "\t\t\t=> Parameterised constructor for class AUTHOR was called!" << endl; A_name = n; A_age = a; Number_of_Books = p; }

//Destructor ~AUTHOR() { cout << "\t\t\t~AUTHOR was called!" << endl; } //Getter and Setter Functions void setName(string N) { A_name = N; } void setAge(int A) { A_age = A; } void setBooks(int B) { Number_of_Books = B; } string getName() { return A_name; } int getAge() { return A_age; } //Display Function void print() { cout << "Author's Name: " << A_name << endl; cout << "Author's Age: " << A_age << endl; cout << "Author's Number of publications: " << Number_of_Publications << endl; cout << "Author's Number of Books: " << Number_of_Books << endl; } }; void main() { system("color B5"); cout << "\t\t
t------------------------------------------------------------------"; cout << "\n\t\t\t| |"; cout << "\n\t\t\t| TASK 1 |"; cout << "\n\t\t\t| |"; cout << "\n\t\t
t------------------------------------------------------------------\n\n"; AUTHOR A1("Azka Khan", 35, 5);//An object of class AUTHOR is instantiated cout << "________________________________________________________________________________ ________________________________________\n\n"; A1.TEACHER::setName("Azka Khan");//Overriding set function of class TEACHER was called! A1.TEACHER::setAge(35);//Overriding set function of class TEACHER was called!

Task 2: Modification & Extension

1) Add a common base class Person

2) Remove name and age data members from Teacher and Author classes and add these data

members in common base class Person

3) Also add constructor and destructor in Person class

4) Create object of Author class in main program. Display all data and see the behavior and order

for constructors and destructors. Explain why?

CODE

#include #include using namespace std; class PERSON{ //Data Members string name; int age; public: //Default Constructor PERSON() { cout << "\t\t\t=> Default constructor for class PERSON was called!" << endl; name = ""; age = 0; } PERSON(string n,int a) { cout << "\t\t\t=> Parameterised constructor for class AUTHOR was called!" << endl; name = n; age = a; } //Destructor ~PERSON() { cout << "\t\t\t~PERSON was called!\n"; } //Getter and Setter Functions void setName(string N) { name = N; } void setAge(int A) { age = A; } string getName() { return name; } int getAge() { return age; } }; class TEACHER:public PERSON {

//Data Member to store Professional experience int p_experience; public: //Default Constructor TEACHER() { cout << "\t\t\t=> Default constructor for class TEACHER was called!" << endl; p_experience = 0; } //Parameterised Constructor TEACHER(int exp) { cout << "\t\t\t=> Parameterised constructor for class TEACHER was called!" << endl; p_experience = exp; } //Destructor Definition ~TEACHER() { cout << "\t\t\t~TEACHER was called!" << endl; } void setExperience(int P) { p_experience = P; } int getExperience() { return p_experience; } //Display Function void print() { cout << "Teacher's Name: " << getName() << endl; cout << "Teacher's Age: " << getAge() << endl; cout << "Professional Experience: " << getExperience() << endl; } }; class AUTHOR: public PERSON{ //Data Members int Number_of_Books; //Data Member to store Professional experience int p_experience; public: //Default Constructor AUTHOR() { cout << "\t\t\t=> Default constructor for class AUTHOR was called!" << endl; Number_of_Books = 0; p_experience = 0; } //Parameterised Constructor AUTHOR(int p,int exp) { cout << "\t\t\t=> Parameterised constructor for class AUTHOR was called!" << endl; Number_of_Books = p;

OUTPUT

EXPLANATION:

As it can be seen on the output screen, the default constructor for class “PERSON” followed by

the parameterized constructor for class “AUTHOR” is called whereas, destructor for class

“AUTHOR” is called before the destructor for class “PERSON”. This happens as the order in

which constructors are called is dependent on the order of inheritance. However, destructors

follow a reverse order to that of constructor. Construction always starts with the base class i-e

constructors from all base class are invoked first and then the derived class constructor is

called. The destructor for a child class is called before the constructor for a parent class.

Destruction always starts with the derived class.

Task 3:_______________________________________________________________________________

Develop programs in C++ for following problem:

 Consider a class Item that stores the:

o Title of an item

o Price

o Array of three variables to only record the sales in Rupees of a particular item for the

last three months

o Has its own function getters and setters

 Another class HardwareItem stores:

o Title of an item

o Price

o Array of three variables to record the sales in Rupees of a particular item for the last

three months

o Equipment Manufacturer Name

o Has its own overridden getters and setters

 Another class SoftwareItem stores:

o Title of an item

o Price

o Array of three variables to record the sales in Rupees of a particular item for the last

three months

o Supported operating system name on which software item runs

o Has its own overridden getters and setters

Develop a program , using Object Oriented techniques(inheritance) and appropriate datatypes

to construct the necessary classes ensuring maximum code reuse. Don’t add code for

constructors.

The application must work for the following test function:

int main()

Item *it=new Item;

HardwareItem h1;

SoftwareItem *s= new SoftwareItem;

it->getData();

it->DisplayData();

h1.getData();

h1.DisplayData();

s->getData();

s->DisplayData();

delete h1;

setPrice(); setSales(); } void DisplayData() { cout << "\t\t
t------------------------------------------------------------------"; cout << "\n\t\t\t| |"; cout << "\n\t\t\t| Item's Description |"; cout << "\n\t\t\t| |"; cout << "\n\t\t
t------------------------------------------------------------------\n\n"; cout << "\t\t\t=> Item's Title: " << getName()<<endl; cout << "\t\t\t=> Item's Price: " << getPrice() <<" Rupees" << endl; cout << "\t\t\t=> Item's Sales for three Month: " << endl; for (int i = 0; i < 3; i++) { cout << "\t\t\t=> Month " << i + 1 << " : " << sales[i] << " Rupees" << endl; } cout << "\n\n"; } }; class HardwareItem : public Item { string manufacture; public: void setManufacture() { cout << "Enter the name of Item's Manufacture: "; cin.ignore(); getline(cin, manufacture); } string getManufacture() { return manufacture; } void getData() { setName(); setPrice(); setSales(); setManufacture(); } void DisplayData() { int* SALES = getSales(); cout << "\t\t
t------------------------------------------------------------------"; cout << "\n\t\t\t| |"; cout << "\n\t\t\t| Hardware Item |"; cout << "\n\t\t\t| |"; cout << "\n\t\t
t------------------------------------------------------------------\n\n";

cout << "\t\t\t=> Item's Title: " << getName()<<endl; cout << "\t\t\t=> Item's Price: " << getPrice()<<" Rupees"<<endl; cout << "\t\t\t=> Item's Manufacturer: " << getManufacture() << endl; cout << "\t\t\t=> Item's Sales for three Month: " << endl; for (int i = 0; i < 3; i++) { cout << "\t\t\t=> Month " << i + 1 << " : " << SALES[i] << " Rupees" << endl; } cout << "\n"; } }; class SoftwareItem : public Item { string OS_Name; public: void setOS_Name() { cout << "Enter Operating System's Name: "; cin.ignore(); getline(cin, OS_Name); } string getOS_Name() { return OS_Name; } void getData() { setName(); setPrice(); setSales(); setOS_Name(); cout << "\n"; } void DisplayData() { int* SALES = getSales(); cout << "\t\t
t------------------------------------------------------------------"; cout << "\n\t\t\t| |"; cout << "\n\t\t\t| Software Item |"; cout << "\n\t\t\t| |"; cout << "\n\t\t
t------------------------------------------------------------------\n\n"; cout << "\t\t\t=> Item's Title: " << getName() << endl; cout << "\t\t\t=> Item's Price: " << getPrice() << endl; cout << "\t\t\t=> Item's Operating System: " << getOS_Name() << endl; cout << "\t\t\t=> Item's Sales for three Month: " << endl; for (int i = 0; i < 3; i++) { cout << "\t\t\t=> Month " << i + 1 << " : " << SALES[i] << " Rupees" << endl; } }