Question about inheritance



 DEVELOP > c-Plus-Plus > Question about inheritance

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 24 Feb 2006 03:49:10 PM
Object: Question about inheritance
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.
.

User: "Victor Bazarov"

Title: Re: Question about inheritance 24 Feb 2006 04:01:35 PM
wrote:

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
--
Please remove capital As from my address when replying by mail
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER