How to inherit from an abstract class properly in C++?

Aesha Amoli

I couldn't find a proper topic for this question as I haven't got a proper error message.

I'm trying to create a management system for a restaurant which mainly provides pizza as well as other foods(pasta, wings, etc). I want this system to be used by the staff. I have created an abstract class named Foods that can be used to inherit by other foods. So far I have created a class that inherits from Foods named Pizza. Below are my code.

PS: I have used namespaces for organize foods and staff members separately. As far as I know some people doesn't recommend namespace and my apologies if you're one of them.

interfaces.h

#include <vector>
#include <string>

namespace foods{
    class Food{
        double price;
        // since the sauces and drinks are given with foods.
        static const std::vector<std::string> sauces;
        static const std::vector<std::string> drinks;
    public:
        virtual int retPrice() = 0;
        virtual void ask() = 0; // ask user what to add
        virtual ~Food() = default;
    };
    const std::vector<std::string> Food::sauces = {"Blue cheese", "Garlic", "Honey BBQ", "Marinara"};
    const std::vector<std::string> Food::drinks = {"Pepsi", "Mountain Dew", "Coca Cola"};

    class Pizza: public Food{
        const double price;
        const std::string pizzaType; // whether it is chicken, beef, etc.
        const std::string size; // small, medium or large
    
        int crust = 1; // how crust it is from 1-5
        std::vector<std::string> toppings; // to store toppings
    public:
        Pizza(): price(15), pizzaType(" "), size(" "){}
        int retPrice() override; // the price should change according to the type
        void ask() override; // ask the customer for a pizza

        void createACustom(); // create a custom pizza with desired toppings
    };
};

functions.cpp

#include <iostream>
#include "interfaces.h"

namespace foods{
    int Pizza::retPrice(){
        return (price+5);
    }
    void Pizza::ask(){
        std::cout << "Hello World!";
    }
}

test.cpp

#include "interfaces.h"

int main(){
    foods::Pizza* pizza = new foods::Pizza();
}

And I'm getting following error.

/usr/bin/ld: /tmp/ccQRR5B8.o: warning: relocation against `_ZTVN5foods5PizzaE' in read-only section `.text._ZN5foods5PizzaC2Ev[_ZN5foods5PizzaC5Ev]'
/usr/bin/ld: /tmp/ccQRR5B8.o: in function `foods::Pizza::Pizza()':
test.cpp:(.text._ZN5foods5PizzaC2Ev[_ZN5foods5PizzaC5Ev]+0x2b): undefined reference to `vtable for foods::Pizza'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

I tried using the keyword override and also made a default deconstructor, yet nothing seems working. I want to know what this error message means and a solution for this. In addition to that what is vtable?

Appreciate your time and answers.

EDIT 1

I have compiled it with g++ -Wall -Wextra test.cpp functions.cpp -o test, which is wrong and then I did g++ -Wall -Wextra test.cpp functions.cpp -o test and I'm getting following error.

/usr/bin/ld: /tmp/ccmv2G17.o:(.bss+0x0): multiple definition of `foods::Food::sauces[abi:cxx11]'; /tmp/ccuBNQjX.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccmv2G17.o:(.bss+0x20): multiple definition of `foods::Food::drinks[abi:cxx11]'; /tmp/ccuBNQjX.o:(.bss+0x20): first defined here
collect2: error: ld returned 1 exit status

Why is it saying that it has multiple definitions?

Jabberwocky

You need to implement the static member variables sauces and drinks in functions.cpp and not in interfaces.h.

functions.cpp

namespace foods {
  int Pizza::retPrice() {
    return (price + 5);
  }
  void Pizza::ask() {
    std::cout << "Hello World!";
  }

  // implement the static variables here.
  const std::vector<std::string> Food::sauces = { "Blue cheese", "Garlic", "Honey BBQ", "Marinara" };
  const std::vector<std::string> Food::drinks = { "Pepsi", "Mountain Dew", "Coca Cola" };
}

And remove them from interfaces.h.

If you implement them in interfaces.h they end up being implemented in each .cpp file that includes interfaces.h.

It's basically the same problem as if you define a global variable in a .h file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make an Abstract Class inherit from another Abstract Class in Python?

From Java

How to call abstract method from abstract class called by inherit class

From Java

Inherit annotations from abstract class?

From Dev

How to test an object class inherit from a certain abstract class

From Dev

How to inherit abstract class with PyCharm

From Dev

Correct way to inherit from Abstract class with dependent interfaces in C#

From Dev

Node.js: how to 'inherit' from abstract class?

From Dev

How to register classes that inherit from an abstract class in python

From Java

Making an abstract class inherit from another abstract class in python

From Dev

Inherit INotifyPropertyChanged for Static Event from abstract Class

From Dev

How to inherit from c++ iterator class?

From Dev

can abstract class inherit another abstract class c#

From Dev

How to inherit a generic class from a nested generic class in C#

From Dev

How to properly inherit the Surface class in Pygame

From Dev

Mock class which inherit from abstract class and implements interface

From Dev

How to inherit a method properly from parent class without changing the parent class?

From Javascript

How to inherit from a class in javascript?

From Dev

Is there a way to inherit from a (preferably abstract) base razor class?

From Dev

Should a python abstract base class inherit from object

From Dev

Doxygen - Inherit documentation from externally documented abstract class

From Dev

@abstractmethod given by a second class to inherit from (multiple inheritance with abstract methods)

From Dev

How to properly derive/inherit from a QGraphicsLineItem?

From Dev

How to call a method in an abstract class properly

From Dev

Inherit multiple abstract class in java

From Dev

Inherit wrapper from abstract method

From Dev

C++ a class inherit from a type

From

Inherit from a Swift class in Objective C

From Dev

Inherit C++ class from protobuf

From Dev

How to inherit using declarations from base class

Related Related

  1. 1

    How to make an Abstract Class inherit from another Abstract Class in Python?

  2. 2

    How to call abstract method from abstract class called by inherit class

  3. 3

    Inherit annotations from abstract class?

  4. 4

    How to test an object class inherit from a certain abstract class

  5. 5

    How to inherit abstract class with PyCharm

  6. 6

    Correct way to inherit from Abstract class with dependent interfaces in C#

  7. 7

    Node.js: how to 'inherit' from abstract class?

  8. 8

    How to register classes that inherit from an abstract class in python

  9. 9

    Making an abstract class inherit from another abstract class in python

  10. 10

    Inherit INotifyPropertyChanged for Static Event from abstract Class

  11. 11

    How to inherit from c++ iterator class?

  12. 12

    can abstract class inherit another abstract class c#

  13. 13

    How to inherit a generic class from a nested generic class in C#

  14. 14

    How to properly inherit the Surface class in Pygame

  15. 15

    Mock class which inherit from abstract class and implements interface

  16. 16

    How to inherit a method properly from parent class without changing the parent class?

  17. 17

    How to inherit from a class in javascript?

  18. 18

    Is there a way to inherit from a (preferably abstract) base razor class?

  19. 19

    Should a python abstract base class inherit from object

  20. 20

    Doxygen - Inherit documentation from externally documented abstract class

  21. 21

    @abstractmethod given by a second class to inherit from (multiple inheritance with abstract methods)

  22. 22

    How to properly derive/inherit from a QGraphicsLineItem?

  23. 23

    How to call a method in an abstract class properly

  24. 24

    Inherit multiple abstract class in java

  25. 25

    Inherit wrapper from abstract method

  26. 26

    C++ a class inherit from a type

  27. 27

    Inherit from a Swift class in Objective C

  28. 28

    Inherit C++ class from protobuf

  29. 29

    How to inherit using declarations from base class

HotTag

Archive