Docsity

Object-Oriented Programming Lab: Polymorphism in C++, Lab Reports of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming Lab: Polymorphism in C++ and more Lab Reports Object Oriented Programming in PDF only on Docsity!

Department of Electrical Engineering

CS212: Object Oriented Programming

Class: BSCS10 - Section A&B

Lab 12: Polymorphism

Date: 24 May, 2021

Instructor: Dr Shams Qazi

Lab Engineer: Ms Shakeela BiBi

NAME: AZKA KHAN

CMS ID: 297896

SECTION A

CODE: #include #include using namespace std; class person { protected: string name; public: person() { name = ""; } ~person() { cout << "\n\n\t\t\t\t=> Destructor of class person was called!" << endl; } void getName() { cout << "\t\t\t\t=> Enter Name: "; cin.ignore(); getline(cin, name); } //********Virtual Functions***********// //Declaring Function as virtual so that we can access getData and print functions of derived class using person's class pointer! virtual void getData() { getName(); } virtual void print() { cout << "\t\t\t\t=> Name: "<<endl; } }; class Student : public person { protected: float GPA; public: Student() { GPA = 0.0; } ~Student() { cout<<"\n\n\t\t\t\t => Destructor of class Student was called!" << endl; } //Polymorphic Function void getData() { getName(); cout << "\t\t\t\t=> Enter student's GPA: "; cin >> GPA;

if (GPA < 0 || GPA>4) { cout << "\t\t\t\t => Invalid GPA!" << endl; cout << "\t\t\t\t=> Enter student's GPA: "; cin >> GPA; } } //Polymorphic Function void print() { cout << "\t\t\t=> Student's Name: " << name << endl; cout << "\t\t\t=> Student's GPA: " << GPA << endl; } }; class Professor : public person { protected: int NumPubl; public: Professor() { NumPubl = 0; } ~Professor() { cout << "\n\n\t\t\t\t => Destructor of class Professor was called!" << endl; } //Polymorphic Function void getData() { getName(); cout << "\t\t\t\t=> Enter number of professor's publications: "; cin >> NumPubl; } //Polymorphic Function void print() { cout << "\t\t\t=> Professor's Name: " << name << endl; cout << "\t\t\t=> Professor's Number of Publication: "<<NumPubl <<endl; } }; void main() { int p; person* personPtr[50]; int n_s, n_p;//n_p=Number of students, n_s=number of Professor cout << "\t\t
t------------------------------------------------------------------"; cout << "\n\t\t\t| |"; cout << "\n\t\t\t| Lab 12 |"; cout << "\n\t\t\t| |"; cout << "\n\t\t
t------------------------------------------------------------------\n\n\n"; cout << "\n\t\t\t\tEnter the number of students you want to input: "; cin >> n_s;