0% found this document useful (0 votes)
4 views2 pages

Polymorphism

Polymorphism in C++ refers to the ability of functions/operators to exhibit different behaviors based on context, with two main types: Compile-time (Static) and Run-time (Dynamic). Compile-time polymorphism includes function and operator overloading, while run-time polymorphism involves function overriding using the virtual keyword. Key differences include binding time, examples, and performance implications.

Uploaded by

b424065
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)
4 views2 pages

Polymorphism

Polymorphism in C++ refers to the ability of functions/operators to exhibit different behaviors based on context, with two main types: Compile-time (Static) and Run-time (Dynamic). Compile-time polymorphism includes function and operator overloading, while run-time polymorphism involves function overriding using the virtual keyword. Key differences include binding time, examples, and performance implications.

Uploaded by

b424065
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

Polymorphism in C++ (Exam Notes)

1■■ Introduction
Polymorphism means 'many forms'. It allows the same function/operator to have different behaviors
depending on the situation.

Example: The operator '+' can be used for numbers (addition) and for strings (concatenation). A
function 'speak()' can mean different things for Animal vs Dog.

There are two main types: (1) Compile-time Polymorphism (Static / Early Binding), (2) Run-time
Polymorphism (Dynamic / Late Binding).

2■■ Compile-Time Polymorphism

Function Overloading
Same function name, different parameters. Decided at compile time.

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

int main() {
Math m;
cout << m.add(5, 10) << endl; // int version
cout << m.add(2.5, 3.7) << endl; // double version
}

Operator Overloading
You can redefine operators for user-defined types.

class B {
public:
int a;
void operator+(B &obj;) { cout << "Operator + Overloaded" << endl; }
void operator() () { cout << "Operator () Overloaded: Value = " <<
this->a << endl; }
};

int main() {
B obj1, obj2;
obj1.a = 4; obj2.a = 7;
obj1 + obj2; // Calls overloaded +
obj1(); // Calls overloaded ()
}
3■■ Run-Time Polymorphism
Function Overriding → Derived class provides its own implementation of a base class function.

class Animal {
public:
virtual void speak() { cout << "Speaking" << endl; }
};

class Dog : public Animal {


public:
void speak() override { cout << "Barking" << endl; }
};

int main() {
Animal* a = new Dog();
a->speak(); // Prints "Barking"
}

4■■ Differences Between Compile-Time and Run-Time Polymorphism


FeatureCompile-Time (Static)Run-Time (Dynamic) Binding TimeCompile timeRuntime
ExamplesFunction Overloading, Operator OverloadingFunction Overriding virtual keyword
neededNoYes PerformanceFasterSlightly Slower

5■■ Important Questions with Answers


Q1. Define Polymorphism. Explain its types with examples.
Answer: Polymorphism means 'many forms'. It allows the same function/operator to behave differently
in different contexts. Types: Compile-time Polymorphism (Overloading), Run-time Polymorphism
(Overriding with virtual).

Q2. What is operator overloading? Explain with syntax and example.


Answer: Operator overloading allows redefining how operators work for user-defined types.
Syntax: return_type operator symbol(arguments)
Example: void operator+(B &obj;) { cout << 'Operator + Overloaded'; }

Q3. Differentiate between function overloading and function overriding.


Overloading → Same name, different parameters, resolved at compile-time.
Overriding → Same name and parameters in base/derived, resolved at runtime using virtual.

Q4. Write a program in C++ to demonstrate runtime polymorphism using virtual functions.
Answer: Use the Animal/Dog example above with virtual keyword.

Q5. Explain compile-time vs run-time polymorphism in C++.


Answer: Compile-time polymorphism is resolved during compilation (overloading). Run-time
polymorphism is resolved during execution (overriding with virtual).

You might also like