blog

Home / DeveloperSection / Blogs / Overloading and Overriding in C++

Overloading and Overriding in C++

priyanka kushwaha2902 30-Jan-2015

 In this blog, I’m explaining about Overloading and Overriding in C++

 

Overloading  

The same  method name with different  type  of parameters in a class is known  as method overloading.

Example:
class  overload
{
public:
             overload();
             int add(inta,intb)
             {
                         returna+b;
             }
             float add(floata,intb)
             {
                         returna+b;
             }
 
~ overload();
 
private:
 
};
int main(array<System::String ^> ^args)
{
                overload obj;
        while (true)
                {
                                int exec;
                                cout<<"\n1)For Integer Addition\n";
                                cout<<"2)For Float Addition\n";
                                        cout<<"4)exit\n";
                                cout<<"Enter any option";
                              cin>>exec;
                                 switch (exec)
                                 {
                                 case 1:
                                                 float a,b,res;
                                                 cout<<"Enter two nos.";
                                                 cin>>a>>b;
                                                  res=obj.add(x,y);
                                                  obj.display(res);
                                                break;
                                 case 2:
                                                 int x,y,res1;
                                                  cout<<"Enter two nos.";
                                                  cin>>x;cin>>y;
                                                  res1=obj.add(x,y);
                                                  Driobj.display(res1);
                                                 break;
                               
                                 case 4:
                                                 exit(0);
                                                 break;
                                 default:
                                                 break;
                                 }
     }
               
                Console::ReadKey(0);
    return 0;
}

 

Overriding

Overriding means same method name with same signature in different class.

Example:
class  overload
{
public:
             overload();
             int add(int a,int b)
             {
                         return a+b;
             }
             float add(float a,int b)
             {
                         return a+b;
             }
             void display(float res)
             {
                         cout<<res;
             }
 
~ overload();
 
private:
 
};           
class DriveClass:overload
{
public:
void add(char str[30],char str1[30])
{
                        char result[100];
                        int j=0;
                for(int i=0;str[i]!='\0';i++)
                        {
                                    result[j]=str[i];
                                    j++;
                        }
                        for(int i=0;str1[i]!='\0';i++)
                        {
                                    result[j]=str1[i];
                                    j++;
                        }
                        for(int i=0;result[i]!='\0';i++)
                                    cout<<result[i];
                                   
            }
void  display(int res)
             {
                         cout<<res;
             }
            DriveClass();
            ~DriveClass();
private:
};
 
DriveClass::DriveClass()
{
}
 
DriveClass::~DriveClass()
{
}
 
 overload:: overload
()
{
}
overload::~ overload()
{
}
 
int main(array<System::String ^> ^args)
{
            overload obj;
            DriveClass Driobj;
            while (true)
            {
                        int exec;
                        cout<<"\n1)For Integer Addition\n";
                        cout<<"2)For Float Addition\n";
                        cout<<"3)for String Additon";
            cout<<"4)exit\n";
                        cout<<"Enter any option";
           cin>>exec;
                         switch (exec)
                         {
                         case 1:
                                     float a,b,res;
                                     cout<<"Enter two nos.";
                                     cin>>a>>b;
                                     res=obj.add(a,b);
                                     obj.display(res);
                                    break;
                         case 2:
                                     int x,y,res1;
                                      cout<<"Enter two nos.";
                                      cin>>x;cin>>y;
                                      res1=obj.add(x,y);
                                      Driobj.display(res1);
                                     break;
                         case 3:
                                     
                                     char str[30],str1[30],str2[100];
                                     cout<<"Enter a string";
                                     cin>>str;
                                     cout<<"Enter another string";
                                     cin>>str1;
                                      Driobj.add(str,str1);
                                     break;
                         case 4:
                                     exit(0);
                                     break;
                         default:
                                     break;
                         }
     }
           
            Console::ReadKey(0);
    return 0;
}

Updated 30-Jan-2015

Leave Comment

Comments

Liked By