Class C++
Write your question here.
For example, I have a base constructor and I want to write a derived constructor using base constructor. Is it valid?
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Bill::Bill(string code, string name, float price)//base constructor
{
_code=code
_name = name;
_price = price;
}
VatBill::VatBill(string code,string name,float price,float tax)//derived class constructor
{
Bill(code,name,price)
_tax=tax
}
|
Yes, it is and you can simplify your code somewhat by using member initialization lists:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class Bill
{
private:
string _code;
string _name;
float _price;
public:
Bill(string code, string name, float price)
: _code(code), _name(name), _price(price) {};
};
class VatBill : public Bill
{
private:
float _tax;
public:
VatBill(string code,string name,float price,float tax)
: Bill(code, name, price), _tax(tax){};
};
|
see also using declaration for inheriting ctor's:
http://en.cppreference.com/w/cpp/language/using_declaration
Topic archived. No new replies allowed.