Interfaces in C++ with Examples
In C++, we use terms abstract class and interface interchangeably. A class with pure virtual function is known as an abstract class. For example, the following function is a pure virtual function:
virtual void fun() = 0;
A pure virtual function is marked with a virtual keyword and has = 0
after its signature. You can call this function an abstract function as it has no body. The derived class must give the implementation to all the pure virtual functions of parent class else it will become abstract class by default.
Why we need an abstract class?
Let’s understand this with the help of a real-life example. Let’s say we have a class Animal, animal sleeps, animal make sound, etc. For now I am considering only these two behaviors and creating a class Animal with two functions sound() and sleeping().
Now, we know that animal sounds are different cat says “meow”, dog says “woof”. So what implementation do I give in Animal class for the function sound(), the only and correct way of doing this would be making this function pure abstract so that I need not give implementation in Animal class but all the classes that inherit Animal class must give implementation to this function. This way I am ensuring that all the Animals have sound but they have their unique sound.
Abstract class Example
#include<iostream> using namespace std; class Animal{ public: //Pure Virtual Function virtual void sound() = 0; //Normal member Function void sleeping() { cout<<"Sleeping"; } }; class Dog: public Animal{ public: void sound() { cout<<"Woof"<<endl; } }; int main(){ Dog obj; obj.sound(); obj.sleeping(); return 0; }
Rules of Abstract Class
1) As we have seen that any class that has a pure virtual function is an abstract class.
2) We cannot create the instance of abstract class. For example: If I have written this line Animal obj;
in the above program, it would have caused compilation error.
3) We can create pointer and reference of base abstract class points to the instance of the child class. For example, this is valid:
Animal *obj = new Dog(); obj->sound();
4) Abstract class can have constructors.
5) If the derived class does not implement the pure virtual function of parent class then the derived class becomes abstract.
Related Posts:
- Data Types in C++
- Operators in C++
- Recursion in C++ with example
- Functions in C++ with example
- goto statement in C++ with example
- Break statement in C++ with example
- Continue Statement in C++ with example
- do-while loop in C++ with example
- While loop in C++ with example
- For loop in C++ with example
- Switch Case statement in C++ with example
- If else Statement in C++
- Constructors in C++
- Enumeration in C++
- OOPs Concepts in C++
- this pointer in C++
- Pointers in C++
- Strings in C++
- Passing Array to Function in C++
- Multidimensional Arrays in C++
- Arrays in C++
- Difference between Function Overloading and Function overriding in C++
- Function Overriding in C++ with examples
- Function overloading in C++ with examples
- Polymorphism in C++ and its types
- Inheritance in C++ with examples
- Destructors in C++ with examples
- Encapsulation in C++ with example
- Friend Class and Friend Functions in C++
- Abstraction in C++ with example