CaseOfInsanity
CaseOfInsanity

Reputation: 23

Adding behaviours to polymorphic objects

Let's say we have the following:

abstract class A;

class B : public A;
class C : public A;
....

class Main;

Class Main is the class that utilises A,B and C.
And we have a following code in class Main:

A obj;
....
for(i = 0; i< count; ++i)
{
    obj->Abstract_Function_In_A();        
}

I want to change Abstract_Function_In_A() in class A to take parameters.
The problem is that, adding any new functions in the base class means
I have to implement the new functions in all of the child classes even if some don't use them.

Otherwise, it may not work as expected when it's called.

Does anyone know a way around it so that I can add new functions without having to define them in all of the child classes?

Upvotes: 2

Views: 67

Answers (1)

Jake Woods
Jake Woods

Reputation: 1828

It sounds like you don't want class A to be abstract, simply define the "default" function (implementation and interface) in class A. Classes that overwrite this function further down the inheritance tree will get their specific behavior and classes that don't get the default.

Upvotes: 1

Related Questions