Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 51: Polymorphism in C++ – Types, Examples, and Applications | Class 11, Chapter 5

Understand polymorphism in C++ with Day 51 of Class 11 Computer Science, Chapter 5. Learn types, syntax, examples, and real-world applications.

Day 51 – Polymorphism in C++

Class 11 Computer Science – Chapter 5

What is Polymorphism? (Polymorphism क्या है?)

Polymorphism in C++ refers to the ability of a function, object, or operator to behave differently based on the context. It is one of the core principles of object-oriented programming.
C++ में Polymorphism का अर्थ है एक फ़ंक्शन, ऑब्जेक्ट, या ऑपरेटर का विभिन्न संदर्भों में अलग-अलग तरीके से व्यवहार करना। यह ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग के प्रमुख सिद्धांतों में से एक है।

Types of Polymorphism (Polymorphism के प्रकार)

  • Compile-Time Polymorphism (संकलन-समय बहुरूपता): Achieved using function overloading and operator overloading.
  • Run-Time Polymorphism (रन-समय बहुरूपता): Achieved using function overriding and virtual functions.

Examples of Polymorphism in C++ (C++ में Polymorphism के उदाहरण)

Example 1: Function Overloading

#include <iostream>
using namespace std;

class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }
    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator calc;
    cout << "Integer addition: " << calc.add(3, 4) << endl;
    cout << "Double addition: " << calc.add(2.5, 3.5) << endl;
    return 0;
}

Output:

Integer addition: 7
Double addition: 6

Example 2: Function Overriding

#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() {
        cout << "Base class show()" << endl;
    }
};

class Derived : public Base {
public:
    void show() override {
        cout << "Derived class show()" << endl;
    }
};

int main() {
    Base *ptr;
    Derived obj;
    ptr = &obj;
    ptr->show();
    return 0;
}

Output:

Derived class show()

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which of the following is an example of compile-time polymorphism?
    (a) Function Overloading | (b) Virtual Functions | (c) Function Overriding | (d) None
  2. What is the purpose of polymorphism?
    (a) Data security | (b) Code reusability | (c) Faster execution | (d) None
  3. Which keyword is used to define virtual functions?
    (a) virtual | (b) override | (c) const | (d) None
  4. What is function overriding?
    (a) Redefining a base class function in the derived class
    (b) Multiple functions with the same name but different parameters
    (c) Reusing an operator for user-defined types
    (d) None
  5. Which type of polymorphism is resolved at runtime?
    (a) Compile-Time | (b) Run-Time | (c) Static | (d) None
  6. What is operator overloading in C++?
    (a) Redefining an operator to work with user-defined types
    (b) Using multiple operators on one object
    (c) Using operators in functions
    (d) None
  7. What is the output of this code?
    class A {
    public:
        virtual void display() {
            cout << "Class A";
        }
    };
    
    class B : public A {
    public:
        void display() override {
            cout << "Class B";
        }
    };
    
    int main() {
        A *ptr;
        B obj;
        ptr = &obj;
        ptr->display();
        return 0;
    }
            
    (a) Class A | (b) Class B | (c) Error | (d) None
  8. What is the main advantage of polymorphism in OOP?
    (a) Flexibility | (b) Faster execution | (c) Data hiding | (d) None
  9. Which function call is resolved during runtime in polymorphism?
    (a) Overloaded functions | (b) Overridden functions | (c) Both | (d) None
  10. Which feature of polymorphism is achieved using function overriding?
    (a) Dynamic Method Dispatch | (b) Compile-Time Execution | (c) Operator Overloading | (d) None

Answers to MCQs:

1: (a), 2: (b), 3: (a), 4: (a), 5: (b), 6: (a), 7: (b), 8: (a), 9: (b), 10: (a)

Short Answer Questions

  1. What is polymorphism in C++?
    Answer: Polymorphism allows functions or objects to behave differently based on the context.
  2. What is the difference between compile-time and run-time polymorphism?
    Answer: Compile-time polymorphism is resolved during compilation (e.g., function overloading), while run-time polymorphism is resolved during execution (e.g., function overriding).
  3. Write a program to demonstrate operator overloading in C++.
    Answer:
    class Complex {
    public:
        int real, imag;
        Complex operator+(Complex obj) {
            Complex temp;
            temp.real = real + obj.real;
            temp.imag = imag + obj.imag;
            return temp;
        }
    };
    
  4. What is the role of virtual functions in polymorphism?
    Answer: Virtual functions allow dynamic method resolution, enabling run-time polymorphism.
  5. Write an example of function overriding in C++.
    Answer:
    class Base {
    public:
        virtual void display() {
            cout << "Base Class";
        }
    };
    
    class Derived : public Base {
    public:
        void display() override {
            cout << "Derived Class";
        }
    };
    

Long Answer Questions

  1. Explain polymorphism in C++ with examples of function overloading and overriding.
  2. Write a program to demonstrate virtual functions in C++.
  3. Discuss the advantages and disadvantages of polymorphism in object-oriented programming.

Post a Comment