C++/Classes and Inheritance

Classes are the building blocks of programs built using the object-oriented methodology. Such programs consist of independent, self-managing modules and their interactions. An object is an instance of such a module, and a class is its definition.

Abstract example
To illustrate classes and objects, we will use a dog.

Dog:

char name int gender int age int size bool healthy

A dog has several variables: name, the gender of the dog, the age of the dog, its size, and whether it is healthy or not. These variables are called properties. This definition would go in a class. An instance of a dog, say, a dog named Lucy, would be an object. So would a dog named Ruffy. You can have multiple instances of a class, just like you can have multiple dogs. Properties are like "inner variables" of each object made of type Dog.

struct Dog
We have just defined what a dog is. Let us make a couple of dogs...

In this short example, we have created two objects of the type Dog. One is lucy, and the other is ruffy. We have set lucy's gender to 1, and ruffy's gender to 2. Also, we've set lucy's age to 3, and ruffy's to 5. However, having such information publicly accessible can be dangerous. A bug in your program, for example, could cause the unintended change of the properties of one of the dogs! You wouldn't want a bug in your software to cause lucy to be sick, would you?

class Dog
Structs are containers whose properties are always public. In order to gain control over what is public and what isn't, we will have to use a class. Let us see how the Dog class would look like in a class instead of a struct:

Now our dogs' precious properties aren't public for everyone to view and change. However, this raises a little problem: we can't change them either. Only other dogs can see and change them. In order to be able to access a Dog's private properties, we would have to make a function to do so. This brings us to our next chapter.

Dogs with Methods
Classes can have functions in them. These functions are called methods. Methods can access all, and even private, properties and methods (yes, methods can be private) of its class. Let us make some methods to get our dog's information...

However, if we run this program, we might have a little problem. We have never initialized lucy's properties...

Constructors
In order to initialize lucy's properties, we could write a method that would do it for us, and call it every time we make a dog. However, C++ already provides us such a method. A constructor is a method which is called every time a new object is created. To use this nice little thing provided to us by C++, we will have to write a method which returns no value, and has the same name as the class. Here's Dog written using a constructor:

From now on, every dog we instantiate will have the name "Lucy", be of gender 1, aged 3, sized 4 and healthy. Well, what if you want some diversity? No problem. Constructors with parameters!

If you have both constructors in your Dog class, you can now either create a dog as always, which will have the default values (named "Lucy" etc etc), or you could use RAII to make your customized dog as follows:

Inheritance
The inheritance mechanism is a core component of the Object Oriented Programming paradigm. Inheritance means that a class "inherits" another class's definition and type. In the previous chapter, we've covered the "HAS-A" relationship. A dog HAS-A name. In this chapter, we will cover the IS-A relationship. A dog IS-A pet.

Abstract example
A pet is a wonderful concept, don't you think? It's an abstract concept, but we will not cover abstraction here. There are many kinds of pets (dogs, cats, horses...), and they all have some things in common. Pet:

char* name;

char* getName; void voice; void interact(Pet* other); void annoy; Pet(char* dname);

Implementation of Pet
As we already know classes, it shouldn't be a problem to implement Pet in C++. Notice anything different? Yep, the  keyword. This keyword tags a method as overridable. Inheriting classes can override inherited methods. I'll explain it in a moment.

Inheritance
OK. We've got a Pet with a "DEFAULT VOICE". However, a dog barks. Well, we could make a dog class, however note that interact receives a pointer to a Pet. As Pet and Dog are two different classes, pets will be unable to interact with dogs. That's not what we want to do, so we need to somehow make Dog a Pet. One word: Inheritance. Remember how I told you that it'd inherit its type? This is what I have meant. Inheritance is done using the ':' operator, as such:

Overriding
Now we can make Dogs, and they would all have unique names, they could voice, interact with other pets and annoy their lovely owner. However, there's yet one problem. They still have the annoying "DEFAULT VOICE". Well now that we inherit from Pet, we can override its virtual methods. voice is a virtual method, so why not do that now? Overriding looks very similar to declaring a new method, so watch out.

Constructors and inheritance
Now our Dog class functions as intended. However, there's yet one complication... We haven't got a constructor yet.

Constructor calling constructors
Now that Pet knows how to initialize itself, Dog needs to too. Dog can not directly modify its name, so it has to make Pet do it for him. If Pet would have had a constructor that took no arguments, there wouldn't have been a problem and the process would have been automatic. However, Pet has no "default constructor" (a constructor that takes no arguments), and Dog is in a little difficult situation. I'll save you the trouble. This is how it's done: Here we have Dog's constructor getting a desired name, and passing it to Pet's constructor to do the work.

Inheritance in a program
Output: Wooflemeister: Woof! Michael annooys you. Michael: DEFAULT VOICE Wooflemeister: Woof! Wooflemeister annoys you.

Exercise
A family car has a color, a maximum speed and a sunroof. The sunroof can be opened and closed. A cab's color is always yellow and it has a maximum speed. A bicycle has a color, a maximum speed and pedals that can be spun. A truck has a color, a maximum speed, and it can carry a cab or a family car, but not a bicycle. A road can contain any of the above. A policeman has a hat, and can block a road or any vehicle. Tip: You can chain inheritance, and create trees.
 * Design and implement a system of vehicles.