J
junw2000
When we use a derived-type object to initialize or assign a base
object, if the base class has private members, how can these members be
initialized, since private members can not be inherited?
For example,
class Item_base {
public:
int count;
Item_base(const std::string &book = "",
double sales_price = 0.0):
isbn(book), price(sales_price) { }
std::string book() const { return isbn; }
private:
std::string isbn; // identifier for the item
int order;
protected:
double price; // normal, undiscounted price
};
class Bulk_item : public Item_base{
.........
}
int main(){
Item_base item; // object of base type
Bulk_item bulk; // object of derived type
Item_base item(bulk); // LINE1
item = bulk; // LINE2
}
LINE1 and LINE2 are legal. But how about the private member
"std::string isbn" and "int order" of the base class
Item_base, how can they be initialize?
How about the protected member "double price"?
Thanks a lot.
object, if the base class has private members, how can these members be
initialized, since private members can not be inherited?
For example,
class Item_base {
public:
int count;
Item_base(const std::string &book = "",
double sales_price = 0.0):
isbn(book), price(sales_price) { }
std::string book() const { return isbn; }
private:
std::string isbn; // identifier for the item
int order;
protected:
double price; // normal, undiscounted price
};
class Bulk_item : public Item_base{
.........
}
int main(){
Item_base item; // object of base type
Bulk_item bulk; // object of derived type
Item_base item(bulk); // LINE1
item = bulk; // LINE2
}
LINE1 and LINE2 are legal. But how about the private member
"std::string isbn" and "int order" of the base class
Item_base, how can they be initialize?
How about the protected member "double price"?
Thanks a lot.