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++
2. 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.
3. #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;
……….
………. }
4. 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”
5. #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();
……..
}
6. 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
7. 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
8. 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
9. 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.
10. 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.
Notes de l'éditeur
//Error: Object of an abstract class can not be created