overload or inherit

E

epanda

Hi,

I would like to merge two class which have got 90% of same source
code. But I don't know what to do.

class A {

int putLightOn () {

switch(status)
{
case iAmOff :
print "I am off" ;
print "put me on!!!\n" ;
}
}

}


class B {

int putLightOn () {

switch(status)
{
case iAmOff :
print "I am off" ;
print "and I am a red light" ; <<<< the only difference
between A and B
print "put me on!!!\n" ;
}
}

}


I would like that B inherit from A but I don't know how re writing B's
putLightOn function

Can you help me?
Thanks
 
J

James Kanze

I would like to merge two class which have got 90% of same
source code. But I don't know what to do.
class A {
int putLightOn () {
switch(status)
{
case iAmOff :
print "I am off" ;
print "put me on!!!\n" ;
}
}
}
class B {
int putLightOn () {
switch(status)
{
case iAmOff :
print "I am off" ;
print "and I am a red light" ; <<<< the only difference
between A and B
print "put me on!!!\n" ;
}
}
}
I would like that B inherit from A but I don't know how re
writing B's putLightOn function

As Kevin said, it largely depends on whether you want the two
classes to behave polymorphically nor not. If so, look into the
Template Method pattern or the Strategy pattern. If not,
consider using variations on mixins.
 

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
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top