element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ Tutorial - Inheritance
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: oneleggedredcow
  • Date Created: 23 Apr 2013 11:38 PM Date Created
  • Views 1549 views
  • Likes 1 like
  • Comments 0 comments
  • coding
  • raspbpi
  • code_exchange
  • Code Exchange
  • raspberry_pi
  • code
  • rpi
  • c++
  • learnc++
Related
Recommended

C++ Tutorial - Inheritance

oneleggedredcow
oneleggedredcow
23 Apr 2013

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.

Tutorial Index

01 - Hello Raspberry Pi

02 - Reading User Input

03 - If Statement

04 - For Loops

05 - While Loops

06 - Functions

07 - Data Types

08 - Arrays

09 - Structures

10 - Classes

11 - Pointers

12 - Dynamic Arrays

13 - Linked List

14 - Linked List Operations

15 - STL List/Vector

16 - Templates

17 - Inheritance

18 - File I/O

19 - Strings

20 - Constants

21 - Hash Tables

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube