class CParent {
..
protected:
int a;
int b;
}
class CChildA : public CParent {
..
private:
int c;
}
class CChildB : public CParent {
..
private:
int d;
}
Ok. pretty simple so far.
Now, this is my confussion.
I''m saving the above data to a DATABASE. now, do i need to put a save function in both CParent and in ChildA and ChildB? That means when i''m in ChildA and ChildB, i call the Parent save member function, then continue and do the Child''s class saving of it''s variables?
--OR--
should i put it in only ChildA and ChildB? If i do that, then i''m repeating code (ie. the int a and int b saving bits).
hmm.. i hope that made sence .. i''m really bad at explaining things.
Hope to hear some answers soon
-PK-
Proper use of Inheritence?
Hello All.
yep. it''s me again ... plodding along with my C++.
I have a question regarding the proper use of inheritence, with regards to member functions, and where they should be placed.
I have the following classes
Of course the parent class should be able to save it''s own data and the childs only save what they have. Why did you even ask since you thought of that yourself and even said that with full save code on both childs would repeat code?
First one I think because if you get many inherited members it will become really boring to maintain code (redundancy : write in each class the inherited variables to be saved). Moreover, I think it''s the proper use of inheritence.
Hope it''ll help...
Hope it''ll help...
the base class has no access to, or knowledge of children.
if you want to save the variables of a child class, then you must write a save function local to that child class. one thing you might do is make the parent save function virtual, and override it in child classes.
if you want to save the variables of a child class, then you must write a save function local to that child class. one thing you might do is make the parent save function virtual, and override it in child classes.
how about having a
public:
void Save()
function in the base class and a
protected:
virtual void DoSave() = 0;
then override DoSave in your derived classes
the Save function can call DoSave and also add any important extra information such as calling
private:
virtual int ID() = 0;
so
void Save()
{
file << ID();
DoSave();
}
by the way: why would you want protected members? isn't it better to make them private and give them protected access/modifier functions. encapsulation and all that
[edited by - petewood on March 23, 2002 7:01:35 AM]
public:
void Save()
function in the base class and a
protected:
virtual void DoSave() = 0;
then override DoSave in your derived classes
the Save function can call DoSave and also add any important extra information such as calling
private:
virtual int ID() = 0;
so
void Save()
{
file << ID();
DoSave();
}
by the way: why would you want protected members? isn't it better to make them private and give them protected access/modifier functions. encapsulation and all that
[edited by - petewood on March 23, 2002 7:01:35 AM]
petewood: why would you want to encapsulate overriden functions?
anyways,
here is sort of solution i came up with.
because your writting a database, you are probalby working with binary data. my method might be workable if you can gaurantee that child classes dont have dynamic data in them, and that save/load is done to identical classes (that includes vtable identity). it may also limit portability, since files may be system/compiler dependent due to padding and such.
anyways,
here is sort of solution i came up with.
because your writting a database, you are probalby working with binary data. my method might be workable if you can gaurantee that child classes dont have dynamic data in them, and that save/load is done to identical classes (that includes vtable identity). it may also limit portability, since files may be system/compiler dependent due to padding and such.
quote:
Why did you even ask
{/quote]
becuase i was wondering if anyone would bring up the concept of VIRTUAL functions here (i didn''t say that on purpose becuase i was curious to see what type of answers I would get
if you want to save the variables of a child class, then you must write a save function local to that child class
OK.. this seems to be the general cenus … and have the child call the parent save, then it''s own save.
quote:
the parent save function virtual, and override it in child classes.
hmm .. by doing that, that means i''ll have duplicate code in the children, becuase the virtual function will not be used. It''s virtual, which if i''m understood, means the children need to define it. So that means all children classes will have duplicate code.
Now i like Petewoods suggestion
By making the base class (parent) have a common SAVE function, which calls another virtual function, the publish base SAVE does the base variables, and then kicks in the virtual doSave, which the children must define I LIKE! That''s the answer i wish. Of course, if there are no children, then the virtual function is just nothing, in the base class
is my understanding of this correct?
-PK-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement