An abstract class in C++ is a class which cannot be instantiated and can have methods (i.e. Virtual Methods) which are not defined.
We will clear the above points with the example :
class LivingBeing { public: void breathe() { cout << "Breathes oxygen from air." << endl; } virtual void sound() = 0; // The sound method is incomplete. };
Now we can see the virtual keyword used in the undefined method sound(), and is initialised with 0.
virtual void sound() = 0;
And if a method is undefined or is marked as virtual the class is considered as Abstract.
Thus the LivingBeing class is also Abstract because it has a virtual method/behaviour(Incomplete method/behaviour).
And since the class is incomplete we cannot create any objects out of it.
In other words abstract classes cannot be instantiated.
Then, what is the purpose of this class from which we cannot create any objects? We will see it below:
class Human : public LivingBeing { private: string name; string food; string language; public: Human() { } Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } void eat() { cout << name << "Eats " << food << endl; } void speak() { cout << name << "Speaks " << language << endl; } void sound() { cout << "Humans will speak" << endl; // This method has to be defined. } };
So, in the above example we have an abstract class LivingBeing.
class LivingBeing { public: void breathe() { cout << "Breathes oxygen from air." << endl; } virtual void sound() = 0; // The sound method is incomplete. };
So, as we know the child classes that inherits from it must define the abstract method sound().
virtual void sound() = 0;
And so we have done in the Human class.
class Human : public LivingBeing { private: string name; string food; string language; public: Human() { } Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } void eat() { cout << name << "Eats " << food << endl; } void speak() { cout << name << "Speaks " << language << endl; } void sound() { cout << "Humans will speak" << endl; // This method has to be defined. } };
We have defined the sound() method using the override keyword,
void sound() { cout << "Humans will speak" << endl; // This method has to be defined. }
Now, let us write the complete code for the above example :
#include <iostream> using namespace std; class LivingBeing { public: void breathe() { cout << "Breathes oxygen from air." << endl; } virtual void sound() = 0; // The sound method is incomplete. }; class Human : public LivingBeing { private: string name; string food; string language; public: Human() { } Human(string nme, string fd, string lang) { name = nme; food = fd; language = lang; } void eat() { cout << name << "Eats " << food << endl; } void speak() { cout << name << "Speaks " << language << endl; } void sound() { cout << "Humans will speak" << endl; // This method has to be defined. } }; int main() { Human human("John", "Burger", "English"); human.breathe(); human.sound(); return 0; }