0% found this document useful (0 votes)
19 views14 pages

Lecture 9

This document discusses polymorphism in C++, a key feature of object-oriented programming that allows functions to behave differently based on the object type. It covers two types of polymorphism: static (compile-time) achieved through method and operator overloading, and dynamic (runtime) achieved using inheritance and virtual functions. Examples illustrate method overloading, operator overloading, and the use of virtual functions and pure virtual functions in class design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Lecture 9

This document discusses polymorphism in C++, a key feature of object-oriented programming that allows functions to behave differently based on the object type. It covers two types of polymorphism: static (compile-time) achieved through method and operator overloading, and dynamic (runtime) achieved using inheritance and virtual functions. Examples illustrate method overloading, operator overloading, and the use of virtual functions and pure virtual functions in class design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Object Oriented programming using C++

Lecture - 9

Priya Pankaj Kumar


Assistant Professor-CSE
Sershah Engineering College, Sasaram
Polymorphism
 Polymorphism in C++ is a feature of object-
oriented programming (OOP) that allows a
function, method, or operator to behave
differently based on the object it is acting
upon.
 Types of Polymorphism:
 Compile-time (Static)Polymorphism
 Runtime (Dynamic)Polymorphism
Types of Polymorphism
 Static Polymorphism:
 Occurs at compile time.
 Achieved through method overloading and operator overloading.
 Dynamic Polymorphism:
 Occurs at runtime.
 Achieved using inheritance and virtual functions.
Static Polymorphism
 Method Overloading:
 Multiple functions with the same name but
different parameters.
 C++ resolves the function based on the function
signature at compile time.
 Operator Overloading:
 Customizing the behavior of operators for user-
defined types.
 Done at compile time..
Static Polymorphism
#include <iostream>
using namespace std;
class Print {
public:
void display(int i) {
cout << "Integer: " << i << endl;
}
void display(double d) {
cout << "Double: " << d << endl;
}
};

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
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

int main() {
// Shape s; // Error: Cannot instantiate an abstract class
Shape* shape = new Circle();
shape->draw(); // Outputs: Drawing Circle
delete shape;
return 0;
}

You might also like