Introduction
In the previous tutorial, we saw how to use templates to make functions and classes more generic. This enables you to write a function or class that works with many different data types.
In this tutorial, we are going to learn about inheritance. Inheritance allows a class to inherit another class’s methods and variables, which allows for more code reuse.
Code
Let’s start with an example:
typedef struct Point
{
double X;
double Y;
Point()
{
X = 0;
Y = 0;
}
Point(double x, double y)
{
X = x;
Y = y;
}
} Point;
class SimplePolygon
{
protected:
int _nsides;
Point* _vertices;
public:
SimplePolygon(int nsides)
{
_nsides = nsides;
_vertices = new Point[nsides];
}
~SimplePolygon()
{
delete[] _vertices;
}
Point GetVertex(int idx)
{
return _vertices[idx];
}
void SetVertex(int idx, Point point)
{
_vertices[idx] = point;
}
};
class Triangle : public SimplePolygon
{
public:
Triangle(Point a, Point b, Point c)
: Polygon(3)
{
SetVertex(0, a);
SetVertex(1, b);
SetVertex(2, c);
}
};
So, the first thing that we have is a little helper structure that stores an X, Y point called Point. There isn’t anything too exciting there, and if there is, you should check out the tutorial on structures. Next we define a class called SimplePolygon. A polygon is a two dimensional closed shape consisting of n-sides (and hence n-vertices). A simple polygon adds the restriction that no two sides of the polygon may intersect. Some common examples of a simple polygon are: triangles, squares, rectangles, octagons, etc. Since the triangle is a more specific example of a simple polygon, this is a good chance for inheritance.
The way to express inheritance is through the use of a : after the class name. So, this line:
class Triangle : public SimplePolygon
translates to “the Triangle class inherits from the SimplePolygon class”. Now the triangle has all of the methods that the SimplePolygon class has, plus and methods that it defines. Letting us do something like this:
int main()
{
Triangle triangle(Point(0, 0), Point(5, 0), Point(0, 9));
Point top = triangle.GetVertex(2);
cout << top.X << " " << top.Y << endl;
return 0;
}
The other thing to note is that inherited classes have access to the protected variables stored on the class that they inherit from. This means that we can access the _nsides and the _vertices variable from the Triangle class. Remember that private variables and methods can only be accessed by the class that defines them. public variables and methods can be accessed by anyone, and protected variables can only be accessed from the class that defines them and anyone who inherits from them.
Now, the main reason for inheritance is so that we can reuse methods and variables for things that are similar. In order to do this, let’s define some more classes:
class Quadrilateral : public SimplePolygon
{
public:
Quadrilateral(Point a, Point b, Point c, Point d)
: SimplePolygon(4)
{
SetVertex(0, a);
SetVertex(1, b);
SetVertex(2, c);
SetVertex(3, d);
}
};
class Square : public Quadrilateral
{
public:
Square(double length, Point center)
: Quadrilateral(Point(center.X - length/2, center.Y - length/2),
Point(center.X + length/2, center.Y - length/2),
Point(center.X + length/2, center.Y + length/2),
Point(center.X - length/2, center.Y + length/2))
{
}
};
class Rectangle : public Quadrilateral
{
public:
Rectangle(double width, double height, Point center)
: Quadrilateral(Point(center.X - width/2, center.Y - height/2),
Point(center.X + width/2, center.Y - height/2),
Point(center.X + width/2, center.Y + height/2),
Point(center.X - width/2, center.Y + height/2))
{
}
};
A quadrilateral is just a fancy name for a polygon with four sides. Two examples of quadrilateral are a square and a rectangle. You can see this reflected in the way that the classes inherit from one another.
Now we have a bunch of classes (five of them) that represent polygons. Say we wanted to compute the area of the given polygon. We could write a method on each one of the classes that computed its area. For example, the area of a square is length squared and the area of a rectangle is width times height and so on. However, if we put a method on the SimplePolygon class that computed its area, then all of the other shapes: triangle, quadrilateral, square, and rectangle would all inherit it. Therefore we only have to write one area method for any and all types of SimplePolygon. So, here it is:
double Area()
{
double A = 0;
for(int i = 0; i < _nsides; i++)
{
Point curr = _vertices[i];
Point next = _vertices[(i + 1)%_nsides];
A += curr.X * next.Y - next.X * curr.Y;
}
return A / 2;
}
To demonstrate the method, we create various shapes and verify that the area is computed correctly:
int main()
{
Triangle triangle(Point(0, 0), Point(5, 0), Point(0, 9));
cout << triangle.Area() << endl;
Square square(5, Point(1, 2));
cout << square.Area() << endl;
Rectangle rectangle(2, 4, Point(3, 4));
cout << rectangle.Area() << endl;
return 0;
}
Great success! We only had to write one area method and it worked for all types of polygons!
This is also very handy when writing methods. You can specify that you want to accept variables that are a SimplePolygon. When you say this, you mean that the variable that is passed in can be a SimplePolygon or any class that inherits from SimplePolygon (i.e. Square). This allows you to still specify a somewhat generic method (in that it can accept a few different classes as input), but still be guaranteed that certain variables and methods exist on the object (i.e. an Area method). This can greatly increase code reuse as well. As a general rule of thumb, you should try to write methods so that they can use the most generic version of a class as possible. So, if you are writing a method that would work on Quadrilateral, Square, and Rectangle, but not on Triangle or SimplePolygon, then you should write one method that takes a Quadrilateral as input.
Word of Warning
When you say that a class inherits from another class it gets all of the variables and methods on the class. Therefore the inheriting class must be a subset of the class that it inherits from. This is fairly obvious in cases like the one above where the definition of the object is very clear. Unfortunately, in the real world, things never seem to be this clear. So, when you have one class inherit from another one, you need to make sure that all of the variables and methods make sense. This is a difficult task that requires much planning and foresight. It is very common to get a little inheritance happy and end up having a Duck class (quack, quack) with a variable on it called NumberOfWheels or something equally silly.
Summary
In this tutorial, we learned about inheritance and how it can save you from writing similar code.
In the next tutorial, we will be learning about how to read and write to a file.
If you have any questions or comments about what was covered here, post them to the comments. I watch them closely and will respond and try to help you out.