Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
1. How to Use This Page
1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.
2. Key Concepts (Quick Recap)
What is Polymorphism?
Polymorphism comes from Greek: poly (many) + morph (forms). It allows the same interface to behave differently in different contexts.
Types of Polymorphism
| Type | When Resolved | Mechanisms | Example |
|---|---|---|---|
| Compile-time (Static) | During compilation | Function overloading, Operator overloading | add(int, int) vs add(float, float) |
| Runtime (Dynamic) | During execution | Virtual functions, Function overriding | Base pointer calling derived function |
Function Overloading
Multiple functions with the same name but different parameters (number, type, or order).
int add(int a, int b) {
return a + b; // add(int, int)
}
float add(float a, float b) {
return a + b; // add(float, float)
}
int add(int a, int b, int c) {
return a + b + c; // add(int, int, int)
}
// Compiler selects the correct version based on arguments:
add(3, 4); // calls add(int, int) → 7
add(3.5f, 4.2f); // calls add(float, float) → 7.7
add(1, 2, 3); // calls add(int, int, int) → 6
Rules for overloading:
- Parameter list must differ (number, type, or order of parameters).
- Return type alone is NOT sufficient to overload a function.
- Default arguments do not create overloads.
Operator Overloading
Giving additional meaning to existing C++ operators for user-defined types (objects).
class Complex {
float real, imag;
public:
Complex(float r = 0, float i = 0) { real = r; imag = i; }
Complex operator + (Complex c) {
return Complex(real + c.real, imag + c.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
Complex c1(3.5, 2.5), c2(1.5, 4.5);
Complex c3 = c1 + c2; // Calls operator+(c2)
c3.display(); // 5 + 7i
Rules for operator overloading:
- Cannot create new operators (only overload existing ones).
- Cannot change the number of operands an operator takes.
- Cannot change the precedence or associativity.
- Some operators cannot be overloaded:
::,?:,.,.*,sizeof.
Virtual Functions
A virtual function is declared in the base class with the virtual keyword. It enables runtime polymorphism — the function called depends on the object type, not the pointer type.
class Shape {
public:
virtual void draw() { // Virtual function
cout << "Drawing shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() { // Overrides base class function
cout << "Drawing circle" << endl;
}
};
Shape *ptr;
Circle c;
ptr = &c;
ptr->draw(); // Calls Circle::draw() — runtime polymorphism
Function Overloading vs Function Overriding
| Feature | Overloading | Overriding |
|---|---|---|
| Where | Same class | Base and derived class |
| Parameters | Must differ | Must be the same |
virtual keyword | Not needed | Needed for runtime binding |
| Resolution | Compile-time | Runtime |
| Inheritance | Not required | Required |
3. I. Choose the Best Answer (MCQs)
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() { cout << "Derived"; }
};
int main() {
Base *ptr = new Derived();
ptr->show();
return 0;
}
4. II. Fill in the Blanks
1. Polymorphism means many forms. 2. Function overloading is an example of compile-time polymorphism. 3. Virtual functions enable runtime polymorphism. 4. The virtual keyword is used to declare a virtual function in the base class. 5. In function overloading, the parameter list must differ in number, type, or order. 6. The return type alone is not sufficient to overload a function. 7. Operator overloading allows existing operators to work with user-defined types. 8. The operator keyword is used to define an overloaded operator function. 9. The :: (scope resolution) operator cannot be overloaded. 10. When a derived class redefines a base class virtual function, it is called function overriding. 11. A function with the same name but different parameters in the same class is called function overloading. 12. Runtime polymorphism is achieved through virtual functions and base class pointers.
5. III. True or False (with corrections)
1. Function overloading requires the functions to be in different classes.
False. Function overloading involves multiple functions with the same name in the same class (or scope) with different parameter lists. Functions in different classes with the same name is function overriding (with inheritance).
2. The return type alone is sufficient to overload a function.
False. Two functions with the same name and same parameters but different return types cause a compilation error. The parameter list must differ for valid overloading.
3. Virtual functions enable runtime polymorphism.
True. When a function is declared virtual in the base class, the function call is resolved at runtime based on the object type, not the pointer type. This is runtime polymorphism.
4. All C++ operators can be overloaded.
False. The following operators cannot be overloaded: scope resolution (::), member access (.), member pointer access (.*), ternary (?:), and sizeof.
5. In function overriding, the parameter list must be the same in both the base and derived class functions.
True. Overriding means the derived class provides a new implementation of a base class function with the same name and same parameters. If parameters differ, it becomes overloading, not overriding.
6. Operator overloading changes the precedence of operators.
False. Operator overloading cannot change the precedence, associativity, or number of operands of an operator. It only provides additional meaning for user-defined types.
6. IV. Match the Following
| Column A | Column B |
|---|---|
| 1. Polymorphism | a. Same name, different parameters |
| 2. Function overloading | b. Compile-time polymorphism |
| 3. Virtual function | c. Many forms |
| 4. Operator overloading | d. Giving new meaning to +, -, etc. |
| 5. Function overriding | e. Derived class redefines base function |
6. ::, ., ?: | f. Cannot be overloaded |
7. virtual keyword | g. Enables runtime polymorphism |
Note: Both 3 and 7 match (g). Virtual functions enable runtime polymorphism.
7. V. Answer Briefly (2-mark questions)
+ operator for a class.class ClassName {
public:
ClassName operator + (ClassName obj) {
ClassName result;
// Add corresponding members
result.member = member + obj.member;
return result;
}
};
8. VI. Answer in Detail (5-mark questions)
+ operator to add two complex numbers.9. Common Mistakes to Avoid
1. Thinking return type alone differentiates overloaded functions — Two functions with the same name and same parameters but different return types cause a compilation error. Parameters must differ.
2. Confusing overloading with overriding — Overloading: same class, different parameters. Overriding: base and derived class, same parameters, with inheritance.
3. Forgetting the virtual keyword — Without virtual, the base class function is called based on the pointer type, not the object type. Always use virtual for functions you intend to override.
4. Overloading operators with wrong syntax — The syntax is returnType operator symbol(parameters). For example, Complex operator + (Complex c), not Complex + operator (Complex c).
5. Trying to overload restricted operators — ::, ., .*, ?:, and sizeof cannot be overloaded. Memorise this list.
6. Changing operator arity — A binary operator (like +) must remain binary when overloaded. You cannot make it take three operands.
7. Not using virtual in the base class — If you override a function in the derived class but forget virtual in the base class, the base version is called through base pointers. This is the most common source of bugs in runtime polymorphism.
8. Overloading based on default arguments — Default arguments do not create new overloads. void f(int a) and void f(int a, int b = 0) would be ambiguous when called with one argument.
10. Exam Checklist
- [ ] I can define polymorphism and name its two types
- [ ] I can explain and write function overloading examples
- [ ] I can explain and write operator overloading examples (especially
+for Complex) - [ ] I can differentiate compile-time vs runtime polymorphism
- [ ] I can explain virtual functions and their role in runtime polymorphism
- [ ] I can differentiate function overloading from function overriding
- [ ] I can list operators that cannot be overloaded
- [ ] I can write programs demonstrating virtual functions with base class pointers
- [ ] I understand that the
virtualkeyword enables dynamic binding - [ ] I can explain why return type alone is insufficient for overloading
