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)
- Which of the following is an example of compile-time polymorphism?
(a) Function Overloading | (b) Virtual Functions | (c) Function Overriding | (d) None - What is the purpose of polymorphism?
(a) Data security | (b) Code reusability | (c) Faster execution | (d) None - Which keyword is used to define virtual functions?
(a) virtual | (b) override | (c) const | (d) None - 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 - Which type of polymorphism is resolved at runtime?
(a) Compile-Time | (b) Run-Time | (c) Static | (d) None - 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 - 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 - What is the main advantage of polymorphism in OOP?
(a) Flexibility | (b) Faster execution | (c) Data hiding | (d) None - Which function call is resolved during runtime in polymorphism?
(a) Overloaded functions | (b) Overridden functions | (c) Both | (d) None - 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
- What is polymorphism in C++?
Answer: Polymorphism allows functions or objects to behave differently based on the context. - 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). - 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; } }; - What is the role of virtual functions in polymorphism?
Answer: Virtual functions allow dynamic method resolution, enabling run-time polymorphism. - 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
- Explain polymorphism in C++ with examples of function overloading and overriding.
- Write a program to demonstrate virtual functions in C++.
- Discuss the advantages and disadvantages of polymorphism in object-oriented programming.
Post a Comment