Thursday, 28 June 2018

C++ problem Explain briefly what polymorphism is and give a short example

copyable code:
#include<iostream>
using namespace std;
class calc
{
public:
void op(int x, int y)
{
cout<<"multiplication 2 numbers using function op()";
cout<<x*y;
}
void op(int x, int y, int z)
{
cout<<"multiplication 3 numbers using function op()";
cout<<x*y*z;
}
};
int main()
{
calc c;
c.op(5,5);
cout<<endl;
c.op(6,3,4);
return 0;
}
#include<iostream>
using namespace std;
class greeting1
{
public:
void greet()
{
cout<<"hi";
}
};
class greeting2:public greeting1
{
public:
void greet()
{
cout<<" Welcome";
}
};
int main()
{
greeting1 g1;     
greeting2 g2;
g1.greet();
g2.greet();
return 0;
}
#include<iostream>
using namespace std;
class greet1
{
public:
virtual void greet()
{
cout<<"Hello base class greeting";
}
};
class greet2 : public greet1
{
public:
void greet()
{
cout<<"\nHello derived class greeting";
}
};
int main()
{
greet1 g1obj;
greet2 g2obj;
greet1 *ptr;
ptr=&g1obj;
ptr->greet(); // call base class function
ptr=&g2obj;
ptr->greet(); // call derive class function
return 0;
}
#include <iostream>
using namespace std;
class Test
{
private:
int myvalue;
public:
Test(): myvalue(10){}
void operator ++()
{
myvalue = myvalue*5;
}
void disp() { cout<<"after overloading: "<<myvalue; }
};
int main()
{
Test t;
// this calls "function void operator ++()" function
++t;   
t.disp();
return 0;
}
Polymorphism:
The ability of an object to respond differently to different messages is called as polymorphism. Thus an operator or method could be used in different ways. A single name of method or an operators gives multiple meaning.
Types of polymorphism:
  • Runtime.
  • Compile time.
  • Ad-hoc polymorphism.
Compile time polymorphism:
The compile time polymorphism can be achieved in two ways:
Method overloading
It is process of overloading a single method to perform different operations. The method name remains same in performing all operations but the parameter of the function defined gets changed with the method.
Op () method contain 2 parameters in first call and 3 parameters in second call.
using namespace std;
class calc
void op(int x, int y)
cout<<"multiplication 2 numbers using function op()";
void op(int x, int y, int z)
cout<<"multiplication 3 numbers using function op()";
int main()
calc c;
return 0;
Method overriding
A method with same name with same or different parameter is defined in both base class and derived class is called as method overriding.
#include<iostream>
using namespace std;
class greeting1
{
public:
            void greet()
            {
            cout<<"hi";
}
};
class greeting2:public greeting1
{
            public:
            void greet()
            {
            cout<<" Welcome";
            }
};
int main()
{
            greeting1 g1;     
greeting2 g2;
            g1.greet();
            g2.greet();
return 0;
}
Runtime polymorphism:
The runtime polymorphism is achieved by using a virtual function. A virtual function is declared in both base class and derived class. When a same method is defined in both class, the base method is defined with a keyword virtual which is taken as virtual function.
#include<iostream>
using namespace std;
class greet1
{
public:
virtual void greet()
{
cout<<"Hello base class greeting";
}
};
class greet2 : public greet1
{
public:
void greet()
{
cout<<"\nHello derived class greeting";
}
};
int main()
{
greet1 g1obj;
greet2 g2obj;
greet1 *ptr;
ptr=&g1obj;
ptr->greet(); // call base class function
ptr=&g2obj;
ptr->greet(); // call derive class function
return 0;
}
Adhoc polymorphism:
It is also called as operator overloading where an operator is overloaded.for example a + operator is used to concat,add etc
5+5=10
Hello+world=hello world
Here an object gets overloaded to perform a specific task. Consider the following example:
#include <iostream>
using namespace std;
class Test
{
   private:
      int myvalue;
   public:
       Test(): myvalue(10){}
       void operator ++()
       {
          myvalue = myvalue*5;
       }
       void disp() { cout<<"after overloading: "<<myvalue; }
};
int main()
{
    Test t;
    // this calls "function void operator ++()" function
    ++t;  
    t.disp();
    return 0;
}
In the above code ++ operator is overloaded to perform a multiplication operation, the ++ operator function is to increment the given value by one, but in case of overloading the operator it function is changed to perform multiplication

No comments:

Post a Comment