Published on

A topic in c++ polymorphism

Published in: Education, Technology, Sports
  • Be the first to comment

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

No notes for slide


  2. 2. What is Meant By Polymorphism?  It is a Greek word.  Poly mean many.  Morphism mean forms.  In programing Polymorphism is the ability for objects of different classes related by inheritance to response differently to the same function call. 2
  3. 3. What is Polymorphism in OOP?  After the inheritance it is another most important feature of OOP.  In polymorphism, the member functions with same name are define in base class and also in each derived class.  Polymorphism is use to keep the interface of base class to its derived classes.  Polymorphism can be achieved by mean of virtual functions.  In polymorphism one pointer to a base class object may also point any object of its derived class. 3
  4. 4. How can we access the member of class?  The member of a class can be access through pointer to the class.  General syntax; P -> member class;     P is pointer to object. -> is member access operator. Member class. It is the member of the object; 4
  5. 5. Simple program to access member of class; void show()  {  #include<iostream.h>   #include<conio.h>  cout<<"Your name is "<<name;  class data  cout<<"Your age is "<<age;  }  {  private:   };  main() char name[12];  {  int age;   public:  void input(){   cout<<"Entr your name“; cin>>name; cout<<"Enter your age"; cin>>age;} data obj,*j; j=&obj; j->input();  j->show();  getch();  } 5
  6. 6. Polymorphism Early Binding OR Static Binding OR Static Polymorphism LATE Binding OR DYNAMIC Binding OR dynamic Polymorphism 6
  7. 7. Early Binding  The Events That Takes Place At Compile Time Is Called Early Binding.  Also Called Static Binding.  Information Required To Call A Function Is Known At Compile Time. 7
  8. 8. Early Binding Example #include<iostream.h> 8 class C: public A{ public: #include<conio.h> void show() class A { cout<<"This Is Second Derived Class "<<endl; } }; public: void main() { void show() { cout<<"This Is Base Class "<<endl; } }; A *ptr; B C class B: public A C1; C2; ptr=&C1; { ptr->show(); public: void show() ptr=&C2; { cout<<"This Is First Derived Class "<<endl; } { ptr->show();
  9. 9. Virtual Function  Special Type Function.  Can Be Define In Both Derived And Base Class.  Its Name Will Be Same In Every Class.  Definition May Be Different.  In Derived Classes It Will Be Executed Through Pointer Of Its Base Class.  It is Declared By Keyword Virtual.  A Redefined Function Is Said To Override The Base Class Function. 9
  10. 10. Late Binding  The Events That Takes Place In Execution Time Is Called Late Binding.  In Late Binding At The Compile Time Compiler Does Not Know Which Message Should Compiler Will Respond Because The Type Of Pointer Is Unknown At Compile Time.  It Depending Upon The Contents In Pointer During Execution Time Of Program. 10
  11. 11. Late Binding Example 11 class C: public A{ #include<iostream.h> public: #include<conio.h> void show() class A { void main() { virtual void show() { cout<<"This Is Base Class "<<endl; A *ptr; } B }; C class B: public A C2; ptr->show(); public: ptr=&C2; { cout<<"This Is First Derived Class "<<endl; }; C1; ptr=&C1; { void show() cout<<"This Is Second Derived Class "<<endl; } }; public: } { ptr->show(); getch(); }
  12. 12. Pure Virtual Function  The virtual function that is only declare But Not Define In The In The Base Class.  General Syntax:   virtual function_name()=0; The class that contains the pure virtual function exists only to act as base or parent classes. 12
  13. 13. Abstract Base Class  The base class that has one or more pure virtual function is called the abstract base class.  These classes designed as a framework or layout for derived classes  An abstract base class cannot be instantiated  i.e.  An object of its type cannot be defined but a pointer to an abstract base class can be defined. 13
  14. 14. Concrete Derived Class  The derived class that does not have any pure virtual function is called concrete derived class.  The pure virtual function of the abstract base class is implemented in the concrete derived class.  The derived classes usually have their own versions of the virtual functions 14
  15. 15. 15 THANKs Good Luck