473,396 Members | 1,895 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,396 software developers and data experts.

Referring to a base class in a derived class

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
Jul 22 '05 #1
8 1636
Webster wrote:

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

Jul 22 '05 #2
Webster wrote:
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()

Jul 22 '05 #3
"lilburne" <li******@godzilla.net> wrote in message
news:bv************@ID-203936.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?

--
Regards,
Webster
Jul 22 '05 #4
Webster wrote:
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

Jul 22 '05 #5
Webster wrote:

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

Jul 22 '05 #6
Webster wrote:
"lilburne" <li******@godzilla.net> wrote in message
news:bv************@ID-203936.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++.

Jul 22 '05 #7
"Webster" <no***@nowhere.net> wrote:
"lilburne" <li******@godzilla.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.
Jul 22 '05 #8
Webster wrote:
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.

Jul 22 '05 #9

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

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.