Lesson 13 - Inheritance in C++
In the previous lesson, Assignment operator, we defined the basic methods that a usual
class should have. In today's tutorial, we're going to expand our knowledge of
object-oriented programming. In the introduction of this course, we mentioned
that the OOP is based on three fundamental concepts:
encapsulation, inheritance and
polymorphism. We're already familiar with the encapsulation and
have used the
private access modifier. Today, we're going to be
taking a look at the inheritance.
Inheritance is one of the basic features of OOP and is used to create new data structures based on old ones. Let's look at a simple example:
Imagine that we were hired to create a database that will keep track of
animals at a zoo There will
be two types of users in our system: users and administrators. Users will be
common zookeepers who will be able to edit information about the animals, e.g.
their weight or wingspan. The administrator will be able to modify animal data
as well and add or delete animals from the database. Administrators will have an
extra field, a phone number so they can be contacted in case of system failure.
It'd be pointless and confusing to fully define both classes since they have
many fields and methods in common. Both the user and the administrator will have
names, ages and be able to log in and log out. Let's define only the
...End of the preview...
ict.social is a large database made up of manuals and tutorials, whose main goal is to provide high-quality IT education to everyone. We started out in the Czech republic, where we display roughly a million articles per month and receive plenty of gratitude from our users. Thanks to our successful establishment, we are now bringing these articles to the rest of the world.
Although we are trying to keep our content free of charge, maintaining the site is a huge effort for everyone involved. Therefore, some content (exercises and more advanced material) costs network points. Don't worry, they're really cheap :)
Requested article covers this content:
In this tutorial, we'll explain the fundamentals of object inheritance in C++. We'll learn to reuse code and use inheritance correctly.
Buying this article gives you unlimited access to it forever. You will learn some more and help us keep giving our site maintenance which helps you and others get better futures. It's a win-win.
You gain points by supporting our network. This is done by sending a helpful amount of money to support the site, or by creating content for the network.
You can get points immediately using:
|Credit card||SMS||Wire transfer|