How to Create an Interface

Asked 2 years ago, Updated 2 years ago, 175 views

How do I set up a class that represents the interface? Can I just write an abstract class?

c++ inheritance interface abstract-class pure-virtual

2022-09-22 22:37

1 Answers

Create a class that has pure virtual methods. A class with pure virtual method as a member function is called an abstract class The abstract class cannot create an instance because it has an undefined member function.

You can inherit this abstract class and override virtual methods.

For the pure virtual method, you can add "= 0" after the virtual function as follows:

// Abstract class to write as interface
class Parent
{
    public:
        virtual ~Parent() {}
        virtual void functionToOveride() = 0;
};

class Child : public Parent
{
    public:
        virtual void functionToOveride()
        {
            ///...
        }
};


2022-09-22 22:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.