C++ abstract class

C++ abstract class

C++ Abstract class

In C++ language, we use terms abstract class and interface interchangeably. Any class with pure virtual functions is known as abstract class. For example the following function is a pure virtual function

A pure virtual function is marked with a virtual keyword or followed by virtual keyword and has = 0 after its signature. we can call this function an abstract function because 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 a abstract class?

Let’s understand it’s with the help of a real life example. Lets we have a class Animal, animal sleeps, animal make sound, etc. For now I am considering only these two behaviours and creating a class Animal with two functions sleeping() and sound() . Now, we know that animal sounds are different dog says “woof”, cat says “meow” . So what implementation do I give in Animal class for the function or method sound(), inly the correct way of doing this would be making this function pure abstract so that I do not need give implementation in Animal class but all the classes that inherits Animal class must give implementation to this function. This is the way that all the Animals have sound but they have their unique sound.

The same example can be written in a C++ program like this:

#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;
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise