Interface

Interface

Class can have pure virtual functions marked with the =0:

class MyClass{
public:
	virtual void doWork() = 0; // pure virtual function
};

When class has at least one pure virtual function it becomes an abstract class. Abstract class can not be instantiated it can only be derived from. Class that has only pure virtual functions is known as an interface. Example:

#include <iostream>
class IMyInterface{
public:
	virtual void doWork() = 0;
	virtual void doMoreWork() = 0;
	virtual ~IMyInterface(){}
};

class DerivedClass : public IMyInterface{
public:
	void doWork() override {
		std::cout << "Do work from a derived class." << std::endl;
	}
	void doMoreWork() override {
		std::cout << "Do more work from a derived class." << std::endl;
	}
};

int main(){
	IMyInterface* p = new DerivedClass;
	p->doWork();
	p->doMoreWork();
	delete p;
}