Board index » cppbuilder » classes & inheritance...?

classes & inheritance...?


2005-04-20 10:23:30 PM
cppbuilder100
Hello,
This is a pretty basic question... :)
I've got a base class and an inheriting class...
class BaseClass
{
public:
void DoSomething();
private:
int BaseVar;
};
class MyBaseClass : public BaseClass
{
};
...so, when I call MyBaseClass::DoSomething() as it is inherited,
BaseClass::BaseVar is undefined. Is there a way to fix this easily with
"inherited" or "virtual" or the like?
Thanks so much!!!
Rob
 
 

Re:classes & inheritance...?

"Robert G. Hoover" < XXXX@XXXXX.COM >writes:
Quote
class BaseClass
{
public:
void DoSomething();

private:
int BaseVar;
};

class MyBaseClass : public BaseClass
{
};

...so, when I call MyBaseClass::DoSomething() as it is inherited,
BaseClass::BaseVar is undefined.
What do mean by "undefined"? Not initialized? Not accessible?
Quote
Is there a way to fix this easily with "inherited" or "virtual" or
the like?
That depends on your answer to the above question.
 

Re:classes & inheritance...?

The visiblities of both a function and a variable is controlled in the
same way by the public, protected and private cases.
The keyword virtual only applies to functions. It does not alter the
effect of public, protected and private.
class BaseClass
{
public:
void DoSomething();
private:
int BaseVar;
};
class MyBaseClass : public BaseClass
{
};
class BaseClass
{
public:
void DoSomething(); // visible to all
protected:
void DoSomethingElse(); // visible to descendent classes
private:
int BaseVar; // visible only to BaseClass
};
class MyBaseClass : public BaseClass
{
int int_var;
MyBaseClass()
{
DoSomething(); // ok
DoSomethingElse(); // ok
int_var = BaseVar; // no good, not visible
}
};
int main()
{
MyBaseClass mbc;
mbc.DoSomething(); // ok;
mbc.DoSomethingElse(); // no good, not visible
int x = mbc.BaseVar; // no good, not visible
return 0;
}
. Ed
Quote
Robert G. Hoover wrote in message
news: XXXX@XXXXX.COM ...
Hello,

This is a pretty basic question... :)


I've got a base class and an inheriting class...

class BaseClass
{
public:
void DoSomething();

private:
int BaseVar;
};

class MyBaseClass : public BaseClass
{
};

...so, when I call MyBaseClass::DoSomething() as it is inherited,
BaseClass::BaseVar is undefined. Is there a way to fix this easily
with "inherited" or "virtual" or the like?
 

{smallsort}

Re:classes & inheritance...?

On Wed, 20 Apr 2005 10:23:30 -0400, Robert G. Hoover wrote:
Quote
BaseClass::BaseVar is undefined. Is there a way to fix this easily with
"inherited" or "virtual" or the like?
No, you'll have to make it protected or public
--
Good luck,
liz
 

Re:classes & inheritance...?

When I cursor over MyVar in the de{*word*81}, I see MyVar=????
What I've done here is to create MyBaseClass when Application->Run(). I am
not creating BaseClass. So, I have MyBaseClass running and I think when I'm
calling DoSomething(), it's referencing the MyVar in the BaseClass which is
uninitialized/undefined instead of (inherited and initialized) MyVar in
MyBaseClass.
Does this make more sense?
Thanks again!
Rob
____________________________________________________________________________
"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
"Robert G. Hoover" < XXXX@XXXXX.COM >writes:

>class BaseClass
>{
>public:
>void DoSomething();
>
>private:
>int BaseVar;
>};
>
>class MyBaseClass : public BaseClass
>{
>};
>
>...so, when I call MyBaseClass::DoSomething() as it is inherited,
>BaseClass::BaseVar is undefined.

What do mean by "undefined"? Not initialized? Not accessible?


>Is there a way to fix this easily with "inherited" or "virtual" or
>the like?

That depends on your answer to the above question.
 

Re:classes & inheritance...?

"Robert G. Hoover" < XXXX@XXXXX.COM >writes:
Quote
When I cursor over MyVar in the de{*word*81}, I see MyVar=????
Questions about the de{*word*81} are best posted in the .ide group.
Quote
What I've done here is to create MyBaseClass when Application->Run(). I am
not creating BaseClass.
I'm confused by your choice of words.
MyBaseClass and BaseClass are classes. You "create" them staticly,
before the program even runs.
Quote
So, I have MyBaseClass running and I think when I'm calling
DoSomething(), it's referencing the MyVar in the BaseClass which is
uninitialized/undefined instead of (inherited and initialized) MyVar
in MyBaseClass.
There is only MyVar per MyBaseClass instance; the one inherited from
BaseClass. Why do you think that there are two different MyVars?
Quote
Does this make more sense?
Unfortunately not.
Could you please post *minimal* (i.e. just enough - not too little and
not too much) code so that others can see what you are seeing?