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 1653
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::WriteColo ur()
"lilburne" <li******@godzi lla.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::WriteColo ur()
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::WriteCo lor' 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******@godzi lla.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::WriteColo ur()
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******@godzi lla.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::WriteColo ur()
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::WriteCol our() 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 Base class (old-style) which defines a default class attribute 'bar'
too. I can't modify the source code of this class. Note that Workes does
not inherit from Base.
- a Derived class which must inherit from Base and is a wrapper around
Worker: it...
|
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 inherited base member's
access level in the derived class: its access level in the base class and
the type of inheritance (public, protected, or private). After this
determination is made, the following possibilities exist for manually
changing the...
|
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 CommonPageBase
- Contains myPage which inherits PageBase
Each of these classes overrides OnInit and ties an event handler
| |
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 attempt to
access the object -- since m_object is not actually created and
initizialized until after the base constructor has been called.
Any thoughts on the practice below?
class Base
{
public:
|
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 static
function (which I know is illegal), but, is there a way to provide
something similar? The class that is the target of my inquiry is a
template class that interfaces to one of several derived classes
through a pointer to a base class. The...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |