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 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
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()
"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
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
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
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++.
"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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Bhan |
last post by:
Using Ptr of derived class to point to base class and viceversa
class base
{
....
}
class derived : public base
{
....
}
|
by: Baski |
last post by:
Base class:
class AssetBase
{
string _clli;
public string CLLI
{
get
|
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...
|
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...
|
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;
|
by: Dennis Jones |
last post by:
Hello,
I have a couple of classes that look something like this:
class RecordBase
{
};
class RecordDerived : public RecordBase
{
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |