I'm having trouble with overriding methods in subclasses. I'll explain
the problem using some code:
class BaseClass
{
protected:
void method2();
public:
void method1();
}
void BaseClass::method1()
{
// ...
method2();
// ...
}
void BaseClass::method2()
{
cout << "method2 of BaseClass";
}
class SubClas :public BaseClass
{
private:
void method2();
}
void SubClass::method2()
{
cout << "method2 of SubClass"
}
int main()
{
SubClass s1;
s1.method1();
return 0;
}
ok, so I have method1 in the base class, which among other things calls
method2. The stuff that happens in method1 is the same for all
subclasses. However, what happens in method2 differs depending on the
subclass (although there is some common behaviour, the subclasses merely
extend method2). Now when I call method1 for the subclass, it should
give the behaviour of the subclass method2. But it does not - it uses
the baseclass method2. I understand why this is, and I have a solution
for it, but I'd like a better way.
The solution I use is just copying the whole method1 into every subclass.
This way the subclass method1 is called, which then calls the subclass
method2.
Is there any way I can code this better?
TIA
zero 5 2760
At 2004-02-07 13:24 +0000, zero wrote: I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code:
class BaseClass { protected: void method2();
Make this
virtual void method2();
zero wrote: I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code:
class BaseClass { protected:
Why is it protected? Are derived classes ever supposed to call
method2() directly?
void method2();
Make that function virtual:
virtual void method2(); public: void method1(); }
; void BaseClass::method1() { // ...
method2();
// ... }
void BaseClass::method2() { cout << "method2 of BaseClass"; }
class SubClas :public BaseClass { private: void method2(); }
; void SubClass::method2() { cout << "method2 of SubClass" }
int main() { SubClass s1;
s1.method1();
return 0; }
ok, so I have method1 in the base class, which among other things calls method2. The stuff that happens in method1 is the same for all subclasses. However, what happens in method2 differs depending on the subclass (although there is some common behaviour, the subclasses merely extend method2). Now when I call method1 for the subclass, it should give the behaviour of the subclass method2. But it does not - it uses the baseclass method2.
That's because you forgot to make method2 virtual. Making a function
virtual means that it will be called polymorphically, which is what you
want here.
I understand why this is, and I have a solution for it, but I'd like a better way. The solution I use is just copying the whole method1 into every subclass. This way the subclass method1 is called, which then calls the subclass method2.
That's a bad solution. It means code duplication, which can lead to
nasty errors and a lot of maintainance work. It also means that your
derived classes need more knowledge about how the base class works, so
it defeats the encapuslation principle.
Is there any way I can code this better?
Do the above mentioned changes.
wonderful, thanks!
Rolf Magnus <ra******@t-online.de> wrote in news:c0*************@news.t-
online.com:
<truncated>
On Sat, 07 Feb 2004 13:24:58 GMT in comp.lang.c++, zero <ze**@this.hi>
was alleged to have written: The stuff that happens in method1 is the same for all subclasses. However, what happens in method2 differs depending on the subclass (although there is some common behaviour, the subclasses merely extend method2).
This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[20.1] What is a "virtual member function"?" It is always good to
check the FAQ before posting. You can get the FAQ at: http://www.parashift.com/c++-faq-lite/
"zero" <ze**@this.hi> wrote... I'm having trouble with overriding methods in subclasses.
Judging from the source below, you're no overriding. You're redefining.
The term "overriding" is only applicable to virtual member functions.
I'll explain the problem using some code:
class BaseClass { protected: void method2();
public: void method1(); }
Semicolon missing here. void BaseClass::method1() { // ...
method2();
This calls 'BaseClass::method2'. According to its declaration,
anyway. // ... }
void BaseClass::method2() { cout << "method2 of BaseClass"; }
class SubClas :public BaseClass { private: void method2(); }
Semicolon is missing here. void SubClass::method2() { cout << "method2 of SubClass" }
int main() { SubClass s1;
s1.method1();
return 0; }
ok, so I have method1 in the base class, which among other things calls method2.
Yes, it calls BaseClass::method2.
The stuff that happens in method1 is the same for all subclasses. However, what happens in method2 differs depending on the subclass (although there is some common behaviour, the subclasses merely extend method2).
That's your intention, I take it.
Now when I call method1 for the subclass, it should give the behaviour of the subclass method2.
Why should you? 'method1' calls 'BaseClass::method2' according to the
way 'method2' is declared.
But it does not - it uses the baseclass method2. I understand why this is, and I have a solution for it, but I'd like a better way. The solution I use is just copying the whole method1 into every subclass. This way the subclass method1 is called, which then calls the subclass method2. Is there any way I can code this better?
Yes, what you're attempting to accomplish is called "polymorphic behaviour"
and it can only be accomplished by declaring 'method2' _virtual_.
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrew Durdin |
last post by:
In Python, you can override the behaviour of most operators for a
class, by defining __add__, __gt__, and the other special object
methods.
I noticed that, although there are special methods for...
|
by: Hongzheng Wang |
last post by:
Hi,
I have a problem about the overriding of private methods of base
class.
That is, if a method f() of base class is private, can the derived
class overriding f() be overriding?
For...
|
by: Burkhard Perkens-Golomb |
last post by:
Hi,
I've a question about XML documentation of overridding methods and
methods implementing an interface declaration: These methods are
normally documented in the base class or interface. So I...
|
by: ORi |
last post by:
Hi all !
There's a question I've been bothering for a while:
I'm actually developing architectural frameworks for application
developing and I think virtual methods, although needed because of...
|
by: ESPNSTI |
last post by:
Hi, I'm very new to C# and .Net, I've been working with it for about a month.
My experience has been mainly with Delphi 5 (not .Net).
What I'm looking for is for a shortcut way to override a...
|
by: TS |
last post by:
i have a class that i'm trying to understand that overrides
BaseApplicationException's methods as follows. What i dont' understand is
that i have never seen the inherit ":" on a method signature,...
|
by: Bob Weiner |
last post by:
What is the purpose of hiding intead of overriding a method? I have googled
the question but haven't found anything that makes any sense of it.
In the code below, the only difference is that...
|
by: bryanbabula |
last post by:
I have a question about overriding i was wondering if anyone could
help me with, or even suggesting a better/different way. I have no
idea if this can even be done or not.
I was wondering if there...
|
by: r035198x |
last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize.
These were designed to be overridden according to specific general contracts. Other classes that...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |