EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Overloading in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Overloading in C++

Overloading in C++

What is Overloading in C++?

To achieve compile-time polymorphism, the C++ programming language provides an overloading feature that allows to an overload of the two or more methods that have the same name but different parameters. It can be performed by using function overloading and operator overloading. Function overloading overloads the two or more functions with the same name but different parameters whereas operator overloading overload operators provide the special meaning to the user-defined data types.  This feature allows using built-in operators on user-defined types. Operator overloading simplifies the code by redefining the functionality as per the user requirements. In this article we will focus on both the function overloading and the operator overloading, we will see the details of it and how it is used in C++.

C++ allows writing flexible and easy to understand code using the concept know as Overloading. It allows achieving different functionality within the existing code with very minimal changes thus reducing the duplicate code. Basically, there are mainly two primary types of overloading which are supported by C++.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

C++ allows us to write functions with the same name but with the difference in datatypes or in the number of arguments passed to it, this feature is known as Function Overloading in C++. This feature allows developers to define the functions with the same name within the same scope. With the same name, the functions represent the same functionality thus allowing to achieve compile-time polymorphism. The function overloading has one advantage that it improves the readability of the code.

How does Overloading work in C++?

  • Function Overloading: It allows us to define the function with the same name, but it distinguishes the functions depending upon the type of parameters passed to them or the number of parameters passed to them. So, all the functions will have the same name but will have either different datatype or will have a different number of parameters passed to it. When the function is called, the compiler will choose the function with the matching type of parameter and matching the number of arguments. Now its developers can choose which function to call according to the requirements. They can choose the appropriate function by passing the parameters following the rules.
  • Operator Overloading: It allows operators to work for user-defined data types i.e. classes. The existing operators are overloaded and given the power to operate on the user-defined class and objects. Operator overloading is achieved by defining the function with the special name. The function will have the name ‘operator’ followed by the operator symbol. We can use the operator symbol directly on the user-defined data type and perform the operation. The necessary action or operation is defined in that special function by us. By means of operator overloading, we can perform operations of different types on the same type of data type.

Types of Overloading in C++

Function Overloading can be achieved in anyways in terms of usage of parameters. When we say usage of parameters it refers to, type of parameters or count of parameters or sequence of parameters. So, function calc (int x, float y) having parameters (int x, float y) is different from a function defined as calc (float x, int y) which have different parameters with the different datatype.

There can be a number of types in which function overloading can be achieved, Let’s see the simple example of function overloading in C++.

#include <iostream>
using namespace std;
class Display {
public:
static void show (char message[] ) {
cout<< "Only Message:" << message << endl;
}
static void show (int messageId, char message[]) {
cout<< "Message with Id:";
cout << messageId << " Message:" << message << endl;
}
};
int main (void) {
Display obj;
char message[] = "Welcome";
int messageId = 10;
obj.show(message); //calling overloaded function with 1 parameter
obj.show(messageId, message);//calling overloaded function with 2 parameters
return 0;
}

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Here, we have class Display which has an overloaded method show. The show method will be called depending upon the arguments passed to it.

Output:

Overloading in c++ 1

Operator overloading can be achieved on almost all the built-in data types available in C++. There is no as such different types of operator overloading but the approaches can be different which are used to achieve operator overloading. Operators such as Unary, Binary, Relational, Assignment, etc can be overloaded in C++.

Let’s see the simple example of overloading of ++ operator. In this example instead of primitive data type, we will use ++ operator on the user-defined class object.

#include <iostream>
using namespace std;
class Bike {
private:
int height;
public:
Bike (): height (80) {} //constructor which initializes the height variable
void operator ++() {
height = height + 5;
}
void Specs () {
cout << "The height of the bike is: " <<height <<endl;
}
};
int main(void)
{
Bike bike;
bike.Specs();
++bike; //calling overloaded operator
bike.Specs();
return 0;
}

So, we have an object whose initial height will be set at 80 and will be increased by 5 when we call ++ operator over it.

Output:

Overloading in c++ 2

Rules of Overloading in C++

Below are the different C++ overloading rules as follows:

Rules for the function overloading

  • In function overloading, the function must differ in terms of data type, number or sequence of parameters. It cannot differ simply on the basis of the return type of function.

Rules for the operator overloading

  • Only built-in operators can be overloaded, the new operators cannot be overloaded.
  • There are four operators which cannot be overloaded, these are . (member selection), :: (scope resolution), .* (member selection using pointer to function) and ?: (ternary operator).
  • The overloaded operator will contain at least one operand of the user-defined data type.
  • There are certain operators that cannot be overloaded by using the friend function, but they can be overloaded as a member function.

Conclusion

So, the overloading in C++ is the unique feature which provides multiple advantages to us. There are mainly two types of overloading i.e function overloading and operator overloading. Function overloading improves the code readability thus keeping the same name for the same action. Operator overloading allows redefining the existing functionality of operators thus by giving special meaning to them. Both are very useful in programming in C++.

Recommended Articles

This is a guide to Overloading in C++. Here we discuss its working, rules, and two types of overloading in c++ i.e function overloading and operator overloading. You may also look at the following article to learn more –

  1. Overloading in PHP
  2. Copy Constructor in C++
  3. Overloading vs Overriding
  4. Overloading in Java

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More