Chapter09 Polymorphism

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Polymorphism

The word polymorphism means


having many forms.
Achieving Polymorphism
Run Time Polymorphism
This type of polymorphism is achieved by
Function Overriding.
Function overriding on the other hand occurs
when a derived class has a definition for one
of the member functions of the base class.
That base function is said to be overridden.
Run Time Polymorphism
class base {
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
class derived:public base {
public:
void print ()
{ cout<< "print derived class" <<endl; }
void show ()
{ cout<< "show derived class" <<endl; }
};
int main() {
base *bptr;
derived d;
bptr = &d;
bptr->print(); //virtual function, binded at runtime (Runtime polymorphism)
bptr->show(); // Non-virtual function, binded at compile time
return 0;
}
Run Time Polymorphism
#include <iostream> class Triangle: public Shape {
using namespace std; public:
class Shape { Triangle( int a = 0, int b = 0):Shape(a, b) { }
protected: int area () {
int width, height; cout << "Triangle class area :" <<endl;
public: return (width * height / 2);
Shape( int a = 0, int b = 0){ }
width = a; };
height = b;
} int main() {
virtual int area() { Shape *shape;
cout << “Shape class area :" <<endl; Rectangle rec(10,7);
return 0; Triangle tri(10,5);
}
}; shape = &rec; // address of Rectangle
class Rectangle: public Shape { shape->area(); // call rectangle area.
public: shape = &tri; // address of Triangle
Rectangle( int a = 0, int b = 0):Shape(a, b) { } shape->area(); // call triangle area.
int area () {
cout << "Rectangle class area :" <<endl; return 0;
return (width * height); }
}
};
Pointers in C++
#include <iostream> int main(void) {
using namespace std; Box Box1(3.3, 1.2, 1.5); // Declare box1
class Box { Box Box2(8.5, 6.0, 2.0); // Declare box2
public: Box *ptrBox; // Declare pointer to a class.
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = // Save the address of first object
2.0) { ptrBox = &Box1;
cout <<"Constructor called." << endl; // Now try to access a member using member
length = l; access operator
breadth = b; cout << "Volume of Box1: " << ptrBox->Volume()
height = h; << endl;
} // Save the address of second object
double Volume() { ptrBox = &Box2;
return length * breadth * height; // Now try to access a member using member
} access operator
private: cout << "Volume of Box2: " << ptrBox->Volume()
<< endl;
double length; // Length of a box
double breadth; // Breadth of a box return 0;
double height; // Height of a box }
};
‘this’ pointer
1) When local variable’s name is same as member’s name

class Test {
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x hidden by the local variable 'x'
this->x = x;
}
void print() { cout << "x = " << x << endl; }
};

int main() {
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
‘this’ pointer

2) To return reference to the calling object


/* Reference to the calling object can be
returned */
Test& Test::func ()
{
// Some processing
return *this;
}
‘this’ pointer
Class Person{
int age;
public:
person &person::greater(person &b){
if (b.age > age){ // B.age > A.age (this.age)
return b;
}else{
return this; // this indicates invoking object A
}
}
};

Person A, B, Max;
Max = A.greater(B);

You might also like