0

I inherited the constructor from class Cars to add a seats parameter. Is there any way to simply add the one parameter I want without copying the inherited constructor's whole parameter list?

#include <iostream>
using namespace std;

class Cars  {
    public:
        string brand;
        string type;
        string category;
        string origin;
        Cars (string abrand, string atype, string acategory, string aorigin)   {
            brand = abrand;
            type = atype;
            category = acategory;
            origin = aorigin;
        }
        void statement()    {
            cout << brand << " " << type << " is a "<< category << " from " << origin;
        }
};

class Seats : public Cars{
    public:
        int seat;
        Seats (int aseat)    {
          seat = aseat;
        }
        void statement2()   {
            cout << brand << " " << type << " has " << seat << " seats.";
        }
};

int main()  {
    Cars car1 ("BMW", "320i", "Sedan", "Germany");
    Cars car2 ("Ford", "Mustang", "Sports", "USA");
    Cars car3 ("Chevrolet", "Suburban", "SUV", "USA");
    Cars car4 ("Toyota", "Land Cruiser", "SUV", "Japan");
    Seats car6 ("Ford", "Mustang", "Sports", "USA", 2);
    Cars car5 ("Rolls Royce", "Phantom", "Luxury", "Great Britain");
    car4.statement();
    car6.statement2();

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Patrick
  • 19
  • 3
  • 6
    a seat should not inherit from a car. inheritance creates an is-a relationship and a seat is not a car – NathanOliver Jun 04 '22 at 14:15
  • 1
    https://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language – Hans Passant Jun 04 '22 at 14:16
  • 3
    A `car` has `seats` ie composition not inheritance. – Richard Critten Jun 04 '22 at 14:17
  • 1
    You can inherit a constructor if you don't change the parameters. In your case, you're adding a parameter, so you need to specify which of the parameters go to the base class and which ones don't. `Seats(string abrand, string atype, string acategory, string aorigin, int aseat) : seat(aseat), Cars(abrand, atype, acategory, aorigin)`. – Raymond Chen Jun 04 '22 at 14:17
  • 3
    @NathanOliver The class "Seat" is misnamed. It should be "CarWithSeatCount". – Raymond Chen Jun 04 '22 at 14:18
  • 3
    @RaymondChen To cater for all those cars with no seats :) – Paul Sanders Jun 04 '22 at 14:37
  • 1
    The argument about composition (has-a-...) vs inheritance (is-a-...) is a guideline or principle, which helps to get a smooth relationship of objects. Inheritance in C++ is about external interface (here is-a-... is important, as the derived classes get the same interface), about stored data (here inheritance and composition nearly are the same), about internal functionality (inherited private functions, which can be reused) and about polymorphism. Whenever a value or a pointer is needed, the derived class can be used instead of the base class. – Sebastian Jun 04 '22 at 20:17
  • You can call the base class constructor with parameters from the member initialization list, like @RaymondChen commented, but I would specify the base class constructor call before the `seats` member variable, as this is the actual order they are called/initialized (base class first). You would have to create a `Seat` constructor with enough parameters to 'forward' them further. – Sebastian Jun 04 '22 at 20:24
  • 1
    Do you mean: `Seats(const Car &car, int seat_) : Car(car), seat(seat_) { }`? – Goswin von Brederlow Jun 04 '22 at 22:24

0 Answers0