Topics    function overloading    function  overriding    function  overhiding    operator overloading   Explain function ove...

Function overloading,overriding ,overhiding and operator overloading in C++

/
0 Comments
 Topics 

  1.   function overloading 
  2.   function  overriding 
  3.   function  overhiding 
  4.   operator overloading 



 Explain function overloading 

  • Function overloading is compiled time polymorphism. 
  • Function overloading is the practice Of declaring the same function with different signatures. 
  • The same function name will be used with a different number Of parameters and parameters Of different type. 
  • Overloading Of functions with different return types is not allowed. 
  • The compiler identifies which function should be called out Of many using the type and number Of arguments. 
  • A function is overloaded when the same name is given to different functions. 
  • However, the two functions with the same name must differ in at least one Of the following, 

        a) The number Of parameters
        b) The data type Of parameters
        c) The order Of appearance
Example:
#include<iostream.h>
#include<conio.h>
class Math
{
public:
void Add(int x,int y) //funtion 1
{
cout<<"sum ="<<x+y<<endl;
}
void Add(float x,float y) //function 2
{
cout<<"sum ="<<x+y<<endl;
}
void Add(int x,int y,,int z) // function 3
{
cout<<"sum ="<<x+y+z<<endl;
}
}
void main()
{
Math m;
m.Add(10,20);
m.Add(10.5,20.5);
m.Add(10,20,40);
getch();
}

Output:
sum=30
sum=31.0
sum=70

What is overriding in C++? 

  • If base class and derived class have member functions with same name and arguments. 
  • If you create an object of the derived class and write code to access that member function then, the member function in the derived class is only invoked. 
  • Means the member function of derived class overrides the member function of the base class. 
  • This is called function overriding or method overriding in C++. 
Example:
#include<iostream.h>
#include<conio.h>
class A{
public:
void fun()
{
cout<<"parent function";
}
}
class B:public A{
public:
void fun()
{
cout<<"base function"; //overriding function
}
}
void main()
{
A a;
B b;
b.fun();// in this only B class function will run insteded of Class A function this is know as overriding
getch();
}

Output:
base function


  • If we want to access parent class function then we need parent class object in this program. In function is not override then we can access parent class function using the chlid class object.
  • Remember this if we class any function using child class object then first compiler search that function in child class .if it is found in child class then the compiler won't go on parent and if it does not find in child class then it will go in the parent class.




What is meant by method overhiding?
 
  • Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case, the method of superclass is hidden by the subclass. It signifies that: The version of a method that is executed will NOT be determined by the object that is used to invoke it. In fact, it will be determined by the type of reference variable used to invoke the method
  • Similarly, a method in a subclass can hide a method in its base class, which will hide this base class method, and expose the subclass method implementation instead.
  • Method overriding gives a programmer the ability to write an improved version of the method of the base class without affecting the message passed by the client programs, which uses the derived class.


Explain operator overloading?
  • •Operator overloading is compiled time polymorphism. 
  • The operator overloading provides the mechanism to perform operations on the user-defined data type. 
  • We can give special meaning to any operators in which program it is implemented. 
  • Mostly operator overloading is used to perform a task like addition, subtraction, multiplication etc on two or more object of the same call.
  • Rules for operator overloading 

  1. The only existing operator can be overloaded. 
  2. The overloaded operator must have at least one operand that is the user-defined type. 
  3. We cannot change the basic meaning and syntax of an operator. 
  4. We cannot use friend function to overload certain operators. However, the member function can be used to overload them. 
  5. Unary operators, overloaded by means of a member function, take no explicit argument s and return no explicit value, but, those overloaded by means of a friend function, take one reference argument. 
  6. Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments. 
  7. When using binary operators overloaded through a member function, the left-hand operand must be an object of the relevant class. 
We cannot overload.
operator  Name
. and .*    class member access operator
::             scope resolution operator
sizeof      size operator
?:            conditional operator

Example:

#include<iostream>
#include<conio.h>
class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {
real = r;   imag = i;
}
     
    // This is automatically called when '+' is used with
    // between two Complex objects
    Complex operator + (Complex const &obj) {
         Complex res;
         res.real = real + obj.real;
         res.imag = imag + obj.imag;
         return res;
    }
    void print() { cout << real << " + i" << imag << endl; }
};
void main()
{
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2; // An example call to "operator+"
      Complex c3 =c1.operator +(c2); // we can also use this  answer will be same
    c3.print();
}




You may also like

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts