This document discusses polymorphism in C++, focusing on its definition, types, and implementation. It explains compile-time polymorphism through function and operator overloading, highlighting how functions can share names but behave differently based on parameters. The document also provides an example of function overloading with a class demonstrating different print methods for various data types.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views8 pages
OOP Lecture 5
This document discusses polymorphism in C++, focusing on its definition, types, and implementation. It explains compile-time polymorphism through function and operator overloading, highlighting how functions can share names but behave differently based on parameters. The document also provides an example of function overloading with a class demonstrating different print methods for various data types.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
OBJECT ORIENTED PROGRAMMING
[CS1420]
L E C T U R E 5 : P O LY M O R P H I S M static and dynamic polymorphism, virtual function, Abstract classes, static and dynamic casting, friend functions and Operator overloading
Engr. Syeda Aimen Naeem
[email protected] P O LY M O R P H I S M • Polymorphism means the ability to take more than one form. • Polymorphism in C++ refers to the ability of objects or functions to take on multiple forms based on their context or usage. It allows different objects to respond to the same message differently. • Like real-life scenarios where a person can exhibit various roles simultaneously, such as being a parent, spouse, and employee. • This feature is integral to Object-Oriented Programming and enhances code flexibility and reusability. TYPES OF POLYMORPHISM COMPILE TIME POLYMORPHISM Compile-time Polymorphism refers to the mechanism in C++ where the compiler determines which function to call during compilation based on the function’s signature and the context in which it is invoked. This is typically achieved through • function overloading and • operator overloading. FUNCTION OVERLOADING • Function overloading is a feature of object-oriented programming where two or more functions can have the same name but behave differently for different parameters. • Such functions are said to be overloaded; hence, this is known as Function Overloading. • Functions can be overloaded through function signature, i.e., either by changing the number of arguments or changing the type of arguments. FUNCTION OVERLOADING EXAMPLE class Overload { public: void print(int num) { cout << "Printing integer: " << num << endl; }
void print(double num) {
cout << "Printing double: " << num << endl; }
void print(string text) {
cout << "Printing string: " << text << endl; } }; FUNCTION OVERLOADING EXAMPLE • Note: can change the function return type, but only changing the return type will not be considered as function overloading.
int add(int a, int b);
double add(int a, int b); // Error: Redefinition of 'add’ Compilation error