OOP Abstract Class
OOP Abstract Class
return 0;
}
Output:
Constructing base
Constructing derived
Destructing base
Constructing base
Constructing derived
Destructing derived
Destructing base
Abstract class – pure virtual function
An abstract class is a class that is designed to be specifically used as a
base class. An abstract class contains at least one pure virtual
function.
You declare a pure virtual function by using a pure specifier (= 0) in
the declaration of a virtual member function in class.
Syntax (Abstract class - Pure virtual function)
class AB {
public:
virtual void f() = 0;
};
Limitations of Abstract class
• You cannot use an abstract class as a parameter type, a function
return type, or the type of an explicit conversion, nor can you declare
an object of an abstract class. You can, however, declare pointers and
references to an abstract class.
Examples - Limitations
class A { virtual void f() = 0;};
class B : A { virtual void f() { }};
A g();
void h(A);
int main() {
A a;
A* pa;
B b;
}
• Class A is an abstract class. The compiler would not
allow the function declarations A g() or void h(A).
Abstract class facts
• A class is abstract if it has at least one pure virtual function.
• We cannot instantiate an abstract class type; however, we can have
pointers and references of abstract class type.
• If we do not override the pure virtual function in derived class, then
derived class also becomes abstract class.
• An abstract class can have constructors.
//Abstract class example program // This class inherits from Base and
//implements fun()
class Base class Derived: public Base
{ {
int x; int y;
public: public:
virtual void fun() = 0; void fun() { cout << "fun() called"; }
int getX() { return x; } };
};
int main()
{
Derived d;
d.fun();
return 0;
}
//Instantiating abstract class ( cause error)
class Test
{
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
int main()
{
Test t;
return 0;
}
//Declaring pointer to abstract class
class Base
{
public:
virtual void show() = 0;
};
class Derived: public Base
{
public:
void show() { cout << "In Derived \n"; }
};
int main()
{ Base *bp = new Derived();
bp->show();
return 0;
}
// An abstract class with constructor
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i) { x = i; } };
class Derived: public Base
{ int y;
public:
Derived(int i, int j):Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " << y; }
};
int main()
{ Derived d(4, 5);
d.fun();
return 0; }
//Not overriding pure virtual function in derived class
class Base
{
public:
virtual void show() = 0;
};
int main(void)
{
Derived d;
return 0;
}
//Not overriding pure virtual function in derived class
class AB {
public: virtual void f() = 0;
};
class D2 : public AB {
void g();
};
int main() {
D2 d;
}
The compiler will not allow the declaration of
object d because D2 is an abstract class; it inherited the pure
virtual function f()from AB.
The compiler will allow the declaration of object d if you define
function D2::f(), as this overrides the inherited pure virtual
function AB::f(). Function AB::f() needs to be overridden if
you want to avoid the abstraction of D2.
Concrete class
• A concrete class is an ordinary class which has no purely virtual
functions and hence can be instantiated.
• https://www.geeksforgeeks.org/abstraction-in-c/
• https://www.geeksforgeeks.org/virtual-destructor/
• https://beginnersbook.com/2017/09/abstraction-in-c-with-example/
• https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-
classes/