Originally Posted by
Atomic_Sheep
So why create a banana class if I can simply put everything into the fruit class? If so just keep breaking classes down more and more, the way I see it, we'll just end up with a bunch of unique classes only good for one thing, so what's the point?
This always depends on your needs.
For example, if your program only need to work with data that is common to all fruit, says name, weight and calories, then you will only need a Fruit class and just fill the instances with appropriate data.
Separation becomes interesting when you have data specific to each type of fruit that does not apply to other types. It becomes really interesting when you have code that needs to treat different types differently.
Lets look at a different example: say you have two types of areas, circles and squares.
For both you only need one length. In the case of the circle that would be the radius, for the square the side length.
At this point you have two options:
1) store some kind of type information
2) create two distinct subclasses
1) type
class Area
{
public:
enum Type {
Circle,
Square
};
Area(Type type, int length)
: m_type(type), m_length(length)
{
switch (type) {
case Circle:
m_area = m_length * m_length * M_PI;
break;
case Square:
m_area = m_length * m_length;
break;
}
}
private:
Type m_type;
int m_length;
int m_area;
};
class Area
{
public:
enum Type {
Circle,
Square
};
Area(Type type, int length)
: m_type(type), m_length(length)
{
switch (type) {
case Circle:
m_area = m_length * m_length * M_PI;
break;
case Square:
m_area = m_length * m_length;
break;
}
}
private:
Type m_type;
int m_length;
int m_area;
};
To copy to clipboard, switch view to plain text mode
2)
class Area
{
public:
Area(int length)
: m_length(length), m_area(0)
{
}
protected:
int m_length;
int m_area;
};
class Circle : public Area
{
public:
Circle(int length) : Area(length)
{
m_area = m_length * m_length * M_PI;
}
};
// and similar for Square
class Area
{
public:
Area(int length)
: m_length(length), m_area(0)
{
}
protected:
int m_length;
int m_area;
};
class Circle : public Area
{
public:
Circle(int length) : Area(length)
{
m_area = m_length * m_length * M_PI;
}
};
// and similar for Square
To copy to clipboard, switch view to plain text mode
Now consider you want to add non-square rectangles.
For (1) you could add a second length and ignore it for circle and square.
For (2) you would just add it to the rectangle class. For (2) you can also add rectangle without having to change Area.
Using base class and subclasses also allows us to name data members more appropriately
class Area
{
protected:
Area()
: m_area(0)
{
}
protected:
int m_area;
};
class Circle : public Area
{
public:
Circle(int radius : Area(), m_radius(radius)
{
m_area = m_radius * m_radius * M_PI;
}
protected:
int m_radius;
};
class Area
{
protected:
Area()
: m_area(0)
{
}
protected:
int m_area;
};
class Circle : public Area
{
public:
Circle(int radius : Area(), m_radius(radius)
{
m_area = m_radius * m_radius * M_PI;
}
protected:
int m_radius;
};
To copy to clipboard, switch view to plain text mode
Cheers,
_
Bookmarks