Current Affairs updated daily
KalviGavaniKalviGavani

Polymorphism Exercise Solutions | KalviGavani

TN Class 11 Computer Science Polymorphism exercise solutions — function overloading, operator overloading, virtual functions with worked examples.

Updated 15 June 202615 min read

Exercise Solutions Available

Step-by-step answers for all textbook questions in this chapter

View Solutions →

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

TypeWhen ResolvedMechanismsExample
Compile-time (Static)During compilationFunction overloading, Operator overloadingadd(int, int) vs add(float, float)
Runtime (Dynamic)During executionVirtual functions, Function overridingBase 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

FeatureOverloadingOverriding
WhereSame classBase and derived class
ParametersMust differMust be the same
virtual keywordNot neededNeeded for runtime binding
ResolutionCompile-timeRuntime
InheritanceNot requiredRequired

3. I. Choose the Best Answer (MCQs)

1. Polymorphism means:
Answer

(a) Many forms (b) Single form (c) No form (d) Hidden form

Answer(a) Many forms "Poly" means many and "morph" means forms. Polymorphism allows the same function or operator to behave differently in different contexts.
2. Function overloading is an example of:
Answer

(a) Runtime polymorphism (b) Compile-time polymorphism (c) Inheritance (d) Encapsulation

Answer(b) Compile-time polymorphism Function overloading is resolved at compile time — the compiler selects the correct function based on the argument types. Runtime polymorphism uses virtual functions.
3. Which of the following can be overloaded in C++?
Answer

(a) :: (scope resolution) (b) . (member access) (c) + (addition) (d) ?: (ternary)

Answer(c) + (addition) The + operator can be overloaded for user-defined types. The ::, ., and ?: operators cannot be overloaded.
4. The keyword used to declare a virtual function is:
Answer

(a) static (b) virtual (c) friend (d) inline

Answer(b) virtual The virtual keyword before a function declaration tells the compiler to use dynamic binding, enabling runtime polymorphism.
5. In function overloading, functions must differ in:
Answer

(a) Return type only (b) Parameter list (number, type, or order) (c) Function name (d) Access specifier

Answer(b) Parameter list (number, type, or order) Overloaded functions must have different parameter lists. Having the same name with the same parameters but a different return type alone is NOT valid overloading.
6. What is the output?
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;
}
Answer

(a) Base (b) Derived (c) Error (d) BaseDerived

Answer(b) Derived Since show() is virtual in the base class, runtime polymorphism applies. The pointer is of type Base* but points to a Derived object, so Derived::show() is called.

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 AColumn B
1. Polymorphisma. Same name, different parameters
2. Function overloadingb. Compile-time polymorphism
3. Virtual functionc. Many forms
4. Operator overloadingd. Giving new meaning to +, -, etc.
5. Function overridinge. Derived class redefines base function
6. ::, ., ?:f. Cannot be overloaded
7. virtual keywordg. Enables runtime polymorphism
Answer1-c, 2-b, 3-g, 4-d, 5-e, 6-f, 7-g

Note: Both 3 and 7 match (g). Virtual functions enable runtime polymorphism.


7. V. Answer Briefly (2-mark questions)

Q1. What is polymorphism? Name its types.
Answer

Polymorphism means "many forms" — the ability of a function or operator to behave differently in different contexts. It is a core principle of OOP.

The two types are: 1. Compile-time (Static) polymorphism — Resolved during compilation. Achieved through function overloading and operator overloading. 2. Runtime (Dynamic) polymorphism — Resolved during program execution. Achieved through virtual functions and function overriding.

Q2. Differentiate between function overloading and function overriding.
Answer
FeatureFunction OverloadingFunction Overriding
WhereSame class or scopeBase and derived class
ParametersMust differMust be the same
InheritanceNot requiredRequired
KeywordNonevirtual in base class
BindingCompile-timeRuntime
PurposeDifferent behaviour for same nameRedefine base class behaviour
Q3. What is operator overloading? Which operators cannot be overloaded?
Answer

Operator overloading allows existing C++ operators to work with user-defined types (objects) by defining a special function using the operator keyword.

Syntax: returnType operator symbol(parameters)

Operators that cannot be overloaded:

  • Scope resolution (::)
  • Member access (.)
  • Member pointer access (.*)
  • Ternary (?:)
  • sizeof
Q4. What is a virtual function? How does it enable runtime polymorphism?
Answer

A virtual function is a member function declared with the virtual keyword in the base class. When a base class pointer refers to a derived class object and calls a virtual function, the derived class version is executed.

class Base {
public:
    virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
    void show() { cout << "Derived"; }  // Overrides
};
Base *ptr = new Derived();
ptr->show();  // Calls Derived::show() — runtime polymorphism

Without virtual, ptr->show() would call Base::show() regardless of the object type.

Q5. Write the syntax for overloading the + operator for a class.
class ClassName {
public:
    ClassName operator + (ClassName obj) {
        ClassName result;
        // Add corresponding members
        result.member = member + obj.member;
        return result;
    }
};
Answer

The operator+ function is called when + is used between two objects of the class.


8. VI. Answer in Detail (5-mark questions)

Q1. Explain function overloading with a C++ program that calculates the area of a circle, rectangle, and triangle using overloaded functions.
Answer

Function overloading allows multiple functions with the same name but different parameter lists. The compiler selects the appropriate function based on the arguments.

#include <iostream>
using namespace std;

// Area of circle
float area(float radius) {
    return 3.14 * radius * radius;
}

// Area of rectangle
float area(float length, float breadth) {
    return length * breadth;
}

// Area of triangle
float area(float base, float height, int flag) {
    return 0.5 * base * height;
}

int main() {
    float r, l, b, base, h;

    cout << "Enter radius of circle: ";
    cin >> r;
    cout << "Area of circle = " << area(r) << endl;

    cout << "Enter length and breadth of rectangle: ";
    cin >> l >> b;
    cout << "Area of rectangle = " << area(l, b) << endl;

    cout << "Enter base and height of triangle: ";
    cin >> base >> h;
    cout << "Area of triangle = " << area(base, h, 0) << endl;

    return 0;
}

How it works:

  • area(float) — called with one argument, calculates circle area.
  • area(float, float) — called with two arguments, calculates rectangle area.
  • area(float, float, int) — called with three arguments, calculates triangle area (the third int parameter distinguishes it from the rectangle version).

The compiler resolves the correct function at compile time based on the number and types of arguments. This is compile-time polymorphism.

Q2. Explain operator overloading with a C++ program to overload the + operator to add two complex numbers.
Answer

Operator overloading allows existing operators to work with user-defined types. We use the operator keyword followed by the operator symbol.

#include <iostream>
using namespace std;

class Complex {
    float real, imag;
public:
    Complex() { real = 0; imag = 0; }
    Complex(float r, float i) { real = r; imag = i; }

    // Overloading + operator
    Complex operator + (Complex c) {
        Complex result;
        result.real = real + c.real;
        result.imag = imag + c.imag;
        return result;
    }

    void display() {
        cout << real << " + " << imag << "i" << endl;
    }
};

int main() {
    Complex c1(3.5, 2.5), c2(1.5, 4.5), c3;

    c3 = c1 + c2;  // Calls c1.operator+(c2)

    cout << "First complex number: ";
    c1.display();
    cout << "Second complex number: ";
    c2.display();
    cout << "Sum: ";
    c3.display();

    return 0;
}
// Output:
// First complex number: 3.5 + 2.5i
// Second complex number: 1.5 + 4.5i
// Sum: 5 + 7i

How it works:

  • c1 + c2 is translated to c1.operator+(c2).
  • The operator+ function creates a new Complex object whose real part is the sum of the real parts and whose imaginary part is the sum of the imaginary parts.
  • The result is returned as a Complex object.

Rules of operator overloading: 1. Only existing operators can be overloaded (no new operators). 2. Cannot change the number of operands. 3. Cannot change precedence or associativity. 4. At least one operand must be a user-defined type. 5. ::, ., .*, ?:, and sizeof cannot be overloaded.

Q3. Explain virtual functions and runtime polymorphism with a C++ program.
Answer

A virtual function is declared with the virtual keyword in the base class. When a base class pointer or reference points to a derived class object, the derived class function is called — this is runtime polymorphism.

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void makeSound() {  // Virtual function
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() {  // Overrides base class
        cout << "Dog barks" << endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() {  // Overrides base class
        cout << "Cat meows" << endl;
    }
};

int main() {
    Animal *ptr;    // Base class pointer
    Animal a;
    Dog d;
    Cat c;

    ptr = &a;
    ptr->makeSound();  // Calls Animal::makeSound()

    ptr = &d;
    ptr->makeSound();  // Calls Dog::makeSound() — runtime polymorphism

    ptr = &c;
    ptr->makeSound();  // Calls Cat::makeSound() — runtime polymorphism

    return 0;
}
// Output:
// Animal makes a sound
// Dog barks
// Cat meows

How it works:

  • The base class Animal declares makeSound() as virtual.
  • Derived classes Dog and Cat override this function with their own implementations.
  • When ptr (of type Animal*) points to a Dog object, ptr->makeSound() calls Dog::makeSound().
  • When ptr points to a Cat object, ptr->makeSound() calls Cat::makeSound().
  • The decision of which function to call is made at runtime based on the object type — this is runtime polymorphism.

Without virtual: If makeSound() were not virtual, ptr->makeSound() would always call Animal::makeSound() regardless of what object ptr points to, because the call would be resolved at compile time based on the pointer type.


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 virtual keyword enables dynamic binding
  • [ ] I can explain why return type alone is insufficient for overloading

Frequently Asked Questions

Polymorphism means 'many forms'. It allows the same function name or operator to behave differently based on context. There are two types: compile-time (function overloading, operator overloading) and runtime (virtual functions).