Polymorphism in C++ | for Interviews | with Codes and Examples

Harsh Chandwani
5 min readMay 16, 2023

--

So, hey lovely people, Harsh Chandwani, this side, a Pre-Final year student, who loves to explore, travel, and meet new people. I have been busy with interviews for like last 2 weeks, for some bigger firms like Juspay, Scaler, and many more.

The one most common thing among all the interviews was the interviewer asking Object Oriented Programming in my own comfortable language.

Now, this is Part 2 of OOPs in C++ for Interviews. Here is the first part.

Now, Polymorphism is one of the most asked things in OOPs, as it is used a lot in companies, you don't need to know what's up at a higher level, but the basics are important.

Question №1

What is Polymorphism?

In simple words, we can define polymorphism as “Having many forms” but the interviewer knows the meaning of the word polymorphism, he wants to what is used and why polymorphism is.

So, here is an answer…

In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A man at the same time is a father, a husband, and an employee. So the same person exhibits different behavior in different situations. This is called polymorphism.

(via GeeksforGeeks)

NOTE: I know, I know there are 2 types of this, but the interviewer does not ask for this till now, and do not answer the questions he hasn't asked.

Question №2

What are the types of Polymorphism?

(Note: whenever this question is asked, ask the interviewer, if you can use the compiler to write the code, as it will be easier to explain, than do without it, he/she will definitely agree)

So, Polymorphism is of 2 types:

  1. Compile time: Compile-time polymorphism means the binding is occurring at compile time. It can be achieved through static binding.
  2. Run time: Run-time polymorphism is where at run time we came to know which method is going to invoke. It can be achieved through dynamic binding.

Let's code Compile time polymorphism…

We will be doing Function Overloading by creating a class Animal and then a class Dog which inherits properties and has a function name the same as of class Animal.

#include <bits/stdc++.h>
using namespace std;
//Compile Time Polymorphism
//Static Polymorphism
//It OVERLOADS
class Animal{
public:
void animalSound(){
cout<<"Animal Sound"<<endl;
}
};
class Dog : public Animal{
public:
void animalSound(){
cout<<"Dog Sound"<<endl;
}
};

int main()
{
//we will now be creating a oject for animalSound
Animal A;
A.animalSound();
Dog D;
D.animalSound();

}

Now, after this, we will overload an operator,

this is Operator Overloading

#include <bits/stdc++.h>
using namespace std;
//Compile Time Polymorphism
//Operator OVERLOADS
//Compile Time Polymorphism
class TestClass {
private:
int count;
public:
// Constructor to initialize count to 5
TestClass() : count(5) {}
void operator --() {
count = count - 3;
}
void Display() {

cout << "Count: " << count; }
};


int main()
{

//this is operator overloading
TestClass tc;
--tc;
tc.Display();
return 0;
}

Question №3

Now, can you please overload a + (plus operator)?

This is how we overload a + operator.


// Operator Overloading
#include <iostream>
using namespace std;

class Complex {
private:
int real, imag;

public:
//Contructor
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 << '\n'; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}

Use, the above code only when the interviewer asked to overload a +/- operator, as overloading them is a difficult task and you may not be able to successfully run the code in the editor.

Now, as we have completed Compile Time overloading, we will be moving to Runtime Overloading.

In this, we will be creating a class Shape, having a virtual function draw, then we create a class Rectangle, inherited from Shape, having the same function draw.

#include <iostream>

using namespace std;
class Shape{
public:
//Virtual function, when we wants to re intialize a function in derived class
virtual void draw(){
cout<<"Drawing Shapes"<<endl;
}
};
class Rectangle: public Shape{
public:
//Reintialize a function draw
void draw(){
cout<<"Rectangle Drawn"<<endl;
}
};
int main()
{
//We create create s object for shape using pointer
Shape* s;
//Creating object rec for class Rectangle
Rectangle rec;
//pointing to rec
s = &rec;
s->draw();

return 0;
}

This is Runtime Overloading, as it does all of the work in runtime or in dynamic time.

Question №4

When to use compile time and when to use run-time polymorphism?

Compile-time polymorphism is used when you have multiple methods with the same name but different parameters or signatures and different return types within the same class. This is known as method overloading. The compiler determines which method to call based on the method signature.

On the other hand, run-time polymorphism is used when you have a method in a child class with the same name, return type, and parameters as a method in its parent class. This is known as method overriding. The method to call is determined at runtime based on the type of the object that invokes the method.

I think we have done enough for this article and covered all the major questions and codes you need in your interview, now next we will cover Inheritance.

So, that’s it for today. Let me know, if I missed anything, will try to add them in the next article.

Till then, learn, help, and contribute.

Signing off

Thanks,

Harsh Chandwani (Twitter: heyy_harshh)

--

--

Harsh Chandwani

MLSS Mentee | Pre-Final Year Student | Web Developer | Speaker |