Abstract Base Class and Polymorphism in C++


Published on

Connect with me through Facebook and twitter for more details:http://www.facebook.com/lijuthomas24, http://www.twitter.com/lijuthomas24

This ppt explains the concept of abstract base class and Polymorphism in C++

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

  • Be the first to like this

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

No notes for slide
  • //Error: Object of an abstract class can not be created
  • Abstract Base Class and Polymorphism in C++

    1. 1. A Class definition with data members and member functions using which no objects are created is called an abstract class. It is designed only to act as a base class . So that other classes can inherit these data members and member functions.
    2. 2. #include <iostream.h> class X // Base Class { int a; public: void display() { cout<<a<<endl;} }; Class Y : public X // Derived Class { int b; public: void display() { cout<<a<<endl; cout<<b<<endl; } }; Void main() { Y ob; ………. ………. }
    3. 3. Pure virtual function can not be defined in the base class, it has to be only declared. Hence, a class containing pure virtual functions cannot be used to declare any object of that class These type of classes are only used to create derived classes and are called as “Abstract base classes”
    4. 4. #include <iostream.h> class base // Base Class { public: virtual void abc() //Pure virtual function declaration }; Class derived : public base // Derived Class { public: void abc() { cout<< “In derived Classn”; } }; Void main() { base b; derived d; d1.abc(); …….. }
    5. 5. Polymorphism is the ability to use an operator or function in different ways. It is achieved using overloading of functions and overloading of operators. Polymorphism is achieved using overloading of functions using:  Multiple methods  Single interface
    6. 6. Multiple methods imply two or more functions with the same name but with different signatures. For example, we have two overloaded add functions as shown below: // adds two integer values add(int a, int b) { int c =a+b; cout<<c<<endl; } // adds two floating values add(float a, float b) { float c =a+b; cout<<c<<endl; } The above two functions are called methods
    7. 7. With respect to previous example, whether we find sum of two integers or two floating point values it doesn’t matter for us. What is required is “to add two values” So, it is logical to use add as the function name. We may call two functions as shown below: add(10,20); add(1.5,3.5); //Invoke the first function //Invoke the second add function
    8. 8. Polymorphism is a feature of OOP with single interface and multiple methods. It also means “One name and multiple forms”. It helps us to reduce complexity using a single interface and multiple methods. It indicates calling one or more functions using same name or giving two or more meanings to an operator.
    9. 9. Also called as static binding, it binds an object to a function call during compilation time. Refers to events that occur during compile time. Execution is much faster. Not flexible since functions are decided during compilation time. Achieved using normal function calls, function overloading and operator overloading. Also called as dynamic binding, it binds an object to a function call during run time. Refers to events that occur during run time. Execution is slower. Very flexible since executable functions are decided during run time. Achieved using inheritance and virtual functions.