Referring to a base class in a derived class

W

Webster

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!
 
J

Janusz Szpilewski

Webster said:
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
 
L

lilburne

Webster said:
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()
 
W

Webster

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?
 
A

Andrey Tarasevich

Webster said:
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.
 
J

Janusz Szpilewski

Webster said:
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
 
L

lilburne

Webster said:
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++.
 
D

Daniel T.

Webster said:
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.
 
R

Rolf Magnus

Webster said:
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,885
Members
47,419
Latest member
ArturoBres

Latest Threads

Top