C96
C96

Reputation: 529

How to inherit from two classes and call the constructor of the parent class I need to make an object of?

Hello I am fairly new in C++ and I am trying to make a programm that you have objects of Application class.

Application class should inherit from two classes. The class Games and the class desktopApp. When I build my programm I get the two followin errors:

no matching function for call to 'desktopApp::desktopApp()

no matching function for call to 'Games::Games()

My questions are:

Why I get these errors?

How can I call the constructor of the parent class I want to make an object of everytime?

Do I need to make two constructors one for each parent class in Application class?

Thank you very much.

Games class code:

class Games
{
    string category;
    float price;
public:
    Games (string category, float price)
    {
        this->category = category;
        this->price = price;
    }
};

desktopApp class code:

class desktopApp
{
    string edition;
    vector<string> ratings;
public:
    desktopApp (string edition, vector<string> ratings)
    {
        this->edition = edition;
        copy(this->ratings.begin(), this->ratings.end(), back_inserter(ratings));
    }
};

Application class code:

class Application:public desktopApp, public Games
{
    string name;
public:
    Application (string name, string category, float price):Games (category, price)
    {
        this->name = name;
    }

    Application (string name, string edition, vector<string> ratings):desktopApp (edition, ratings)
    {
        this->name = name;
    }
};

Main:

int main()
{
    Applications game1("aGame", "Violent", 45.7);
}

Upvotes: 0

Views: 44

Answers (1)

Alberto
Alberto

Reputation: 12949

Having two base classes A and B, a class C that inherits from both, looks like this:

class C : public A, public B{
public:
     C(/*params*/) : A(/*params*/), B(/*params*/){/*code*/}
};

So in your case your Application should looks like this:

class Application:public desktopApp, public Games
{
    string name;
public:
    Application(string edition, vector<string> ratings, string name, string category, float price) :
    desktopApp(edition,ratings),
    Games(category,price)
    {
        this->name = name;
    }

    Application(string category, float price, string name, string edition, vector<string> ratings) :
        Games(category,price),
        desktopApp(edition,ratings)
    {
        this->name = name;
    }
};

Upvotes: 2

Related Questions