Close

Class inheritance for menu system

A project log for Sample based drum machine

Sample based drum machine

lee-sampsonLee Sampson 10/14/2022 at 15:110 Comments
#include <vector>
#include <iostream>
#include <memory>

class can_display {
public:
  virtual void display() = 0;
  virtual ~can_display() = default;
};

class A : public can_display 
{
  void display() override
  {
    // display something
    std::cout << "Display from A\n";
  }
};

class B : public can_display 
{
  void display() override
  {
    // display something two
    std::cout << "Display from B\n";
  }
};


int main()
{
    can_display* a = new A();
    can_display* b = new B();

    can_display* array[2] = {a, b};

    array[0]->display();
    array[1]->display();

}

Test code to have multiple menu subclasses within one overall menu class. Essentially allows you to have different classes with the same functions within an array. You can then have something like this:

menu[x].display();

So each menu subclass in the array can be selected and then the function "display()" can be run.

Will update with actual menu based example

Discussions