Polymorphism

Chapter 15 · Tamil Nadu State Board

  • 6 min read
  • 6-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson explains polymorphism in C++ through function overloading, constructor overloading and operator overloading. Overloaded functions share a name but differ in their parameter signatures, allowing the compiler to select a suitable function for a call. Overloaded constructors provide different ways to create objects, while operator overloading gives existing operators additional meanings. These techniques help programs handle different kinds of data and actions using familiar names and symbols.

Written by AI from this lesson. How we check it

Key points

  • Polymorphism lets a message or function take more than one form, and C++ achieves it through function and operator overloading.
    From your textbook
    “In C++, polymorphism is achieved through function overloading and operator overloading.”
  • Functions in the same scope can share a name if their parameter numbers or types differ.
    From your textbook
    “two or more functions in the same scope share the same name but their parameters are different.”
  • The compiler selects an overloaded function by comparing the call's argument types with the definitions' parameter types.
    From your textbook
    “the compiler determines the most appropriate definition to use, by comparing the argument types”
  • A function's return type alone does not distinguish overloaded functions, and default arguments are not part of the parameter list for this purpose.
    From your textbook
    “The return type of overloaded functions are not considered for overloading same data type”
  • A class can have several constructors with different signatures, allowing objects to be created in different ways.
    From your textbook
    “A class can have more than one constructor with different signature.”
  • Operator overloading gives an existing C++ operator an additional user-defined meaning, but some operators cannot be overloaded.
    From your textbook
    “No new operators can be created, only existing operators can be overloaded.”
Test yourself: 6 questions

Polymorphism in C++

Definition

Polymorphism
Polymorphism is the ability of a message or function to take more than one form.

In C++, function overloading and operator overloading provide polymorphism. Overloading gives one name two or more distinct meanings. The compiler selects the appropriate overloaded function or operator by comparing the call's arguments with the definitions.

Definition

Function signature
A function signature is the number and types of the function's parameters.

Definition

Overload resolution
Overload resolution is the process of selecting the most appropriate overloaded function or operator.

Remember: Overloaded functions share a name, but their parameter lists must differ in number or data types.

Function Overloading

Function overloading lets two or more functions in the same scope use the same name with different parameters. For example, area can be overloaded for a circle, a triangle, and a rectangle. This avoids needing a separate function name for each action.

Rules for function overloading

  • Overloaded functions must differ in the number of arguments or their data types.
  • Return types are not considered when deciding whether functions are overloaded.
  • Default arguments are not considered part of the parameter list for overloading.

Worked example

What does the overloaded print function display for print(10), print(10.10), and print("Ten")?

Try it first, then show the solution
  1. print(10) matches print(int i).
  2. print(10.10) matches print(double f).
  3. print("Ten") matches print(string c).

Answer: It displays: It is integer 10; It is float 10.1; It is string Ten.

The add example uses different parameter lists to add values. It has versions for two long values, three long values, and two float values. With inputs 3, 4, and 5, the sum is 12; with 4 and 6, it is 10; with 2.1 and 3.1, it is 5.2.

Remember: Function overloading can reduce the number of function names a programmer must remember and the number of comparisons in a program.

Constructor Overloading

Constructors are special functions of classes, so they can be overloaded. A class can have several constructors with different signatures. This gives flexibility in creating different types of objects.

Constructors in the add class

ConstructorWhat it does
add()Sets num1, num2, and sum to 0.
add ( int s1, int s2 )Sets num1 and num2 from the arguments and sets sum to 0.
add (add &a)Copies num1 and num2 from object a and sets sum to 0.

In the example, object a uses the constructor without parameters, b uses the parameterized constructor, and c uses the copy constructor. The program reads 20 and 30 for a, then calculates each object's sum. The sums for a, b, and c are 50, 30, and 30.

Remember: When a class has multiple constructors, pass the required arguments when creating an object.

Operator Overloading

Definition

Operator overloading
Operator overloading gives an operator additional, user-defined functionality.

Operators such as +, ++, -, +=, -=, *, <, and > can be overloaded. For example, + can be given meanings for different data types, such as addition for integers or concatenation for strings. The syntax uses the Operator keyword, an operator symbol, an argument list, and a function body.

Operators that cannot be overloaded

  • Scope operator (::)
  • Sizeof
  • Member selector (.)
  • Member pointer selector (✳)
  • Ternary operator (?:)

Remember: Most operators can be overloaded, but the listed operators cannot.

Rules for Function Overloading

Definition

Function overloading
Function overloading gives the same function name more than one distinct meaning.

Overloaded functions share a name but have different signatures. A signature is the function’s argument list, including the number and type of arguments. Ordinary functions and member functions can both be overloaded.

Rules to remember

  • Overloaded functions must differ in their signatures.
  • The return type does not help distinguish overloaded functions.
  • Two functions cannot differ only because one uses a reference parameter and the other uses a normal, call-by-value parameter.
  • Default arguments do not count as a difference when overloading functions.
  • Constructors can be overloaded, but destructor functions cannot.

Operator Overloading

Definition

Operator overloading
Operator overloading gives an operator a special meaning for objects of a class.

The overloaded operator is defined using the keyword operator followed by the operator symbol. For example, + can be given a meaning for adding complex numbers or joining strings.

Operators that cannot be overloaded

  • Scope resolution operator (::)
  • sizeof ()
  • Conditional operator ( ?: )
  • Member selection (.)
  • Member pointer selector (✳)

Restrictions on operator overloading

  • An operator’s precedence and associativity cannot be changed.
  • New operators cannot be created. Only existing operators can be overloaded.
  • The original procedure of an operator cannot be redefined. An overloaded operator adds meaning for other uses, but does not change how integers are added.
  • Overloaded operators cannot have default arguments.
  • For an overloaded binary operator, the left-hand object must belong to the relevant class.

Worked example

Add the complex numbers 3+4i and 5+8i using the overloaded + operator.

Try it first, then show the solution
  1. The + operator adds the real parts: 3 + 5.
  2. It adds the imaginary parts: 4 + 8.
  3. The operator returns a complex number containing those two sums.

Answer: 8+12i

Worked example

Join the strings COMPUTER and SCIENCE using the overloaded + operator.

Try it first, then show the solution
  1. The overloaded operator joins the second string to the first string.
  2. The joined string is displayed.

Answer: COMPUTERSCIENCE

Polymorphism and Overloading

In C++, polymorphism is achieved through function overloading and operator overloading. Overloading means that one name can have two or more distinct meanings.

Two forms of overloading

Function overloadingOperator overloading
One function name is used with different argument signatures.An operator is given a special meaning for objects.
The argument list determines which function is used.The operator is defined with the keyword operator and its symbol.

Remember: For function overloading, compare the argument signatures; the return type alone cannot distinguish functions.

Constructor Overloading in the Deposit Class

The Kitty Bank starts with Rs500. The Deposit class stores the amount in a data member named amount. Its constructors determine how much money is added.

The three constructors

  • A constructor without a parameter adds no amount.
  • A constructor with an amount parameter adds that amount.
  • A constructor that adds an equal extra amount deposits twice the amount supplied.

These constructors give different ways to create a Deposit object. After creating an object, display the final amount in the Kitty Bank.

Remember: The initial amount is Rs500; the selected constructor decides what is added to it.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is polymorphism in C++?

Polymorphism is the ability of a message or function to take more than one form. In C++, function overloading and operator overloading provide polymorphism by allowing a function name or operator to have different meanings.

What is function overloading in C++?

Function overloading means two or more functions in the same scope share a name but have different parameter lists. The functions must differ in the number of arguments or their data types. The compiler selects the suitable function by comparing the call's arguments with the definitions.

What are the rules for function overloading?

Overloaded functions must differ in the number of arguments or their data types. Their return types are not considered when deciding whether functions are overloaded. Default arguments are also not considered part of the parameter list for overloading.

What is constructor overloading?

Constructor overloading means a class has more than one constructor with different signatures. It lets objects of the class be created in different ways, such as with no arguments, with parameters, or by copying another object.

Which operators cannot be overloaded in C++?

The operators that cannot be overloaded are the scope operator (::), Sizeof, member selector (.), member pointer selector (✳), and ternary operator (?:).

What is function overloading?

Function overloading gives one function name more than one distinct meaning. The overloaded functions have different signatures, which means their argument lists differ in the number or type of arguments.

Show 6 more questions

What is a function signature in C++?

A function signature is the function’s argument list. It includes the number and types of the arguments. Overloaded functions use the same name but must have different signatures.

Can return type alone be used to overload a function?

No. The return type does not help distinguish overloaded functions. Overloaded functions must differ in their signatures, such as by having a different number or type of arguments.

What is operator overloading?

Operator overloading gives an existing operator a special meaning for objects of a class. It is defined using the keyword operator followed by the operator symbol. For example, + can add complex numbers or join strings.

What is the initial amount in the Kitty Bank?

The Kitty Bank begins with an initial amount of Rs500. The Deposit class stores the amount in a data member named amount. The selected constructor determines whether any additional amount is added.

What does the Deposit constructor without a parameter do?

The Deposit constructor without a parameter adds no amount to the Kitty Bank. The initial amount remains Rs500. The final amount should be displayed after creating the Deposit object.

What happens when an amount is added using the constructor that deposits an equal extra amount?

The constructor adds an extra amount equal to the amount supplied. So, the supplied amount is deposited along with an additional equal amount, and the final Kitty Bank amount is then displayed.

Book back answers

14 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

SECTION – A Choose the correct answer

  1. Which of the following refers to a function having more than one distinct meaning?

    • (A) Function Overloading
    • (B) Member overloading
    • (C) Operator overloading
    • (D) Operations overloading
    Try it, then show the answer

    (A) Function Overloading

    An overloaded function has more than one distinct meaning.

    From the book: “an ‘overloaded function’ refers to a function having more than one distinct meaning”

  2. Which of the following reduces the number of comparisons in a program ?

    • (A) Operator overloading
    • (B) Operations overloading
    • (C) Function Overloading
    • (D) Member overloading
    Try it, then show the answer

    (C) Function Overloading

    The chapter notes that function overloading reduces the number of comparisons in a program.

    From the book: “Function overloading is not only implementing polymorphism but also reduces the number of comparisons in a program”

  3. void dispchar(char ch=’$’,int size=10) for(int i=1;i<=size;i++) cout<<ch; How will you invoke the function dispchar() for the following input? To print $ for 10 times

    • (A) dispchar();
    • (B) dispchar(ch,size);
    • (C) dispchar($,10);
    • (D)dispchar(‘$’,10 times);
    Try it, then show the answer

    (A) dispchar();

    Both parameters have default values, so calling the function without arguments uses '$' and 10.

    From the book: “The default arguments of overloaded functions are not considered as part of the parameter list in function overloading.”

  4. Which of the following is not true with respect to function overloading?

    • (A) The overloaded functions must differ in their signature.
    • (B) The return type is also considered for overloading a function.
    • (C) The default arguments of overloaded functions are not considered for Overloading.
    • (D) Destructor function cannot be overloaded.
    Try it, then show the answer

    (B) The return type is also considered for overloading a function.

    Return type is not considered when deciding whether functions are overloaded; they must differ in argument number or types.

    From the book: “The return type of overloaded functions are not considered for overloading same data type”

All 14 book back answers →

Practice

Question 1 of 6Score 0

What must differ between functions that share an overloaded name?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is function overloading?2 marks

Function overloading is when two or more functions in the same scope share a name but have different parameters.

How does the compiler choose which overloaded function to use?2 marks

It compares the argument types in the function call with the parameter types in the definitions and selects the most appropriate match.

State two rules for function overloading.2 marks

Overloaded functions must differ in the number or data types of their arguments. Their return types do not distinguish them, and default arguments are not considered part of the parameter list.

Give two restrictions on operator overloading.2 marks

The precedence and associativity of an operator cannot be changed. New operators cannot be created; only existing operators can be overloaded.

Watch out

  • Do not assume that different return types alone make two functions overloads.
  • Do not assume operator overloading can change an operator's precedence or create a new operator.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →