Question about inheritance

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.
 
V

Victor Bazarov

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) { }

This is where 'isbn' and 'price' are initialised. It is done by the
object's constructor. It has all access it needs to do so. Notice, that
since you didn't list 'order' here, it is left _uninitialised_.
std::string book() const { return isbn; }

private:
std::string isbn; // identifier for the item
int order;
protected:
double price; // normal, undiscounted price

Also, consider that since you didn't declare it in any way, the compiler
creates a _copy_ constructor for you. The compiler-created copy c-tor has
the signature

Item_base(const Item_base & other);

and it performs copy-construction for all members. Since this copy
constructor is a member, it has all necessary access.

Also, the compiler creates the _assignment_ operator for you. Its
signature is

Item_base& operator =(const Item_base & other);

all members are _assigned_ using their assignment semantics.
};


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

This is what happens: the compiler tries to see if it can construct the
'item' object from 'bulk', it tries several known _conversions_, and finds
that if 'bulk' is _converted_ to 'Item_base' object (which it can do since
'bulk' has the _derived_ type), then the _copy_constructor_ can be used to
construct 'item'.
item = bulk; // LINE2

The compiler-generated copy assignment operator is invoked here, similarly
to the above. The compiler finds that if 'bulk' is converted to the class
'Item_base' (and that's allowed), it can then use the compiler-generated
assignment operator.
}

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?

Compiler takes care of that.
How about the protected member "double price"?

Same thing.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top