473,782 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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::WriteColo ur()

Jul 22 '05 #3
"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
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::WriteCo lor' 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******@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++.

Jul 22 '05 #7
"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.
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::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.

Jul 22 '05 #9

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

Similar topics

2
2040
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...
1
4344
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...
10
3334
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
7
6625
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
5
3164
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
4
5247
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:
6
2501
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
2625
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
2413
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...
0
9479
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,...
1
10080
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,...
1
7492
isladogs
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...
0
6733
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();...
0
5378
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...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
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...

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.