Ticker

6/recent/ticker-posts

Abstract Class in C++ | Characteristics of Abstract Class

Abstract Class 

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class. 

Characteristics of Abstract Class 

1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created. 

2. Abstract class can have normal functions and variables along with a pure virtual function. 

3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface. 

4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too. 

Pure Virtual Functions 

Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with = 0. Here is the syntax for a pure virtual function, 

virtual void f() = 0; 

Example of Abstract Class 

class Base //Abstract base class 

        public: 

        virtual void show() = 0; //Pure Virtual Function 

}; 

class Derived:public Base 

        public: 

        void show() 

        

                cout << "Implementation of Virtual Function in Derived class"; 

        

}; 

int main() 

        Base obj; //Compile Time Error 

        Base *b; 

        Derived d; 

        b = &d; 

        b->show(); 

Output : Implementation of Virtual Function in Derived class 

In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class.

ALSO SEARCH:

"characteristics of abstract class in C++"

"abstract classes in C++"

"advantages of abstract class in C++"

"abstract class in C++"

"use of abstract class in C++"

"pure virtual function in C++"