Object-Oriented Programming Lab: Polymorphism in C++, 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 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
CS212: Object Oriented Programming Page 1
pf3
pf4
pf5

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;