class IDemo{public:virtual ~IDemo() {}virtual void OverrideMe() = 0;};
class Parent{public:virtual ~Parent();};
class Child : public Parent, public IDemo{public:virtual void OverrideMe(){//do stuff}};
class IBase {public:virtual ~IBase() {}; // destructor, use it to call destructor of the inherit classesvirtual void Describe() = 0; // pure virtual method};
class Tester : public IBase {public:Tester(std::string name);virtual ~Tester();virtual void Describe();private:std::string privatename;};
Tester::Tester(std::string name) {std::cout << "Tester constructor" << std::endl;this->privatename = name;}
Tester::~Tester() {std::cout << "Tester destructor" << std::endl;}
void Tester::Describe() {std::cout << "I'm Tester [" << this->privatename << "]" << std::endl;}
void descriptor(IBase * obj) {obj->Describe();}
int main(int argc, char** argv) {
std::cout << std::endl << "Tester Testing..." << std::endl;Tester * obj1 = new Tester("Declared with Tester");descriptor(obj1);delete obj1;
std::cout << std::endl << "IBase Testing..." << std::endl;IBase * obj2 = new Tester("Declared with IBase");descriptor(obj2);delete obj2;
// this is a bad usage of the object since it is created with "new" but there are no "delete"std::cout << std::endl << "Tester not defined..." << std::endl;descriptor(new Tester("Not defined"));
return 0;}
#include <iostream>#include <string>
// Static binding interface// Notice: instantiation of this interface should be usefuless and forbidden.class IBase {protected:IBase() = default;~IBase() = default;
public:// Methods that must be implemented by the derived classvoid behaviorA();void behaviorB();
void behaviorC() {std::cout << "This is an interface default implementation of bC().\n";};};
class CCom : public IBase {std::string name_;
public:void behaviorA() { std::cout << "CCom bA called.\n"; };};
class CDept : public IBase {int ele_;
public:void behaviorB() { std::cout << "CDept bB called.\n"; };void behaviorC() {// Overwrite the interface default implementationstd::cout << "CDept bC called.\n";IBase::behaviorC();};};
int main(void) {// Forbid the instantiation of the interface type itself.// GCC error: ‘constexpr IBase::IBase()’ is protected within this context// IBase o;
CCom acom;// If you want to use these interface methods, you need to implement them in// your derived class. This is controled by the interface definition.acom.behaviorA();// ld: undefined reference to `IBase::behaviorB()'// acom.behaviorB();acom.behaviorC();
CDept adept;// adept.behaviorA();adept.behaviorB();adept.behaviorC();// adept.IBase::behaviorC();}