Polymorphism in C++
Hello Reader Today we discuss about Polymorphism in C++ In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts.
Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is done, by method overriding, when both super and sub class have member function with same declaration bu different definition.
For example, the + (plus) operator in C++:
5 + 3 // integer addition
3.14 + 5.0 // floating point addition
s1 + "Hitesh" // string concatenation
In C++, that type of polymorphism is called overloading.
Typically, when the term polymorphism is used with C++, however, it refers to using virtual methods, which we'll discuss shortly.
Example of Function Overriding in C++
class Base
{
public:
void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
No comments :
Post a Comment