Sometimes implementation of all functions cannot be provided in a base class because we don’t know the implementation. Such a class is called an abstract class.For example, let Shape be a base class. We cannot provide the implementation of function draw() in Shape, but we know every derived class must have an implementation of draw(). Similarly, an Animal class doesn’t have the implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.
A pure virtual function (or abstract function) in C++ is a virtual function for which we can have an implementation, But we must override that function in the derived class, otherwise, the derived class will also become an abstract class. A pure virtual function is declared by assigning 0 in the declaration.
Example of Pure Virtual Functions
C++
class Test {
public :
virtual void show() = 0;
};
|
Complete Example
A pure virtual function is implemented by classes that are derived from an Abstract class.
C++
#include <iostream>
using namespace std;
class Base {
int x;
public :
virtual void fun() = 0;
int getX() { return x; }
};
class Derived : public Base {
int y;
public :
void fun() { cout << "fun() called" ; }
};
int main( void )
{
Derived d;
d.fun();
return 0;
}
|
Some Interesting Facts
1. A class is abstract if it has at least one pure virtual function.
Example
In the below C++ code, Test is an abstract class because it has a pure virtual function show().
C++
#include <iostream>
using namespace std;
class Test {
int x;
public :
virtual void show() = 0;
int getX() { return x; }
};
int main( void )
{
Test t;
return 0;
}
|
Output
Compiler Error: cannot declare variable 't' to be of abstract
type 'Test' because the following virtual functions are pure
within 'Test': note: virtual void Test::show()
2. We can have pointers and references of abstract class type.
For example, the following program works fine.
C++
#include <iostream>
using namespace std;
class Base {
public :
virtual void show() = 0;
};
class Derived : public Base {
public :
void show() { cout << "In Derived \n" ; }
};
int main( void )
{
Base* bp = new Derived();
bp->show();
return 0;
}
|
3. If we do not override the pure virtual function in the derived class, then the derived class also becomes an abstract class.
The following example demonstrates the same.
C++
#include <iostream>
using namespace std;
class Base {
public :
virtual void show() = 0;
};
class Derived : public Base {
};
int main( void )
{
Derived d;
return 0;
}
|
Output
Compiler Error: cannot declare variable 'd' to be of abstract type
'Derived' because the following virtual functions are pure within
'Derived': virtual void Base::show()
4. An abstract class can have constructors.
For example, the following program compiles and runs fine.
C++
#include <iostream>
using namespace std;
class Base {
protected :
int x;
public :
virtual void fun() = 0;
Base( int i)
{
x = i;
cout << "Constructor of base called\n" ;
}
};
class Derived : public Base {
int y;
public :
Derived( int i, int j)
: Base(i)
{
y = j;
}
void fun()
{
cout << "x = " << x << ", y = " << y << '\n' ;
}
};
int main( void )
{
Derived d(4, 5);
d.fun();
Base* ptr = new Derived(6, 7);
ptr->fun();
return 0;
}
|
OutputConstructor of base called
x = 4, y = 5
Constructor of base called
x = 6, y = 7
5. An abstract class in C++ can also be defined using struct keyword.
Example
struct shapeClass
{
virtual void Draw()=0;
}
Comparison with Java
In Java, a class can be made abstract by using an abstract keyword. Similarly, a function can be made pure virtual or abstract by using an abstract keyword. See Abstract Classes in Java for more details.
Interface vs Abstract Classes
An interface does not have an implementation of any of its methods, it can be considered as a collection of method declarations. In C++, an interface can be simulated by making all methods pure virtual. In Java, there is a separate keyword for the interface.
We can think of Interface as header files in C++, like in header files we only provide the body of the class that is going to implement it. Similarly in Java in Interface we only provide the body of the class and we write the actual code in whatever class implements it.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.