Overloading and Overriding in C++

Sohini Dhar
4 min readAug 6, 2021

--

Features of C++

This is a very common interview question in case you are interviewing for a C++ developer role. We will see in the next few sections what overloading and overriding actually mean. I will try and explain the differences between them with some C++-style code.

Function Overloading

In case we have multiple methods/functions with the same name but different parameters, then they are called overloaded functions/methods. I will probably try to give the simplest example of the addition of two numbers.

#include <iostream> class Addition
{
public:
void add(int a, int b)
{
int c = a+b;
std::cout<< "Integer addition : "<< c << "\n";
}
void add(float a, float b)
{
float c = a+b;
std::cout<< "Float addition : "<< c << "\n";
}
void add(double a, double b)
{
double c = a+b;
std::cout<< "Double addition : "<< c << "\n";
}
};
int main()
{
Addition a{};
a.add(4,6);
a.add(8.56f,2.98f);
a.add(7.84,9.34);
return 0;
}

Output

Integer addition : 10
Float addition : 11.54
Double addition : 17.18

Note that the compiler understands which method of add it needs to call while the user passes the numbers, if we specifically input a number with a decimal point, it is considered double by default. Thus, for the third input, the control goes to the loop with parameters of type double. Now, suppose you provide input as:

a.add(8.56f, 8.9);This line will result in an error as the second input is ambiguous, and we do not have any method which reads float as the first parameter and double as the second.

Operator Overloading

Consider, we have a class called Complex Number, which contains a real and imaginary part and we want to add two such numbers, by regular addition, this is not possible, however, by operator overloading, this can be done. The ‘+’ operator can be given a different meaning for the class of Complex Numbers. One important piece of information here is, not all operators can be overloaded. The operators which cannot be overloaded are : dot(‘.’) operator, scope-resolution(‘::’) operator, ternary(‘?:’) operator, point to member(‘.*’), sizeof and type id operators. Let us now see an example where the operator ‘<’(less than) is overloaded. Let’s consider we have a class called Time which has three members : hours, minutes and seconds. We are given two such objects and asked to compare which time of a day is smaller. We can create a very small implementation of this through our code.

#include <iostream>class Time
{
public:
Time(int h, int m, int s) : hr(h), min(m), secs(s)
{
}
bool operator<(Time& t1)
{
int timeInSecs = hr*3600 + min*60 + secs;
int newTimeInSecs = t1.hr*3600 + t1.min*60 + t1.secs;
return timeInSecs < newTimeInSecs;
}
void display()
{
std::string output{};
if(hr>=0 && hr<9)
{
output+='0'+std::to_string(hr)+':';
}
else
{
output+=std::to_string(hr)+':';
}
if(min>=0 && min<9)
{
output+='0'+std::to_string(min)+':';
}
else
{
output+=std::to_string(min)+':';
}
if(secs>=0 && secs<9)
{
output+='0'+std::to_string(secs);
}
else
{
output+=std::to_string(secs);
}
std::cout << "Time in 24hr format : "<< output <<"\n";
}
private:
int hr;
int min;
int secs;
};
int main()
{
Time t1(13, 7, 25);
t1.display();
Time t2(17, 20, 50);
t2.display();
if(t1 < t2)
{
std::cout << "The smaller time is : t1 \n";
}
else
{
std::cout << "The smaller time is : t2 \n";
}
return 0;
}

Output

Time in 24hr format : 13:07:25
Time in 24hr format : 17:20:50
The smaller time is : t1

So you can see how the less than operator can be overloaded and can be used to compare two Time objects.

Function Overriding

This occurs when a method of the base class is overridden by a method of derived class. Let us try to understand this by an example.

#include <iostream>class Base
{
public:
Base()
{
std::cout << "Hello from Base\n";
}
virtual void printName()
{
std::cout << "I am in Base Class\n";
}
void printHello()
{
std::cout << "Hello from Base Class\n";
}
virtual ~Base()
{
std::cout << "Base destructor\n";
}
};
class Derived : public Base
{
public:
Derived()
{
std::cout << "Hello from Derived\n";
}
void printName()
{
std::cout << "I am in Derived class\n";
}
void printHello()
{
std::cout << "Hello from Derived Class\n";
}
~Derived()
{
std::cout << "Derived destructor\n";
}
};
int main()
{
Base* b = new Derived();
b->printName();
b->printHello();
delete b;

return 0;
}

Output

Hello from Base
Hello from Derived
I am in Derived class
Hello from Base Class
Derived destructor
Base destructor

Notice how the b->printName() method calls the derived class because in the base class we have the word call virtual so at run time we bind the method. However, since printHello() method does not have the keyword virtual above it in the base class, the output is the print statement from the base class as pointer b is a pointer of base class. printName method shows how a method of base class with same name as Derived class is overridden since b is initialized to Derived class. Also, we use a virtual destructor so that the we clean up the memory of both the derived and base class. If we did not have the virtual keyword before the destructor, the line “delete b” would invoke the print statement inside the Base class destructor only.

Hope you got some idea on overloading and overriding methods of C++. This is quite a long article, however, if you read it and follow the code written as examples you should be able to understand the idea behind the concepts. In case you need more information, we always have Google.

Let me know what you think and do not forget to like if you enjoyed reading!

Happy C++ Coding!! Please follow, like, comment, share if you like this.

--

--

Sohini Dhar
Sohini Dhar

Written by Sohini Dhar

Programmer, Traveler, Adventurer!!!!

No responses yet