Object-Oriented Programming
Using C++
Lecture#8
Name: Farhan (PhD in Progress)
University of Agriculture, Peshawar
[email protected]Outline
Introduction to
Polymorphism
Virtual and pure virtual
functions
Abstract Base classes and
Concrete derived classes
Polymorphism
Poly Many
Morphs Forms
Polymorphism Many forms
It simply means more than one form
Same entity (method or object) can perform different operations in
different scenarios
3
Polymorphism
Real life example of polymorphism
A person at the same time can have different characteristic
Like a man at the same time is a
4
Introduction to Polymorphism
Definition:
Polymorphism means “many forms.”
It allows the same function or method to behave differently on
different objects.
Types of Polymorphism:
i) Compile-time Polymorphism
Achieved through Function Overloading and Operator
Overloading
Resolved during compilation
ii) Run-time Polymorphism
Achieved using Virtual Functions
Resolved during program execution (dynamic dispatch)
Key Benefits:
Enhances flexibility in code.
Promotes code reuse and extensibility.
Simplifies interface management in large systems.
5
Introduction to Polymorphism
#include <iostream>
using namespace std;
class Print {
public:
void show(int num) {
cout << "Integer: " << num << endl;
}
void show(string text) {
cout << "Text: " << text << endl;
}};
int main() {
Print p;
p.show(10); // Calls show(int)
p.show("Hello"); // Calls show(string)
return 0;
}
6
Introduction to Polymorphism
#include <iostream>
using namespace std;
class Animal {
public:
void speak() {
cout << "Animal makes a sound." << endl;}
void speak(string name) {
cout << name << " makes a sound." << endl;
}};
int main() {
Animal a;
a.speak(); // Output: Animal makes a sound.
a.speak("Dog"); // Output: Dog makes a sound.
return 0;
}
7
What is Virtual Function
A virtual function is a member function in a base class
that can be overridden in a derived class.
Syntax:
class Base
{ public:
virtual void show()
{ cout << "Base class show function"; } };
Key Point:
It can have a body in the base class.
Not mandatory to override in the derived class.
Usage:
Allows runtime polymorphism using base class
pointers or references.
8
What is Virtual Function
A virtual function allows runtime
polymorphism. It lets a base class function
be overridden by derived classes.
It tells the compiler to use runtime (dynamic)
binding.
Example
Step1:
class Animal {
public:
virtual void speak() {
cout << "Animal makes a sound." << endl; }};
virtual void speak() means this function can be
overridden in derived classes, and the correct
function will be called at runtime. 9
Example of Virtual Function
Step 2:
class Dog : public Animal {
public:
void speak() override {
cout << "Dog barks." << endl;}};
Step 3: Use a Base Class Pointer or Reference
Use a base class pointer to hold the address of the
derived class object. This enables polymorphism.
Animal* a;
Dog d;
a = &d;
Step 4: Call the Function – Runtime Polymorphism
Happens
Now when you call the function using the base class
pointer, the derived class version is called:
a->speak(); // Output: Dog barks.
Example of Virtual
Polymorphism
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}};
int main() {
Animal* a; // Base class pointer
Dog d; // Derived class object
a = &d; // Point to Dog object
a->sound(); // Output: Dog barks
return 0;
}
Pure virtual function
A pure virtual function is a virtual function that
.
has no body in the base class and must be
overridden in the derived class.
Syntax:
class Base
{ public: virtual void show() = 0; // pure
virtual };
Key Point:
Declares the class as abstract.
You cannot create an object of a class with
a pure virtual function.
Usage: Used when you want to force derived
12
Pure virtual function
A. pure virtual function is a function
in a base class that must be
overridden by derived classes.
It has no body in the base class.
It is written using = 0.
🔸 When a class has at least one pure
virtual function, it becomes an
abstract class — you cannot
create objects of it.
13
Pure virtual function
#include <iostream>
using
. namespace std;
// Abstract base class
class Animal {
public:
virtual void sound() = 0; // Pure virtual function
};
// Derived class must override
the function
class Cat : public Animal {
public:
void sound() override {
cout << "Cat meows" << endl;
}};
int main() {
// Animal a; ❌ Not allowed:
abstract class
Cat c;
c.sound(); // Output: Cat meows 14
Abstract Base classes & Concrete
derived classes
Abstract Base Class
An abstract base class is a class that has at
least one pure virtual function.
Why Use It?
Acts like a blueprint.
Cannot create objects of an abstract class.
Forces derived classes to implement specific
functions.
Syntax Example:
class Shape
{ public:
virtual void draw() = 0; // Pure virtual
15
Abstract Base classes & Concrete
derived classes
A concrete derived class is a class that inherits
from an abstract base class and provides
definitions for all pure virtual functions.
Why Use It?
It gives a complete implementation of the
abstract blueprint.
Can create objects of the concrete class.
Syntax Example:
class Circle : public Shape { public: void draw()
{ cout << "Drawing Circle"; } };
Key Point: The concrete class must override
all pure virtual functions of the abstract base
16
Abstract and Concrete Class
Easy Real-Life Example:
Abstract Class (Shape) → like a contract: "Any shape must know how to draw itself."
Concrete Class (Circle, Square) → gives actual drawing code.
#include <iostream>
using namespace std; // Abstract Base Class
class Animal {
public: // Pure virtual function
virtual void sound() = 0; }; // Must be overridden
// Concrete Derived Class
class Dog : public Animal {
public:
void sound() {
cout << "Dog says: Woof!" << endl;}}; // Another Concrete Derived
Class (Optional)
class Cat : public Animal {
public:
void sound() {
cout << "Cat says: Meow!" << endl; }};
int main() {
Dog d;
d.sound(); // Output: Dog says: Woof!
Cat c;
c.sound(); // Output: Cat says: Meow!
// Animal a; ❌ Error: cannot create object of abstract class
17
Pure and Concrete Class
#include <iostream>
using namespace std;
// Abstract Base Class
class Animal {
public:
virtual void sound() = 0; // Pure virtual function};
// Concrete Derived Class
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;}};
int main() {
// Animal a; ❌ Error: Can't create object of abstract class
Dog d; // ✅ OK: Concrete class
d.sound(); // Output: Dog barks
return 0;}
18
Function Overloading
Same function name.
Different parameter list:
Different number of parameters.
Different types of parameters.
Different order of parameters (if
types are different).
Function Overloading
Output
sum1=11
Sum2=11
.2
sum3=18
Assignment and Quiz
Write a C++ program to create a class Calculator
that overloads a function operate() to:
Add two integers.
Add two floats.
Concatenate two strings.
Create a class Area that has overloaded functions to
calculate:
Area of a circle.
Area of a rectangle.
Area of a triangle.
Design a class Distance that can:
Add distances in kilometers and meters.
Add distances in miles and feet.
Show result in meters.
Use function overloading to perform unit conversions and