ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-312Polymorphism
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-312

    Object Oriented Programming

    Progress0 / 16 done
    Topics16
    1Evolution of Object-Oriented (OO) Programming2Object-Oriented (OO) Concepts and Principles3Problem Solving in OO Paradigm4OO Programme Design Process5Classes6Methods7Objects and Encapsulation8Constructors and Destructors9Operator Overloading10Function Overloading11Virtual Functions12Derived Classes13Inheritance14Polymorphism15I/O and File Processing16Exception Handling
    7 min read
    Topic 14 of 16
    Topic Study Note Illustration
    14

    Polymorphism

    Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) . The term "polymorphism" comes from Greek, meaning "many forms." In programming, polymorphism allows objects of...

    Polymorphism in C++

    Polymorphism is one of the core concepts of Object-Oriented Programming (OOP). The term "polymorphism" comes from Greek, meaning "many forms." In programming, polymorphism allows objects of different classes to be treated as objects of a common base class. It enables one interface to control access to a general class of actions, where specific actions are determined at runtime.

    Polymorphism in C++ allows you to call derived class methods through a base class pointer or reference, providing flexibility and reusability in code.

    There are two main types of polymorphism in C++:

    1. Compile-time polymorphism (also known as Static polymorphism)
    2. Runtime polymorphism (also known as Dynamic polymorphism)

    1. Compile-Time Polymorphism (Static Polymorphism)

    Compile-time polymorphism is resolved during the compilation phase. The most common forms of compile-time polymorphism in C++ are:

    • Function overloading
    • Operator overloading

    Function Overloading

    Function overloading allows you to define multiple functions with the same name but with different parameters (either in number or type). The correct function is chosen at compile time based on the number and types of arguments passed.

    #include <iostream>
    using namespace std;
    
    class Print {
    public:
        void show(int i) {
            cout << "Integer: " << i << endl;
        }
        
        void show(double d) {
            cout << "Double: " << d << endl;
        }
        
        void show(string s) {
            cout << "String: " << s << endl;
        }
    };
    
    int main() {
        Print obj;
        obj.show(5);      // Calls the show(int) method
        obj.show(3.14);   // Calls the show(double) method
        obj.show("Hello"); // Calls the show(string) method
        
        return 0;
    }
    

    Explanation:

    • The show() function is overloaded for different parameter types: int, double, and string.
    • The appropriate function is called based on the type of argument passed at compile time.

    Output:

    Integer: 5
    Double: 3.14
    String: Hello
    

    Operator Overloading

    Operator overloading allows you to define how operators work for user-defined types (like classes). You can provide custom behavior for operators such as +, -, =, etc.

    #include <iostream>
    using namespace std;
    
    class Complex {
    public:
        int real, imag;
        
        Complex() : real(0), imag(0) {}
        
        Complex operator + (const Complex &c) {
            Complex temp;
            temp.real = real + c.real;
            temp.imag = imag + c.imag;
            return temp;
        }
        
        void display() {
            cout << real << " + " << imag << "i" << endl;
        }
    };
    
    int main() {
        Complex c1, c2, c3;
        c1.real = 3; c1.imag = 4;
        c2.real = 1; c2.imag = 2;
        
        c3 = c1 + c2;  // Operator + is overloaded
        
        c3.display();  // Output: 4 + 6i
        
        return 0;
    }
    

    Explanation:

    • The + operator is overloaded to add two Complex objects.
    • The custom behavior is defined by the operator+ function, which adds the real and imaginary parts of two complex numbers.

    Output:

    4 + 6i
    

    2. Runtime Polymorphism (Dynamic Polymorphism)

    Runtime polymorphism is resolved during the runtime of the program. The most common mechanism for achieving runtime polymorphism in C++ is through virtual functions. When a base class pointer or reference is used to point to a derived class object, the correct function is called based on the actual type of the object, not the type of the pointer.

    Virtual Functions

    A virtual function in the base class is overridden by the derived class, and the correct function is invoked based on the type of object being referred to, even when accessed through a base class pointer or reference.

    #include <iostream>
    using namespace std;
    
    class Shape {
    public:
        virtual void draw() {  // Virtual function
            cout << "Drawing Shape" << endl;
        }
    };
    
    class Circle : public Shape {
    public:
        void draw() override {  // Override the base class method
            cout << "Drawing Circle" << endl;
        }
    };
    
    class Square : public Shape {
    public:
        void draw() override {  // Override the base class method
            cout << "Drawing Square" << endl;
        }
    };
    
    int main() {
        Shape* shape;
        Circle circle;
        Square square;
        
        shape = &circle;
        shape->draw();  // Calls Circle's draw() at runtime
        
        shape = &square;
        shape->draw();  // Calls Square's draw() at runtime
        
        return 0;
    }
    

    Explanation:

    • draw() is a virtual function in the Shape class.
    • Circle and Square classes override the draw() function.
    • Despite the shape pointer being of type Shape*, the actual method that gets invoked is determined at runtime based on the type of the object (Circle or Square).

    Output:

    Drawing Circle
    Drawing Square
    

    Pure Virtual Functions and Abstract Classes

    A pure virtual function makes a class abstract, meaning that it cannot be instantiated. Derived classes must provide an implementation for the pure virtual function.

    #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 shape;  // Error: cannot instantiate an abstract class
        
        Circle circle;
        circle.draw();  // Calls Circle's draw() method
        
        return 0;
    }
    

    Explanation:

    • The draw() method in Shape is a pure virtual function (= 0), making Shape an abstract class.
    • Circle overrides the draw() method, and we can instantiate and use objects of Circle.

    Output:

    Drawing Circle
    

    Advantages of Polymorphism

    1. Code Reusability: Polymorphism allows a base class reference or pointer to be used for objects of different derived classes. This makes the code more flexible and reusable.

    2. Extensibility: New derived classes can be added without modifying the base class code, and the code that uses the base class can handle the new derived class objects seamlessly.

    3. Maintainability: Polymorphism reduces the need for complex conditionals (like if or switch) to handle different object types. It provides cleaner and more maintainable code.

    4. Flexibility: Through runtime polymorphism, the specific method of a derived class is called at runtime, depending on the object type, allowing for more dynamic and flexible program behavior.


    Summary

    • Polymorphism in C++ allows objects of different classes to be treated as objects of a common base class, with the behavior determined at runtime (through virtual functions) or at compile time (through function and operator overloading).
    • Compile-time polymorphism includes function and operator overloading, where the correct function is resolved during compilation.
    • Runtime polymorphism uses virtual functions and base class pointers/references to invoke derived class functions at runtime.
    • Polymorphism is a powerful mechanism in OOP that enhances flexibility, reusability, and maintainability in your programs.
    ← Previous topic 13
    Inheritance
    Next topic 15 →
    I/O and File Processing

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming