0% found this document useful (0 votes)
8 views9 pages

OOP Lab 10

The document outlines Experiment No. 10 for the Electrical & Computer Engineering department at Air University, focusing on polymorphism and abstract classes in C++. It explains the concepts of polymorphism, types of polymorphism, and abstract classes, along with examples in C++ code. Additionally, it includes lab tasks that require students to apply these concepts in designing systems for payment processing, animal simulations, and drawing applications.

Uploaded by

240774
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)
8 views9 pages

OOP Lab 10

The document outlines Experiment No. 10 for the Electrical & Computer Engineering department at Air University, focusing on polymorphism and abstract classes in C++. It explains the concepts of polymorphism, types of polymorphism, and abstract classes, along with examples in C++ code. Additionally, it includes lab tasks that require students to apply these concepts in designing systems for payment processing, animal simulations, and drawing applications.

Uploaded by

240774
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/ 9

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL & COMPUTER ENGINEERING

EXPERIMENT NO. 10

Lab Title:

Student Name: Reg. No:

Objective:

LAB ASSESSMENT:

Attributes Excellent Good Average Satisfactory Unsatisfactory


(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows the
lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Attributes Excellent Good Average Satisfactory Unsatisfactory


(5) (4) (3) (2) (1)
Data Presentation
Experiment Results
Conclusion

Total Marks: Obtained Marks:

Date: Signature:
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

Lab 10
Items Description
Lab Course Title Lab – Object Oriented Programming
Lab Title Polymorphism & Abstract Class.
Objective learn to apply a core concept of OOP in C++ and how to implement
polymorphism using pointers and derived classes
Duration 3 Hours
Operating System/Tools Windows/MS Office/Dev C++/Computer System

Polymorphism:
The word polymorphism means having many forms. A real-life example of polymorphism is a person
who at the same time can have different characteristics. A man at the same time is a father, a husband,
and an employee. So, the same person exhibits different behaviour in different situations. This is called
polymorphism. In C++, polymorphism concept can be applied to functions and operators. A single
function can work differently in different situations. Similarly, an operator works different when used
in different context.
 Polymorphism allows you to create a pointer to a derived class which is also compatible with
the base class.

Types of Polymorphism
Polymorphism in C++ can be classified into two types:
 Compile-time Polymorphism
 Runtime Polymorphism

Abstract Class in C++


An abstract class in C++ is a class that cannot be instantiated directly. It serves as a base class and is
intended to be inherited. It contains at least one pure virtual function.
Pure Virtual Function:
A virtual function is made pure by assigning = 0 in its declaration: This tells the compiler that derived
classes must override this function.

1
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

Source Code Output


#include <iostream>
using namespace std;

class Shape {
public:
// Pure virtual function
virtual void draw() = 0;

void info() {
cout << "This is a shape." << endl;
}
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing a circle." << endl;
}
};

class Square : public Shape {


public:
void draw() override {
cout << "Drawing a square." << endl;
}
};

int main() {
// Shape s; // Error: Cannot instantiate abstract
class

Shape* shapePtr;

Circle c;
Square s;

shapePtr = &c;
shapePtr->draw(); // Output: Drawing a
circle.

shapePtr = &s;
shapePtr->draw(); // Output: Drawing a
square.

return 0;
}

 You cannot create objects of an abstract class.

2
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

 A class becomes abstract if at least one of its functions is pure virtual.


 Derived classes must override all pure virtual functions to become concrete (instantiable).
 Abstract classes are used to define interfaces or blueprints for derived classes.

Runtime Polymorphism:
Also known as late binding and dynamic polymorphism, the function call in runtime
polymorphism is resolved at runtime in contrast with compile time polymorphism, where the compiler
determines which function call to bind at compilation. Runtime polymorphism is implemented using
function overriding with virtual functions.

1. Function Overriding
Function Overriding occurs when a derived class defines one or more member functions of the base
class. That base function is said to be overridden. The base class function must be declared as virtual
function for runtime polymorphism to happen.

Example:
In the below example, a virtual function display() is defined in the base class Base, and it is overridden
in the derived class Derived. The Base class pointer basePtr points to an object of the Derived class.
When the display() function is called using the basePtr, the derived class version of
the display() function is called, and prints "Derived class function." This is possible because call is
resolved at the runtime.

Source Code Output


#include<iostream>
using namespace std;

class Base {
public:

// Virtual function
virtual void display() {
cout << "Base class function";
}
};

class Derived : public Base {


public:

// Overriding the base class function


void display() override {
cout << "Derived class function";
}
};

int main() {

// Creating a pointer of type Base


Base* basePtr;

// Creating an object of Derived class

3
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

Derived derivedObj;

// Pointing base class pointer to


// derived class object
basePtr = &derivedObj;

// Calling the display function


// using base class pointer
basePtr->display();
return 0;
}

2. Virtual Function:
A virtual function is a member function which is declared in the base class using the keyword
virtual and is redefined (Overriding) by the derived class. The term Polymorphism means the
ability to take many forms. It occurs if there is a hierarchy of classes which are all related to
each other by inheritance
Example:
This example demonstrates a polygon-rectangle hierarchy with the use of virtual functions in the base
class and how Rectangle objects are being casted into the Base class type through pointers.

#include<iostream> //library for c++


using namespace std;
class polygon //class declaration
{
private: //not accessible outside the class
double width; //variables declaration
double height;
public: //accessible outside the class
polygon(double w, double h) //two argument
constructor
{ width=w; height=h;
}
double getwidth() //function to return value of
width
{ return width;
}
double getheight() //function to return value of
height
{ return height;

}
virtual double getArea() //virtual function
{ return 0;
}
virtual string gettype() //virtual function
{ return "Generic Polygon";

}};
class rectangle: public polygon //derived class
{

4
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

public:
rectangle(double w, double h): polygon(w,h)
//two argument constructor
{

}
double getArea() //function to return value of
area
{
return (getwidth() * getheight());
}
string gettype() //type function for rectangle
{ return "rectangle";

}
};

int main()
{
polygon * p = new polygon(20,30); //pointer
declare and initialize for polygon
rectangle * r = new rectangle(20,30); //pointer
declare and initialize for rectangle
cout<<p->gettype()<<endl<<" Area : "<<p-
>getArea()<<endl; //call funtions
cout<<r->gettype()<<endl<<" Area : "<<r-
>getArea()<<endl;
cout<<endl<<endl;
}

Example:
This example demonstrates the polygon-rectangle hierarchy with pure virtual functions. The base class
now is an abstract base class and lots of other classes added to the hierarchy.
Source Code Output

5
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

#include<iostream> //library for c++


using namespace std;
class polygon //class declaration
{
private: //not accessible outside the class
double width; //variables declaration
double height;public: //accessible outside the
class
polygon(double w, double h) //two argument
constructor
{ width=w; height=h;
}
double getwidth() //function to return value of
width
{ return width; }
double getheight() //function to return value
of height
{ return height; }
virtual double getArea()=0; //virtual function
virtual string gettype()=0; //virtual function
};
class rectangle: public polygon //derived class
{
public: rectangle(double w, double h):
polygon(w,h) //two argument constructor
{}
double getArea() //function to return value of
area by multiplying width and height
{ return (getwidth() * getheight());

}
string gettype() //type function for rectangle
{ return "rectangle";

}};

class square : public rectangle //derive class


from rectangle
{
public: square(double l): rectangle(l,l) //one
argument constructor
{}
string gettype() //return type as square
{ return "square"; }};
class parallelogram : public rectangle //derive
class from rectangle
{
public:
parallelogram(double w, double h):
rectangle(w,h) //two argument constructor
{} string gettype() //return type as
parallelogram

6
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

{ return "parallelogram"; }};


class triangle : public polygon //derive class
from polygon
{public: triangle(double w=0, double h=0):
polygon(w,h) //two argument constructor
{} double getArea() //return value of area
{ return (getwidth()* getheight())/2; }
string gettype() //return type as triangle
{ return "triangle"; }};

int main()
{ polygon * ppoly[100] = {new
rectangle(1.2,2.1),new square(2.5), new
triangle(5.0,6.0), new parallelogram(20,30),new
rectangle(100,200),
new square(2.5), new triangle(5.0,6.0),
new parallelogram(20,30), NULL}; //pointer
declare and initialize
for(int i=0; ppoly[i]!=NULL; i++) //for loop is
used to display all data
{ cout<<i+1<<" ."<<ppoly[i]-
>gettype()<<endl<<"\n\t Width = "<<ppoly[i]-
>getwidth()<<"\n\t Height = "<<ppoly[i]-
>getheight()<<"\n\t Area = "<<ppoly[i]-
>getArea();
cout<<endl;
}
}

In Lab Tasks

In Lab Task 01: You are developing a payment processing system for an online marketplace. The
system must support multiple payment methods: Credit Card, PayPal, and Bank Transfer. Each
payment method has a different way of processing a payment, but all of them should follow a common
interface. Design this system using abstract classes.
 What is the common functionality all payment types must implement?
 How would you design the abstract base class?
 How will polymorphism help in processing payments uniformly?

7
AIR UNIVERSITY, ISLAMABAD
Department of Electrical & Computer Engineering

LAB 09– COMPUTER PROGRAMMING


LAB TASKS

Lab Title: Polymorphism and Abstract Class.


Lab Task 01: You are building a simulation for a zoo. The zoo contains various animals like Lion,
Elephant, and Monkey. Each animal makes a different sound and eats different food. You want to
design the system so that all animals can be handled uniformly using base class pointers or references.
Use virtual functions to achieve polymorphism.
 What behaviors are common to all animals?
 How will polymorphism help you treat different animals the same way in code?
 How can virtual functions help achieve dynamic behavior?

Lab Task 02: You are developing a drawing application that allows users to draw different shapes
like Circle, Rectangle, and Triangle. Each shape must implement functions to calculate its area and
draw itself on the screen. Design a class structure using an abstract class that defines the common
interface for all shapes.
 What are the common behaviors shared by all shapes?
 Why use an abstract class instead of a concrete base class?
 How would polymorphism help in managing different shapes?

You might also like