• Nem Talált Eredményt

Introduction to the object-oriented world

In document Mechatronic Systems Programming in C++ (Pldal 174-178)

III. fejezet - Object-oriented programming in C++

1. Introduction to the object-oriented world

Object-oriented programming uses the "things" ("objects") and the interaction between them to plan and to develop applications and computer programs. This methodology comprises solutions, like encapsulation, modularity, polymorphism and inheritance.

It should be noted that OOP languages only offer tools and support in order that object-orientation be realised.

In the present book, we will also focus on presenting these tools after a short overview.

1.1. Basics

Now let's have a look at the basic notions of object-orientation. To understand these, we do not even have to have much knowledge in programming.

Class

A class determines the abstract features of a thing (object), including its features (attributes, fields, properties) and its behaviour (what the thing can do, methods, operations and functions).

We can say that a class is a scheme describing the nature of something. For example, the class Truck has to contain the common properties of trucks (manufacturer, engine, brake system, maximum load etc.), as well as their specific behaviour, like braking, turning to left etc.

Classes offer by themselves modularity and structuredness for object-oriented computer programs. The notion of class should also be understandable for those people who are familiar with the given problem but not familiar with programming that is the features of a class should be expressive. The code of a class should be relatively independent (encapsulation). Both the integrated properties and the methods of a class are called class members (data member and member function in C++).

Object

An object is a scheme (an example) of a class. With the help of the class Truck, we can define all possible trucks by enumerating their properties and behaviour forms. For example, the class Truck has the feature brake system, but the brake system of the object myCar can be an electronically controlled one (EBS) or a simple air brake.

Instance

The notion of instance (of a class) is synonymous with the notion of object. Instance means an actual object created at runtime. So, myCar is an instance of the class Truck. The set of the property values of the actual object is called the state of that object. Therefore all objects are characterised by the state and behaviour defined in their corresponding class.

Method

Methods are responsible for the capabilities of objects. In spoken language, the corresponding term for methods is verbs. Since myCar is a Truck, it has the ability of braking, so Brake() is one of the methods of myCar. Of course, it may have other methods as well, like Ignition(), StepOnTheGas(), TurnLeft() or TurnRight(). Within a program, a method has only effect on the given object in general when it is used. Although all trucks can brake,

activating (calling) the method Brake() will only slow down one given vehicle. In C++, methods are rather called member functions.

III.1. ábra - The object myCar (an instance of the class Truck)

Message passing

Message passing is the process during which an object sends data to another object or "asks" another object to execute one of its methods. The role of message passing can be better understood if we think of the simulation of the functioning of trucks. In that simulation, the object driver sends a "brake" message to activate the Brake() method of myCar, which results in braking the vehicle. The syntax of message passing may be very different among different programming languages. On the code level, message passing is realised by calling a method in C++.

1.2. Basic principles

The object-orientation of a system, of a programming language can be measured by supporting the following principles. If it is only some of these principles that are realised, that is an object-based system, if all four principles are supported, that is an object-oriented system.

1.2.1. Encapsulation, data hiding

As we have already seen, classes principally consist of features (state) and methods (behaviour). However, the state and behaviour of objects can be divided into two groups. There are some features and methods that we hide from other objects. These are internal (private or protected) states and behaviour. However, the others are made public. According to the basic principles of OOP, the state features have to be private while most of the methods may be public. If it is needed, public methods can be used to access private features in a controlled way.

It is not necessary for an object that passes a message to another object to know the inner structure of the latter.

For example, the Truck has the method Brake(), which exactly defines how braking takes place. However, the driver of myCar does not have to know how that car brakes.

All objects provide a well-defined interface for the external world. That interface defines what can be accessed from the given object from the outside. If this interface is completely written, there is not any problem to modify the internal world of the class in the future for client applications using that object. For example, it can be ensured that trailers may only be connected to objects of the class HeavyTruck.

1.2.2. Inheritance

Inheritance means creating specific versions of a class that inherit the features and behaviour of their parent class (base class) and use them as if they were of their own. The classes created in this way are called subclasses or derived classes.

For example, the subclasses Van and HeavyTruck are derived from the class Truck. In the following, myCar will be an instance of the class HeavyTruck. Let's also suppose that the class Truck defines the method Brake() and the property brake system. All classes derived from that class (Van and HeavyTruck) inherit these members, so programmers have to write only once the code corresponding to them.

III.2. ábra - Inheritance

Subclasses may change the inherited properties. For example, the class Van can prescribe that its maximum load is 20 tons. The subclass HeavyTruck may make EBS braking as its default for its method Brake().

Derived classes can be extended with new members as well. The method Navigate() can be added to the class HeavyTruck. On the basis of what has been said so far, the method Brake() of the given instance HeavyTruck uses an EBS-based brake in spite of the fact that it inherits a traditional Brake() method from the class Truck; it also has a new method named Navigate(), which cannot be found in the class Van.

Actually, inheritance is an is-a relation: myCar is a HeavyTruck, a HeavyTruck is a Truck. So myCar has the methods of both HeavyTruck and Truck.

Both derived classes have one direct parent class, namely Truck. This inheritance method is called single inheritance to be differentiated from multiple inheritance.

Multiple inheritance means that a derived class inherits the members of more direct parent classes. For example, we can define two classes totally independent of each other with the names Truck and Ship. From these classes, we can create a new class named Amphibious, which has the features and behaviour of both trucks and ships.

Most programming languages (ObjectPascal, Java, C#) support only single inheritance; however, C++ offers both inheritance methods.

III.3. ábra - Multiple inheritance

1.2.3. Abstraction

Abstraction simplifies complex reality by modelling problems with their corresponding classes and it has its effects on the level of inheritance appropriate for these problems. For example, myCar can be treated as a Truck in most cases; however, it may also be a HeavyTruck as well if the specific features and behaviour of HeavyTruck are needed but it can also be considered as a Vehicle if it is treated as a member of a fleet of vehicles. (Vehicle is the parent class of Truck in the example.)

Abstraction can be achieved through composition. For example, a class named Car has to contain the components engine, gearbox, steering gear and many others. In order to construct a Car, we do not have to know how the different components work, we only have to know how to connect to them (i.e. its interface). An interface determines how to send them or receive from them messages and it gives information about the interaction between the components of the class.

1.2.4. Polymorphism

Polymorphism makes it possible to replace the content of some inherited (deprecated) behaviour forms (methods) with a new one in the derived class and to treat the new, replaced methods as the members of the parent class.

For the next example, let's suppose that the classes Truck and Bicycle inherit the method Accelerate() of the class Vehicle. In the case of a Truck, the command Accelerate() means the operation StepOnTheGas(), whereas in the case of a Bicycle it means calling the method Pedal(). In order that acceleration function correctly, the method Accelerate() of the derived classes should override the method Accelerate() inherited from the class Vehicle. This is overriding polymorphism.

Most OOP languages support parametric polymorphism as well where methods are written for compilers as schemes independently of types. In C++, this can be carried out with templates.

1.3. An object-oriented example code

Finally, let's see a C++ code written on the basis of the things said so far. Now, we mainly aim at giving readers some impressions about OOP, since OOP is only detailed in the following subchapters of the present book.

#include <iostream>

#include <string>

using namespace std;

class Truck { protected:

string manufacturer;

string engine;

string brake_system;

class HeavyTruck : public Truck { public:

Van post("ZIL", "Diesel", "airbrake");

post.Brake(); // Classic braking.

HeavyTruck myCar("Kamaz", "gas engine", "EBS", 40);

myCar.Brake(); // Brake with EBS. become able to define abstract data types. C++ also offers us the new class type.

The types struct and class are built up of data members and related operations (member functions). Classes can be created by both data types; however, it is the class type that observes more the principles of obejct- orientation because of the default access restriction of its members. By default, all members of a type struct is public, whereas the members of a type class can only be accessed by the member functions of that class.

A class declaration has two parts. The header of the class contains the keyword class/struct, followed by the name of the class. The header is followed by the class body, which is enclosed within curly brackets followed by a semi-colon. Declarations contain not only data members and member functions but also keywords regulating access to the members and followed by a colon: public, private (hidden) and protected.

class ClassName { public:

type4 Function1(parameterlist1) { } type5 Function2(parameterlist2) { } protected:

type3 data3;

private:

In document Mechatronic Systems Programming in C++ (Pldal 174-178)