Lecture 9
Lecture 9
Lecture - 9
int main() {
Print obj;
obj.display(10); // Calls the int version
obj.display(3.14); // Calls the double version
return 0;
}
Method Overloading
Method overloading occurs when multiple methods
have the same name but differ in the number or type
of their parameters.
The correct method is chosen based on the arguments
passed at the time of the method call.
Imagine a scenario where you are building a system
for printing different types of information,
printing a message,
printing a number
printing a floating-point value.
All of these actions can be referred to as "print", but
the content of the print varies based on the type of
data.
Method Overloading
#include <iostream>
using namespace std;
class Printer {
public:
// Method to print an integer int main() {
void print(int i) {
cout << "Printing an integer: " << i << Printer p;
endl;
} p.print(10);
// Method to print a floating-point number p.print(3.14 );
void print(double d) {
cout << "Printing a double: " << d <<
p.print("Hello”);
endl;
} return 0;
// Method to print a string message }
void print(string s) {
cout << "Printing a string: " << s << endl;
}
};
Operator Overloading
C++ has the ability to provide the operators
with a special meaning for a data type, this
ability is known as operator overloading.
For example, we can make use of the addition
operator (+) for string class to concatenate two
strings.
We know that the task of this operator is to add
two operands. So a single operator ‘+’, when
placed between integer operands, adds them
and when placed between string operands,
concatenates them.
Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag; int main()
public:
Complex(int r =0, int i=0)
{
{ Complex c1(10, 5), c2(2, 4);
real = r;
imag = i;
Complex c3 = c1 + c2;
} c3.print();
Complex operator+(Complex const& obj)
{
}
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};
Dynamic Polymorphism
Dynamic polymorphism occurs when the
decision about which method to call is made
at runtime.
Achieved via inheritance and virtual
functions.
Use Case: Used when you want a function to
behave differently for different object types
(even if they are through the same base class
pointer or reference).
Virtual Functions
A virtual function is a function that is
declared in the base class and overridden in
the derived class.
It ensures the correct function is called for
an object, regardless of the type of reference
(or pointer) used for the function call.
Virtual Functions
#include <iostream>
using namespace std; int main() {
class Shape { Shape* shape1 = new Circle();
public:
virtual void draw() { Shape* shape2 = new Square();
cout << "Drawing Shape" << endl;
}
}; shape1->draw(); // Outputs:
Drawing Circle
class Circle : public Shape {
public:
shape2->draw(); // Outputs:
void draw() override { Drawing Square
cout << "Drawing Circle" << endl;
}
};
delete shape1;
delete shape2;
class Square : public Shape {
public:
return 0;
void draw() override { }
cout << "Drawing Square" << endl;
}
};
Pure Virtual Functions
A pure virtual function is a virtual function
with no implementation in the base class,
forcing derived classes to implement it.
Syntax: Declare the virtual function with = 0
at the end.
Abstract Class: A class containing at least
one pure virtual function is an abstract class
and cannot be instantiated.
Pure Virtual Functions
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
int main() {
// Shape s; // Error: Cannot instantiate an abstract class
Shape* shape = new Circle();
shape->draw(); // Outputs: Drawing Circle
delete shape;
return 0;
}