Trang 1
TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ Programming Engineering in Mechatronics Giảng viên: TS. Trương Công Tuấn Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí 1
TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ Programming Engineering in Mechatronics Giảng viên: TS. Trương Công Tuấn Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí 1
Chapter III. Object-oriented programming in C++ Introduction to the object-oriented world Classes and objects Inheritance (derivation) Polymorphism Class templates 2
Chapter III. Object-oriented programming in C++ Introduction to the object-oriented world Classes and objects Inheritance (derivation) Polymorphism Class templates 3
Introduction to the object-oriented world Basics Basic principles An object-oriented example code 4
Introduction to the object-oriented world Basics 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: 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. • Both the integrated properties and the methods of a class are called class members (data member and member function in C++). 5
Introduction to the object-oriented world Basics Object • An object is a scheme (an example) of a class: the class Truck has the feature brake system, but the brake system of the object myCar can be an electronicallycontrolled 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: 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. 6
Introduction to the object-oriented world Basics Method • Methods are responsible for the capabilities of objects: the methods of myCar: Brake(), Ignition(), StepOnTheGas(), TurnLeft() or TurnRight(). • In C++, methods are rather called member functions. 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 object driver sends a "brake" message to activate the Brake() method of myCar, which results in braking the vehicle. • On the code level, message passing is realised by calling a method in C++. 7
Introduction to the object-oriented world Basics Basic principles An object-oriented example code 8
Introduction to the object-oriented world Basic principles Encapsulation, data hiding • classes principally consist of features (state) and methods (behaviour) • 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. 9
Introduction to the object-oriented world Basic principles 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. Inheritance 10
Introduction to the object-oriented world Basic principles Inheritance • 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. 11
Introduction to the object-oriented world Basic principles 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. • 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. 13
Introduction to the object-oriented world Basic principles 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. 14
Introduction to the object-oriented world Basic principles 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. 15
Introduction to the object-oriented world Basics Basic principles An object-oriented example code 16
Chapter III. Object-oriented programming in C++ Introduction to the object-oriented world Classes and objects Inheritance (derivation) Polymorphism Class templates 18
Classes and objects 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. 19
Hiển thị 19 trang đầu. Tải tài liệu để xem đầy đủ.
Chưa có bình luận nào. Hãy là người đầu tiên!
Bình luận (0)
Chưa có bình luận nào. Hãy là người đầu tiên!