11

Abstract Base Class and Polymorphism in C++

Embed Size (px)

DESCRIPTION

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++

Citation preview

Page 1: Abstract Base Class and Polymorphism in C++
Page 2: Abstract Base Class and Polymorphism in C++

ABSTRACT CLASS

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.

Page 3: Abstract Base Class and Polymorphism in C++

#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;

……….

………. }

Page 4: Abstract Base Class and Polymorphism in C++

Abstract Base Class

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”

Page 5: Abstract Base Class and Polymorphism in C++

#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 Class\n”; }

};

Void main()

{ base b;

derived d;

d1.abc();

……..

}

//Error: Object of an abstract class can not be created

//Results in compile error

//OK

Page 6: Abstract Base Class and Polymorphism in C++

PolymorphismPolymorphism 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

Page 7: Abstract Base Class and Polymorphism in C++

Multiple Methods: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 valuesadd(int a, int b){ int c =a+b; cout<<c<<endl;}

// adds two floating valuesadd(float a, float b){ float c =a+b; cout<<c<<endl;}

The above two functions are called methods

Page 8: Abstract Base Class and Polymorphism in C++

Single Interface: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); //Invoke the first function add(1.5,3.5); //Invoke the second add function

Page 9: Abstract Base Class and Polymorphism in C++

What do we infer ??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.

Page 10: Abstract Base Class and Polymorphism in C++

Types of PolymorphismStatic Polymorphism

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.

Dynamic PolymorphismAlso 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.

Page 11: Abstract Base Class and Polymorphism in C++