473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Feb 24 '06 #1
1 1110
ju******@gmail.com 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
Feb 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.