how to design base class?
-
today I discuss with my friends .we conculde that there is two ways design base class . 1 .we design the method whether belongs to all class or only belongs to sub-class into base-class the code writed like that
class CVehicle
{
public:
virtual void fly()
{
cout<<"I'm sorry I don't have method fly"<2 .we think that sub class has its own method so we should design like that
class CVehicle
{
public:};
class CAeroplane:public CVehicle
{
public:
void fly()
{
cout<<"I am a Aeroplane i can fly "<so which way will the best way we should order ?
-
today I discuss with my friends .we conculde that there is two ways design base class . 1 .we design the method whether belongs to all class or only belongs to sub-class into base-class the code writed like that
class CVehicle
{
public:
virtual void fly()
{
cout<<"I'm sorry I don't have method fly"<2 .we think that sub class has its own method so we should design like that
class CVehicle
{
public:};
class CAeroplane:public CVehicle
{
public:
void fly()
{
cout<<"I am a Aeroplane i can fly "<so which way will the best way we should order ?
wan.rui@qq.com wrote:
so which way will the best way we should order ?
2. A base class should only contain members that are common to those which will derive it.
Signature construction in progress. Sorry for the inconvenience.
-
today I discuss with my friends .we conculde that there is two ways design base class . 1 .we design the method whether belongs to all class or only belongs to sub-class into base-class the code writed like that
class CVehicle
{
public:
virtual void fly()
{
cout<<"I'm sorry I don't have method fly"<2 .we think that sub class has its own method so we should design like that
class CVehicle
{
public:};
class CAeroplane:public CVehicle
{
public:
void fly()
{
cout<<"I am a Aeroplane i can fly "<so which way will the best way we should order ?
The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:
class CVehicle
{
public:
virtual void Move();
}class CCar: public CVehicle
{
public:
void Move()
{
cout << "I am a car. I can be driven" << endl;
}
}
class CBike: public CVehicle
{
public:
void Move()
{
cout << "I am a bike. I can be ridden" << endl;
}
}class CAeroplane: public CVehicle
{
public:
void Move()
{
cout << "I am an aeroplane. I can be flown" << endl;
}
}*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:
class CVehicle
{
public:
virtual void Move();
}class CCar: public CVehicle
{
public:
void Move()
{
cout << "I am a car. I can be driven" << endl;
}
}
class CBike: public CVehicle
{
public:
void Move()
{
cout << "I am a bike. I can be ridden" << endl;
}
}class CAeroplane: public CVehicle
{
public:
void Move()
{
cout << "I am an aeroplane. I can be flown" << endl;
}
}*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
I mean there are some different action in different subclass ,where i can't conclude a common interface like move() . my question is when we at this situation we write all different method in base class?or put the different method in different sub-class?
-
The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:
class CVehicle
{
public:
virtual void Move();
}class CCar: public CVehicle
{
public:
void Move()
{
cout << "I am a car. I can be driven" << endl;
}
}
class CBike: public CVehicle
{
public:
void Move()
{
cout << "I am a bike. I can be ridden" << endl;
}
}class CAeroplane: public CVehicle
{
public:
void Move()
{
cout << "I am an aeroplane. I can be flown" << endl;
}
}*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
and i would like to store subclass with baseclass pointer.maybe we can dynamic_cast it to subclass and then to visit his method?or we could use base class pointer to visist method.
-
The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:
class CVehicle
{
public:
virtual void Move();
}class CCar: public CVehicle
{
public:
void Move()
{
cout << "I am a car. I can be driven" << endl;
}
}
class CBike: public CVehicle
{
public:
void Move()
{
cout << "I am a bike. I can be ridden" << endl;
}
}class CAeroplane: public CVehicle
{
public:
void Move()
{
cout << "I am an aeroplane. I can be flown" << endl;
}
}*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
+5. That solution is working safe.
regards Torsten When I'm not working
-
I mean there are some different action in different subclass ,where i can't conclude a common interface like move() . my question is when we at this situation we write all different method in base class?or put the different method in different sub-class?
If you can't ask a common question, how can you interact with the base class? Answer: you can't. If you want something to move, no further questions asked, provide a move() method (possibly abstract/pure virtual) and override it to do the actual movement in subclasses. If you need to know the type of something before asking it the question, then you're probably asking the wrong question.