Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Polymorphism

Polymorphism

The term Polymorphism means the ability to take many forms. It occurs if there is a hierarchy of classes which are all related to each other by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

The + operator in C++ is used to perform two specific functions. When it is used with numbers (integers and floating-point numbers), it performs addition.

int a = 5;
int b = 6;
int sum = a + b; // sum = 11

And when we use the + operator with strings, it performs string concatenation.

string firstName = "Samad ";
string lastName = "Sharma";
// name = "Samad Sharma"
string name = firstName + lastName;

In C++ polymorphism is mainly divided into two types:

  • Compile time Polymorphism
  • Runtime Polymorphism

Function Overloading

In C++, we can use two functions having the same name if they have different parameters (either types or number of arguments).

And, depending upon the number/type of arguments, different functions are called.

// C++ program to overload sum() function

#include <iostream>
using namespace std;

// Function with 2 int parameters
int sum(int num1, int num2) {
return num1 + num2;
}

// Function with 2 double parameters
double sum(double num1, double num2) {
return num1 + num2;
}

// Function with 3 int parameters
int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}

int main() {
// Call function with 2 int parameters
cout << "Sum 1 = " << sum(5, 6) << endl;

// Call function with 2 double parameters
cout << "Sum 2 = " << sum(5.5, 6.6) << endl;

// Call function with 3 int parameters
cout << "Sum 3 = " << sum(5, 6, 7) << endl;

return 0;
}

It’s a compile-time polymorphism because the compiler knows which function to execute before the program is compiled.

Operator Overloading

In C++, we can overload an operator as long as we are operating on user-defined types like objects or structures.
We cannot use operator overloading for basic types such as int, double, etc.

Operator overloading is basically function overloading, where different operator functions have the same symbol but different operands.
And, depending on the operands, different operator functions are executed.

Syntax

To overload an operator, we use a special operator function.

class className {
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};
  • returnType is the return type of the function.
  • operator is a keyword.
  • symbol is the operator we want to overload. Like: +, <, -, ++, etc.
  • arguments is the arguments passed to the function.

Since operator overloading allows us to change how operators work, we can redefine how the + operator works and use it to add the complex numbers of c1 and c2 by writing the following code:

#include<iostream> 
using namespace std;

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

// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}

Try the following example in the editor below.

Your task is to create two functions with same name “compute” and return type int. One will take two integer parameters and return absolute difference between them. Other will take one integer parameter and return the square of that number.

Start solving Polymorphism on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test