Abstract Classes and Methods - Rybd04/2143-OOP GitHub Wiki
A class that cannot be instantiated on its own and is designed to be a base class for other classes. It contains at least one pure virtual function, which is a function declared with the = 0 syntax and has no implementation in the abstract class itself.
This would be like if you had two animal classes that both made different sounds. When the makeSound() method is called it will be overrided depending on which animal made the sound.
#include <iostream>
using namespace std;
// Abstract class (cannot be instantiated)
class Animal {
public:
// Pure virtual function (abstract method)
virtual void makeSound() = 0;
// Regular method
void sleep() {
cout << "Sleeping..." << endl;
}
};
// Derived class (must implement the pure virtual function)
class Dog : public Animal {
public:
void makeSound() override {
cout << "Woof! Woof!" << endl;
}
};
int main() {
// Animal a; // Error: cannot create an instance of abstract class
Dog d;
d.makeSound(); // Woof! Woof!
d.sleep(); // Sleeping...
return 0;
}
https://www.ibm.com/docs/en/zos/2.4.0?topic=only-abstract-classes-c https://learn.microsoft.com/en-us/cpp/cpp/abstract-classes-cpp?view=msvc-170