Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create array of an abstract class in c++

I am trying to create a program that takes food order and prints it out. I have my base class Food which has a pure virtual function in it. Class Food has 2 subclass Pizza and Dessert. I am trying to make an array of Food in my main so when a customer orders Pizza or Dessert, it will be stored in the array of Food. But every time I try, I get an error. How should I put the two items together then if I want to use a loop to go over each item the customer ordered? This is my code:

int main()
{
  Dessert d("brownie");
  Pizza p("BBQ delux");
  Food array[2] = {d,p};
}

This is my error message. (NOTE: get_set_price() and print_food() are my pure virtual functions which is defined in base class and implemented in the 2 subclasses)

main.cpp:37:14: error: invalid abstract type ‘Food’ for ‘array’
  Food array[2] = {d,p};

In file included from main.cpp:4:0:
Food.h:5:7: note:   because the following virtual functions are pure within ‘Food’:
 class Food
       ^

Food.h:20:15: note:     virtual void Food::get_set_price()
  virtual void get_set_price()=0;
               ^

Food.h:27:15: note:     virtual void Food::print_food()
  virtual void print_food()=0; 
               ^

main.cpp:37:22: error: cannot allocate an object of abstract type ‘Food’
  Food array[2] = {f,o};
                  ^
like image 341
Safwan Ull Karim Avatar asked Oct 14 '15 10:10

Safwan Ull Karim


People also ask

Can you create an array of an abstract class?

- An Abstract class is one whose instance CANNOT be created. - Creating an Array which holds the Object Reference Variable of that Abstract class are just the references not the object itself.

Can we create array of abstract class in C++?

You cannot create instances of abstract classes, but you can assign concrete derived instances to pointers or references of the base class.

How do we declare an abstract class in C?

You create an abstract class by declaring at least one pure virtual member function. That's a virtual function declared by using the pure specifier ( = 0 ) syntax. Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes.

Can you have an array of classes in C Plus Plus?

Array of Objects in c++ Like array of other user-defined data types, an array of type class can also be created. The array of type class contains the objects of the class as its individual elements. Thus, an array of a class type is also known as an array of objects.


Video Answer


2 Answers

You cannot create instances of abstract classes, but you can assign concrete derived instances to pointers or references of the base class.

int main()
{
  Dessert d("brownie");
  Pizza p("BBQ delux");
  Food* array[2] = {&d,&p};
}

then work with array

array[0]->print_food();
like image 76
Mykola Avatar answered Oct 20 '22 08:10

Mykola


You need reference semantics for that, because Food arr[2]; tries to initialize the array with default values (which are abstract, thus not constructible).

I think std::array<std::unique_ptr<Food>, 2> arr; should be the most natural to use in this case.

std::array<std::unique_ptr<Food>> arr = {
    std::make_unique<Dessert>("brownie"),
    std::make_unique<Pizza>("BBQ delux")
};

If you just want to loop over those two values, though, using initializer_list would be easiest, I suppose.

for (auto f : std::initializer_list<Food*>{&d,&p})
    f->commonMemberFunction();

Unfortunately it won't deduce the correct type from just {}, but a helper could be created, I suppose,

like image 25
Bartek Banachewicz Avatar answered Oct 20 '22 06:10

Bartek Banachewicz