Published on

object oriented programming lectures

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Polymorphism• It simply means ‘one name, multiple forms’.• We have already seen how the concepts of polymorphism implemented using overloaded function.• The overloaded member function are selected for invoking by matching arguments.• This information is known to the compiler at compile time and it is known as early binding . Also known as compile time polymorphism.
  2. 2. #include <iostream.h> void main()class A {{ B calculate; public : cout<<calculate.sum(25.5)<<endl; float sum() { cout<<calculate.sum(4.5,5)<<endl; return 10; cout<<calculate.sum(4.5,3.5,2.5)<<endl; } cout<<calculate.sum()<<endl; float sum(float i, float j,float k) { return i+j+k; } }};class B : public A Output{ 25.5 public: 9.5 using A::sum; 10.5 float sum(float i) { 10 return i; } float sum(float i, int j) { return i+j; }};
  3. 3. • Consider the situation where the function name and prototype is same in both the base and derived classes. class A { int x; public: void show(){…..} }; class B : public A { public: void show(){…..} };• Since the prototype is same, function is not overloaded.• Virtual function and pointer are used to invoke appropriate member function while the program is running and it is known as late binding .• Also known as runtime polymorphism.
  4. 4. // pointers to base class#include <iostream.h> int main () {class CPolygon { CRectangle rect; protected: CTriangle trgl; int width, height; public: void set_values (int a, int b) { CPolygon * p1 = &rect; width=a; height=b; CPolygon * p2 = &trgl; }}; p1->set_values (4,5); p2->set_values (4,5);class CRectangle : public CPolygon { public: int area (void){ return (width * height); } cout << rect.area() << endl;}; cout << trgl.area() << endl; return 0;}class CTriangle: public CPolygon { public: int area (void){ 20 return (width * height / 2); 10 }}; Code
  5. 5. • The function main creates two pointers that point to objects of class CPolygon, that are *p1 and *p2. These are assigned to the addresses of rect and trgl, and because they are objects of classes derived from CPolygon they are valid assignations.• The only limitation of using *p1 and *p2 instead of rect and trgl is that both *p1 and *p2 are of type CPolygon* and therefore we can only refer to the members that CRectangle and CTriangle inherit from CPolygon. For that reason when calling the area() members we have not been able to use the pointers *p1 and *p2.• To make it possible for the pointers to class CPolygon to admit area () as a valid member, this should also have been declared in the base class and not only in its derived ones.
  6. 6. Virtual members• In order to declare an element of a class which we are going to redefine in derived classes we must precede it with the keyword virtual so that the use of pointers to objects of that class can be suitable.
  7. 7. #include <iostream.h>class CPolygon { protected: int main () { int width, height; CRectangle rect; public: CTriangle trgl; void set_values (int a, int b) { CPolygon poly; width=a; height=b; } CPolygon * p1 = &rect; virtual int area (void) { return (0); } CPolygon *p2=&trgl;}; CPolygon * p3 = &poly; p1->set_values(4,5);class CRectangle: public CPolygon { p2->set_values(4,5); public: int area (void){ p3->set_values (4,5); return (width * height); cout << p1->area() << endl; } cout << p2->area() << endl;}; cout << p3->area() << endl;}class CTriangle: public CPolygon { public: 20 int area (void) { 10 return (width * height / 2); 0 }};
  8. 8. • The three classes (CPolygon, CRectangle and CTriangle) have the same members: width, height, set_values() and area().• area() has been defined as virtual because it is later redefined in derived classes. You can verify if you want that if you remove this word (virtual) from the code and then you execute the program the result will be 0 for the three polygons instead of 20,10,0. That is because instead of calling the corresponding area() function for each object (CRectangle::area(), CTriangle::area() and CPolygon::area(), respectively), CPolygon::area() will be called for all of them since the calls are via a pointer to CPolygon.• Therefore, what the word virtual does is to allow a member of a derived class with the same name as one in the base class be suitably called when a pointer to it is used
  9. 9. Abstract base classes• Abstract classes are similar to the class CPolygon of our previous example. The only difference is that in our previous example we have defined a valid area() function for objects that were of class CPolygon (like object poly), whereas in an abstract base class we could have simply left without defining this function by appending = 0 to the function declaration.• The class CPolygon could have been thus: // abstract class CPolygonclass CPolygon { protected: int width, height; public: void set_values(int a, int b){ width=a; height=b; } virtual int area (void) = 0; };
  10. 10. • This type of function is called a pure virtual function, and all classes that contain a pure virtual function (do-nothing function) are considered abstract base classes.• The greatest difference of an abstract base class is that instances (objects) of it cannot be created, but we can create pointers to them. Therefore a declaration likes: CPolygon poly; // incorrect CPolygon * ppoly1; //correct• This is because the pure virtual function that it includes is not defined and it is impossible to create an object if it does not have all its members defined. A pointer that points to an object of a derived class where this function has been defined is perfectly valid.• The main objective of an abstract base class is to provide some traits to the derived classes and to create a base pointer required for achieving run time polymorphism.
  11. 11. #include <iostream.h>class CPolygon { protected: int main () { int width, height; CRectangle rect; public: CTriangle trgl; void set_values (int a, int b) CPolygon * ppoly1 = &rect; { width=a; height=b; } CPolygon * ppoly2 = &trgl; virtual int area (void) = 0;}; ppoly1->set_values (4,5); ppoly2->set_values (4,5);class CRectangle: public CPolygon { cout << ppoly1->area() << endl; public: cout << ppoly2->area() << endl; int area (void) return 0; {return (width * height); } }};class CTriangle: public CPolygon { public: int area (void) 20 10 {return (width * height / 2); }};
  12. 12. #include <iostream.h>class Polygon { int main () { protected: int width, height; Rectangle rect; Triangle trgl; public: Polygon * p1 = &rect; Polygon * p2 = &trgl; void set_values (int a, int b) { width=a; height=b; } p1->set_values (4,5); p2->set_values (4,5); virtual int area (void) =0; p1->printarea(); p2->printarea(); void printarea (void) { cout << area() << endl; } return 0;}; }class Rectangle: public Polygon { public: int area (void) 20 { return (width * height); }}; 10class Triangle: public Polygon { public: int area (void) { return (width * height / 2); }};