Overriding Methods

Z

zero

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
 
F

fifo

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();
 
R

Rolf Magnus

zero said:
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.
 
D

David Harmon

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

Victor Bazarov

zero said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top