C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Referring to a base class in a derived class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Webster
Guest





PostPosted: Sat Jan 31, 2004 1:01 am    Post subject: Referring to a base class in a derived class Reply with quote



Hello,

I have a class Shape and subclasses Circle, Rectangle etc. I also have a
function to write them to a file given a filepointer. However, I store the
colour info in the Shape class. My question is, is it possible to create a
WriteColour function in the Shape class, but when I call the sub class
writes (i.e. Circle.Write(...)), it will call the Shape's WriteColour
function??

Or is there a way to refer to the base class in a derived class to call it??
i.e.
base->function();

Thanks for any help!

--
Regards,
Webster


Back to top
Janusz Szpilewski
Guest





PostPosted: Sat Jan 31, 2004 1:18 am    Post subject: Re: Referring to a base class in a derived class Reply with quote



Webster wrote:
Quote:

Or is there a way to refer to the base class in a derived class to call it??
i.e.
base->function();


You can always use the fully qualified name, in this case:

Shape::function();

Regards,
Janusz


Back to top
lilburne
Guest





PostPosted: Sat Jan 31, 2004 1:20 am    Post subject: Re: Referring to a base class in a derived class Reply with quote



Webster wrote:

Quote:
Hello,

I have a class Shape and subclasses Circle, Rectangle etc. I also have a
function to write them to a file given a filepointer. However, I store the
colour info in the Shape class. My question is, is it possible to create a
WriteColour function in the Shape class, but when I call the sub class
writes (i.e. Circle.Write(...)), it will call the Shape's WriteColour
function??

Or is there a way to refer to the base class in a derived class to call it??
i.e.
base->function();


Unless you have also defined WriteColour in the derived
class then:

WriteColour();

will do fine. Otherwise you need to use scope resolution:

Base::WriteColour()




Back to top
Webster
Guest





PostPosted: Sat Jan 31, 2004 1:26 am    Post subject: Re: Referring to a base class in a derived class Reply with quote

"lilburne" <lilburne (AT) godzilla (DOT) net> wrote


Quote:

Unless you have also defined WriteColour in the derived
class then:

WriteColour();

will do fine. Otherwise you need to use scope resolution:

Base::WriteColour()


Right now Write() is defined as Virtual in the Shape class. So is it better
to have it non-virtual, and instead write the colour, and then in the
subclasses, call the Base::Write() ?? What is the better programming style?

--
Regards,
Webster



Back to top
Andrey Tarasevich
Guest





PostPosted: Sat Jan 31, 2004 1:29 am    Post subject: Re: Referring to a base class in a derived class Reply with quote

Webster wrote:
Quote:
I have a class Shape and subclasses Circle, Rectangle etc. I also have a
function to write them to a file given a filepointer. However, I store the
colour info in the Shape class. My question is, is it possible to create a
WriteColour function in the Shape class, but when I call the sub class
writes (i.e. Circle.Write(...)), it will call the Shape's WriteColour
function??

Or is there a way to refer to the base class in a derived class to call it??
i.e.
base->function();
...

If you derived classes do not declare their own 'WriteColor' functions
then by simply writing 'WriteColor' you'll refer to base class'
function. Otherwise you can use scope resolution operator '::' and write
'Shape::WriteColor' to explicitly refer to 'Shape's 'WriteColor' function.

--
Best regards,
Andrey Tarasevich


Back to top
Janusz Szpilewski
Guest





PostPosted: Sat Jan 31, 2004 1:47 am    Post subject: Re: Referring to a base class in a derived class Reply with quote

Webster wrote:

Quote:

Right now Write() is defined as Virtual in the Shape class. So is it better
to have it non-virtual, and instead write the colour, and then in the
subclasses, call the Base::Write() ?? What is the better programming style?


Having the only functionality of the overriding function limited to
calling the overridden one in the base class is definitely not a great
idea. In such a case just do not redefine the base class function in the
derived class.

If a function is virtual it does not mean you have to override it in the
derived classes. Nevertheless if the method is very simple, not going to
be overriden, like returning the color of a shape you may even leave it
as non-virtual.

It may be a good idea to read carefully more in your C++ primer about
polymorphism.

Regards,
Janusz


Back to top
lilburne
Guest





PostPosted: Sat Jan 31, 2004 1:52 am    Post subject: Re: Referring to a base class in a derived class Reply with quote

Webster wrote:

Quote:
"lilburne" <lilburne (AT) godzilla (DOT) net> wrote in message
news:bvevra$rpij1$1 (AT) ID-203936 (DOT) news.uni-berlin.de...


Unless you have also defined WriteColour in the derived
class then:

WriteColour();

will do fine. Otherwise you need to use scope resolution:

Base::WriteColour()



Right now Write() is defined as Virtual in the Shape class. So is it better
to have it non-virtual, and instead write the colour, and then in the
subclasses, call the Base::Write() ?? What is the better programming style?


You only need to be concerned if you really, really want to
call the base class method. Otherwise just call Write().

You make the function non-virtual if you can't think of a
good reason why a derived class would want to redefine it.

Be aware that if you make it virtual and *have* to use the
Base::Write() syntax then the base method will be called
even if you alter the class heirarchy. There is no
equivalent of 'super' in C++.


Back to top
Daniel T.
Guest





PostPosted: Sat Jan 31, 2004 6:57 am    Post subject: Re: Referring to a base class in a derived class Reply with quote

"Webster" <noone (AT) nowhere (DOT) net> wrote:

Quote:
"lilburne" <lilburne (AT) godzilla (DOT) net> wrote:


Unless you have also defined WriteColour in the derived
class then:

WriteColour();

will do fine. Otherwise you need to use scope resolution:

Base::WriteColour()


Right now Write() is defined as Virtual in the Shape class. So is it better
to have it non-virtual, and instead write the colour, and then in the
subclasses, call the Base::Write() ?? What is the better programming style?

Better programming style would be to not have member-variables in your
base class, IMO.

Back to top
Rolf Magnus
Guest





PostPosted: Sat Jan 31, 2004 12:43 pm    Post subject: Re: Referring to a base class in a derived class Reply with quote

Webster wrote:

Quote:
Hello,

I have a class Shape and subclasses Circle, Rectangle etc. I also
have a
function to write them to a file given a filepointer. However, I
store the
colour info in the Shape class. My question is, is it possible to
create a WriteColour function in the Shape class, but when I call the
sub class writes (i.e. Circle.Write(...)), it will call the Shape's
WriteColour function??

You can use something similar to the following:

class Shape
{
public:
//...
void Write() const;
private:
void WriteColour() const;
virtual void DoWrite() const = 0;

//...
};

void Shape::WriteColour() const
{
//...
}

void Shape::Write() const
{
WriteColour();
DoWrite();
}

class Circle
{
//...
private:
void DoWrite() const;
};

So when someone calls Write() on the object, WriteColour() will always
be called first, then DoWrite() will be called polymorpically.

Quote:
Or is there a way to refer to the base class in a derived class to
call it?? i.e.
base->function();

Just:

function();

should do fine. But the downside is that you have to remember that in
every derived class. And if you decide at some point that you might
want to add something else that is the same for all shapes, you need to
change every derived class, while with the approach I showed above, you
only need to change Shape::Write() to handle it.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.