C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Overload

C++ Overload

shape Definition

Allotting multiple meanings to a single name in the same scope which varies in type and number of parameters is called as Overloading. The concept of CPP Overload comes under compile time polymorphism which will be discussed in coming chapters. The process of compiler taking the best decision of which function or operator has to be called based on the criteria provided is called as CPP Overload resolution.

Function overloading in C++

shape Description

The functions with same name having multiple definitions but varies in return type and/or arguments list is called as function overloading. CPP Overload function must vary in number of arguments even if they does not differ in return type.

shape Example

Possibilities for function overload

There are two ways to overload a function. Function overloading varying in number of arguments : Two functions with same names but with different no. of parameters of the same type are used in this type of function overloading. For example, in the below mentioned example two sum() functions are mentioned to return sum of two and three integers which is nothing but the sum() is overloaded and depends on no.of arguments. [c] int splessons (int a, int b) { cout << a+b; } int splessons(int a, int b, int c) { cout << a+b+c; } [/c] Function overloading varying in type of arguments : Two or more functions with same name and same number of parameters, but parameter type is different. For example, let there be two sum() functions, first has two integer arguments and second one has two double arguments. [c] int splessons(int a,int b) { cout << a+b; } double splessons(double a,double b) { cout << a+b; } [/c]

shape Example

[c] #include <iostream> using namespace std; /* Function arguments are of different data type */ int mul(int, int); float mul(float, float); int main() { int i, j, a; float k, l, b; cout << "Enter two integers\n"; cin >> i >> j; a = mul(i, j); cout << "Multiplication of given integers is : " << a << endl; cout << "Enter two floating point numbers\n"; cin >> k >> l; b = mul(k, l); cout << "Multiplication of given floats is : " << b << endl; return 0; } int mul(int a, int b) { int mul; mul = a * b; return mul; } float mul(float a, float b) { float mul; mul = a * b; return mul; }[/c] Output [c] Enter two integers 8 9 Multiplication of given integers is : 72 Enter two floating point numbers 7.1 18.6 Multiplication of given floats is : 132.06 [/c]

Operator overloading in C++

shape Description

Operator overloading helps in defining an operator with a new meaning that is useful to perform operations on user-defined classes.

Implementation of Operator Overloading

Implementation of functions like below will result in Operator Overloading. If the left operand is an Object of that class then it is operator overloading member function. But if the left operand is different, then Operator overloading function must be a non-member function. Operator overloading friend function should have the access to the private and protected members of class.

shape Syntax

returntype  classname :: operator  operator_symbol( argument list) { body of function; }

Rules for Operator Overloading

  1. Original syntax of an operator cannot be changed.For example, + can be used for concatenation but not for subtraction.
  2. No chance for creating new operators.
  3. The rules of the operator cannot be changed i.e more operands cannot be used neglecting the operator usage.
  4. The operator which is overloaded should have atleast a single operand to perform the operation.

shape More Info

Operator overloading cannot be performed on

shape Example

[c] #include <iostream> using namespace std; class splessons { private: int count; public: splessons():count(10){ } void operator ++() { count=count+1; } void Disp() { cout << "Count: " << count; } }; int main() { splessons s; ++s; /* operator function void operator ++() is called */ s.Disp(); return 0; }[/c] Output [c] Count: 11[/c]

Examples on various operators

shape Examples

Increment operator [c] //Example for operator overloading using increment operator #include <iostream> using namespace std; class subject { private: int a; public: subject(): a(7) { } subject operator ++() { subject tempo; ++a; //incrementing value tempo.a=a; return tempo; } void Disp() { cout<< "a="<< a <<endl; } }; int main() { subject obj, obj1; obj.Disp(); obj.Disp(); obj=++obj; obj.Disp(); obj.Disp(); return 0; }[/c] Output [c] a=7 a=7 a=8 a=8[/c] Relational operator [c]//Example for overloading using relational operators #include<iostream> using namespace std; class relational { int i; public: int input() { cout << "enter any value:" << endl; cin >> i; } int disp() { cout << i << endl; } int operator < (relational s) { return(i < s.i); } }; int main() { int t; relational o1,o2; cout << "For first object " << endl; o1.input(); cout << "For second object "<< endl; o2.input(); t=o1 < o2; if(t==0) cout<< "first object is bigger than second object" << endl; else cout<< "first object is smaller than second object"<< endl; return(t); }[/c] Output [c] For first object enter any value: 8 For second object enter any value: 6 first object is bigger than second object [/c]

Summary

shape Key Points

CPP Overload chapter draws out following important points.
  • Having multiple definitions to a single function name is overloading.
  • Function overloading and operator overloading are the types.
  • Function overloading vary in type and no.of arguments.
  • Operator overloading overloads the operator assigning user-defined meaning to it.

shape Programming Tips

Overloading must not be confused with polymorphism forms where the method is chosen dynamically.