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.
Autogiven the presence of theautokeyword 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.