What is Abstract Base Class?
- Allows the base class to provide only an interface for its derived classes.
- Prevents anyone from creating an instance of this class.
- A class is made abstract if atleast one pure virtual function defined.
EXAMPLE: Demonstrate the definition of a abstract base class
#include <iostream>
using namespace std;
class MyInterface {
public:
virtual void Display() = 0;
};
class MyClass1 : public MyInterface {
public:
void Display() {
cout << "MyClass1" << endl;
}
};
class MyClass2 : public MyInterface {
public:
void Display() {
cout << "MyClass2" << endl;
}
};
void main()
{
// MyInterface obj;
// Error E2352 Interface.cpp 26: Cannot create instance of abstract class 'MyInterace' in function main()
// Error E2353 Interface.cpp 26: Class 'MyInterface' is abstract because of 'MyInterface::Display() = 0' in function main()
// *** 2 errors in Compile ***
MyClass1 obj1;
obj1.Display();
MyClass2 obj2;
obj2.Display();
}
OUTPUT:
MyClass1
MyClass2
hello would you please differentiate between abstract class and abstract base class please explain with examples of both.....please....
ReplyDelete