C Overloading
C Overloading
If we create two or more members having the same name but different in number or
type of parameter, it is known as C++ overloading. In C++, we can overload:
o methods,
o constructors, and
o indexed properties
1. #include <iostream>
2. using namespace std;
3. class Cal {
4. public:
5. static int add(int a,int b){
6. return a + b;
7. }
8. static int add(int a, int b, int c)
9. {
10. return a + b + c;
11. }
12. };
13. int main(void) {
14. Cal C; // class object declaration.
15. cout<<C.add(10, 20)<<endl;
16. cout<<C.add(12, 20, 23);
17. return 0;
18. }
Output:
30
55
Let's see the simple example when the type of the arguments vary.
1. #include<iostream>
2. using namespace std;
3. int mul(int,int);
4. float mul(float,int);
5.
6.
7. int mul(int a,int b)
8. {
9. return a*b;
10. }
11. float mul(double x, int y)
12. {
13. return x*y;
14. }
15. int main()
16. {
17. int r1 = mul(6,7);
18. float r2 = mul(0.2,3);
19. std::cout << "r1 is : " <<r1<< std::endl;
20. std::cout <<"r2 is : " <<r2<< std::endl;
21. return 0;
22. }
Output:
r1 is : 42
r2 is : 0.6
When the compiler shows the ambiguity error, the compiler does not run the program.
o Type Conversion.
o Function with default arguments.
o Function with pass by reference.
o Type Conversion:
1. #include<iostream>
2. using namespace std;
3. void fun(int);
4. void fun(float);
5. void fun(int i)
6. {
7. std::cout << "Value of i is : " <<i<< std::endl;
8. }
9. void fun(float j)
10. {
11. std::cout << "Value of j is : " <<j<< std::endl;
12. }
13. int main()
14. {
15. fun(12);
16. fun(1.2);
17. return 0;
18. }
1. #include<iostream>
2. using namespace std;
3. void fun(int);
4. void fun(int,int);
5. void fun(int i)
6. {
7. std::cout << "Value of i is : " <<i<< std::endl;
8. }
9. void fun(int a,int b=9)
10. {
11. std::cout << "Value of a is : " <<a<< std::endl;
12. std::cout << "Value of b is : " <<b<< std::endl;
13. }
14. int main()
15. {
16. fun(12);
17.
18. return 0;
19. }
The above example shows an error "call of overloaded 'fun(int)' is ambiguous". The
fun(int a, int b=9) can be called in two ways: first is by calling the function with one
argument, i.e., fun(12) and another way is calling the function with two arguments, i.e.,
fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler
could not be able to select among fun(int i) and fun(int a,int b=9).
The above example shows an error "call of overloaded 'fun(int&)' is ambiguous". The
first function takes one integer argument and the second function takes a reference
parameter as an argument. In this case, the compiler does not know which function is
needed by the user as there is no syntactical difference between the fun(int) and fun(int
&).
Where the return type is the type of value returned by the function.
operator op is an operator function where op is the operator being overloaded, and the
operator is the keyword.
o Existing operators can only be overloaded, but the new operators cannot be
overloaded.
o The overloaded operator contains atleast one operand of the user-defined data
type.
o We cannot use friend function to overload certain operators. However, the
member function can be used to overload those operators.
o When unary operators are overloaded through a member function take no
explicit arguments, but, if they are overloaded by a friend function, takes one
argument.
o When binary operators are overloaded through a member function takes one
explicit argument, and if they are overloaded through a friend function takes two
explicit arguments.
C++ Operators Overloading Example
Let's see the simple example of operator overloading in C++. In this example, void
operator ++ () operator function is defined (inside Test class).
1. #include <iostream>
2. using namespace std;
3. class Test
4. {
5. private:
6. int num;
7. public:
8. Test(): num(8){}
9. void operator ++() {
10. num = num+2;
11. }
12. void Print() {
13. cout<<"The Count is: "<<num;
14. }
15. };
16. int main()
17. {
18. Test tt;
19. ++tt; // calling of a function "void operator ++()"
20. tt.Print();
21. return 0;
22. }
Output:
Output:
1. #include <iostream>
2. using namespace std;
3. class Animal {
4. public:
5. void eat(){
6. cout<<"Eating...";
7. }
8. };
9. class Dog: public Animal
10. {
11. public:
12. void eat()
13. {
14. cout<<"Eating bread...";
15. }
16. };
17. int main(void) {
18. Dog d = Dog();
19. d.eat();
20. return 0;
21. }
Output:
Eating bread...