CHAPTER 5
Polymorphism
and
Virtual Functions
Function Overloading
• When two or more functions in the same class
have the same name but different parameter
lists (number or type of parameters), it’s
called function overloading.
• The compiler decides which function to call at
compile time (also called early binding).
Example:
#include <iostream>
using namespace std;
class Printer {
public:
void print(int x) {
cout << "Printing integer: " << x << endl;
}
void print(string s) {
cout << "Printing string: " << s << endl;
}
};
int main() {
Printer p;
p.print(10); // Calls first function
p.print("Hello"); // Calls second function
}
Function Overriding
• When a derived class has a function with the
same name, parameters, and return type as a
function in the base class, it overrides the
base class version.
Used in inheritance to change behavior.
Example :
#include <iostream>
using namespace std;
class Animal {
public:
void speak() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void speak() { // Overrides Animal's speak
cout << "Dog barks" << endl;
}
};
int main() {
Dog d;
d.speak(); // "Dog barks“
return 0;
}
Differences Between Function
overloading and function overriding
Function Overloading Function Overriding
Same function name but different Same function name and same parameter
parameter list (number or type). list.
Occurs in base–derived class relationship
Occurs within the same class.
(inheritance).
Decided at compile time → compile-time Decided at runtime → runtime
polymorphism. polymorphism.
Return type can be different if parameters Return type must be same as base
are different. function.
Usually involves a virtual function in base
virtual keyword not required.
class.
Can happen without inheritance. Needs inheritance to work.
Basic Pointer Example
#include <iostream>
using namespace std;
int main() {
int x = 10; // A normal variable
int *ptr = &x; // ptr stores the address of x
cout << "Address stored in ptr: " << ptr << endl;
cout << "Value at that address: " << *ptr << endl; // Dereferencing
*ptr = 20; // Change the value at that address
cout << "New value of x: " << x << endl;
return 0;
}
Pointers to Objects
• A pointer to an object stores the address of that
object and can be used to access its members
using the arrow (->) operator.
• Syntax:
ClassName* pointerName;
• Use -> instead of . to access members through the
pointer.
ptr->function_name();
• Without ->, you’d have to write:
(*ptr).function_name();
Example:
#include <iostream>
using namespace std;
class Person {
public:
void introduce() {
cout << "Hello, I am a person" << endl;
}
};
int main() {
Person p;
Person *ptr = &p; // Pointer to object
ptr->introduce(); // Access using pointer
}
Virtual Functions & Late Binding
• A virtual function is a function in the base
class that you expect to override in derived
classes.
• If you call it through a base class pointer, C++
decides at runtime which version to use →
late binding or runtime polymorphism.
Example:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void speak() { // Virtual function
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void speak() override {
cout << "Dog barks" << endl;
}
};
int main() {
Animal *ptr;
Dog d;
ptr = &d;
ptr->speak(); // Late binding → "Dog barks"
}
Pure Virtual Function
• A Pure virtual function is a virtual function
with no body in the base class, declared using
= 0.
• Purpose → Forces derived classes to override
the function.
• Syntax:
virtual void functionName() = 0;
Example:
#include <iostream>
using namespace std;
class Shape //Abstract Class
{
public:
// Pure virtual function
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle\n";
}
};
int main() {
// Shape s; ❌ Cannot create object of abstract class
Circle c;
c.draw();
return 0;
}
Abstract Class
• An abstract class is a class that contains at least one
pure virtual function.
• Purpose → Works as a blueprint for other classes.
• Key point → You cannot create objects of abstract
classes.
• Example:
class Shape { // Abstract class
public:
virtual void draw() = 0; // Pure virtual function
};
Example:
#include <iostream>
using namespace std;
class Shape //Abstract class
{
public:
virtual void draw() = 0; // Pure virtual
void info() { cout << "I am a shape.\n"; }
};
class Square : public Shape {
public:
void draw() override {
cout << "Drawing Square\n";
}
};
int main() {
Shape *s = new Square();
s->draw(); // "Drawing Square"
s->info(); // "I am a shape."
delete s;
}
THANK YOU