Function Overloading vs Overriding in C++

Dantusaikamal
4 min readJan 3, 2021
A laptop with code on screen. (Just for display purpose and not C++ code)
Photo by James Harrison on Unsplash

Before we jump right into Function overloading and Function Overriding, there are a few pre requisites that would help you understand the topics much clearly.

  • C++ basics — Syntax, data types, OOP features
  • Understanding of Functions in C++
  • Function prototypes

If you are familiar with these topics already, you can directly skip to the Function Overloading and Function Overriding topics directly.

Let us discuss the Prerequisites first.

C++ Basics:

When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other’s methods. Let us now briefly look into what a class, object, methods, and instant variables mean.

  • Object − Objects have states and behaviors. Example: A dog has states — color, name, breed as well as behaviors — wagging, barking, eating. An object is an instance of a class.
  • Class − A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support.
  • Instance Variables − Each object has its unique set of instance variables. An object’s state is created by the values assigned to these instance variables.

Here’s the famous hello world program in CPP:

C++ Datatypes:

C++ offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types −

Type = Keyword

Boolean - bool

Character - char

Integer - int

Floating point - float

Double floating point - double

Valueless - void

Wide character - wchar_t

Object Oriented Programming in C++:

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.

Object-oriented programming has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the C++ code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

Tip: The “Don’t Repeat Yourself” (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.

Functions in C++

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Function Prototypes in C++

In C++, the code of function declaration should be before the function call.

However, if we want to define a function after the function call, we need to use the function prototype.

In simple terms, we can not declare our functions after main function.

If we want to declare a function after main function, Function prototyping must be used to call a function.

Syntax:

returnType functionName(dataType1, dataType2, …);

Now that we finished discussing the Prerequisites, let us jump right into Function Overloading and Function overriding.

Function Overloading in C++

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.

Function overloading can be considered as an example of polymorphism feature in C++.

In other words, we can have multiple functions of different return types but with same name.

Take a look at the example below:

Here, we are having 3 functions with the same name.

If we pass an integer value to print function, the value is assign to variable i.

Similarly if we pass a double value to print, the value is assigned to variable f.

Function Overloading rules:

Since we use Function Overloading to specify multiple definitions of the same function(same name),

We can overload a function using different ways:

void print(); //Without arguments

void print(int a);

void print(float a); //Change in data types

void print(int a, int b); //Change in number of variables

void print(int a, double b);

void print(double a, int b);// Change in order

Function Overriding in C++

In Function overloading, we discussed the possible ways to define a function in multiple ways and pass different parameters.

But if there is a collision incase we pass the same parameters to function with same name and same data types, then it is said to be function overriding.

In this program, void display() is declared twice with same arguments(null) and same return type(void).

So there is a collision between first display statement and second display statement.

In such collision cases, always the second function overwrites the first function.

That is, the content of first display function is not considered by default.

We can access the base function using Inheritance

Key differences between Function overloading and overriding.

Thank you!

Here’s a link for the video recording of my presentation on the same topic: https://www.youtube.com/watch?v=MZwy-KmwMu8

About me:
I’m Sai Kamal, currently pursuing my Bachelor’s in CS. Check out my Portfolio to know more about me : https://dantusaikamal.github.io/

Example programs used in the article are available at:

https://github.com/Dantusaikamal/Object-Oriented-Programming-using-CPP

References:

Let Us C++ Textbook by Yashavant Kanetkar

https://www.geeksforgeeks.org/function-overloading-vs-function-overriding-in-cpp/

--

--

Dantusaikamal

Hi, I’m Sai kamal, a Full Stack Developer 🚀 from Hyderabad, India. Currently a sophomore student, a tech enthusiast and an ML geek with a penchant for writing!