0

My professor gave the following code to show an example of inheritance:

//Base class
class Inventory {
    int quant, reorder; // #on-hand & reorder qty
    double price; // price of item
    char * descrip; // description of item
public:
    Inventory(int q, int r, double p, char *); // constructor
    ~Inventory(); // destructor
    void print();
    int get_quant() { return quant; }
    int get_reorder() { return reorder; }
    double get_price() { return price; }
};
Inventory::Inventory(int q, int r, double p, char * d) : quant (q), reorder (r), price (p)
{
    descrip = new char[strlen(d)+1]; // need the +1 for string terminator
    strcpy(descrip, d);
} // Initialization list



//Derived Auto Class
class Auto : public Inventory {
    char *dealer;
public:
    Auto(int q, int r, double p, char * d, char *dea); // constructor
    ~Auto(); // destructor
    void print();
    char * get_dealer() { return dealer; }
};
Auto::Auto(int q, int r, double p, char * d, char * dea) : Inventory(q, r, p, d) // base constructor
{
    dealer = new char(strlen(dea)+1); // need +1 for string terminator
    strcpy(dealer, dea);
}

I was confused line "Auto::Auto(int q, int r, double p, char * d, char * dea) : Inventory(q, r, p, d)", what is the definition "Inventory(q, r, p, d)" doing. Similarly in the line "Inventory::Inventory(int q, int r, double p, char * d) : quant (q), reorder (r), price (p)" I'm not sure what he is doing with quant (q), reorder (r), price (p). Are these the same variables that were defined in class as int quant, reorder and double price? If so, why did he have to use in the constructor. And why/how did he use a constructor from a base class to help define "Auto" class constructor.

1
  • I see that this is not your code but I think it's worth mentioning that this code is pretty ugly, I think it would be a really really good idea to not call a class Auto given the presence of the auto keyword in the language. Also using the char * seems to be a non-idiomatic way to do this in c++, using std::string is just such a better way of going about things in almost all cases. Commented Dec 5, 2012 at 1:34

2 Answers 2

5

He is using "initialization lists".

He is calling the base class constructor. Auto::Auto(int q, int r, double p, char * d, char * dea) : Inventory(q, r, p, d) defines a constructor for the Auto class which calls the parametrized constructor of the Inventory. The parameterized constructor needs to be called because Auto is inheriting from Inventory and Inventory defines a parameterized constructor. C++ specifies that if you define a parameterized constructor, the default constructor is overridden and the only way you can instantiate an object of that class or any sub-class is by calling the parametrized constructor (or by defining an alternate default constructor).

In the case of Inventory::Inventory(int q, int r, double p, char * d) : quant (q), reorder (r), price (p), he is initializing the fields quant, reorder and price with the values q, r and p respectively. This is actually a shorthand and you can instead assign the values in the constructor body (unless the member is a constant).

Sign up to request clarification or add additional context in comments.

12 Comments

Why does he need to call "Inventory(q, r, p, d)". What does the line do? And I don't see anything in the constructor of Auto which require a parametrized constructor of Inventory. Is it for "just in case"
In the main he never defines a variable of "Inventory", thus never instantiating it. However, he calls "Auto car(3, 1, 8745.99, "Four-Door", "GM"). Does this automatically call the Inventory constructor because of the constructor definition in Auto, thereby initializing the values of "quant", "reorder", "price" and "d".
You can only instantiate an object of type Auto if the constructor of Auto somehow calls the parameterized constructor of Inventory because it inherits from it. So according to the principles of OOP, an Auto object is an Inventory object.
So since he instantiated Auto, he didn't need to instantiate Inventory. But how about setting the private member values of "quant", "reorder", "price" and "d" which are used as the constructor for Inventory. How are they called?
When you call Inventory's parameterized constructor like this: Inventory(q, r, p, d), it does that for you. Inventory's constructor does that.
|
4

In the children constructor, you always call the constructor of the father by default. If that constructor receives arguments, you have to explicitly write the call by using the : notation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.