intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 3 - TS. Nguyễn Thành Hùng

Chia sẻ: _ _ | Ngày: | Loại File: PDF | Số trang:165

2
lượt xem
1
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng "Kỹ thuật lập trình hệ cơ điện tử" Chương 3 - Lập trình hướng đối tượng trong C++, cung cấp cho sinh viên những kiến thức như: Giới thiệu về thế giới hướng đối tượng; Các lớp và đối tượng; Kế thừa (dẫn xuất); Đa hình; Mẫu lớp;...Mời các bạn cùng tham khảo!

Chủ đề:
Lưu

Nội dung Text: Bài giảng Kỹ thuật lập trình hệ cơ điện tử: Chương 3 - TS. Nguyễn Thành Hùng

  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. Nguyễn Thành Hùng Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí Hà Nội, 2020 1
  2. Chapter III. Object-oriented programming in C++ ❖ Introduction to the object-oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates 2
  3. Chapter III. Object-oriented programming in C++ ❖ Introduction to the object-oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates 3
  4. Introduction to the object-oriented world ❖ Basics ❖ Basic principles ❖ An object-oriented example code 4
  5. Introduction to the object-oriented world ❖ Basics ▪ Class • A class determines the abstract features of a 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. • Both the integrated properties and the methods of a class are called class members. 5
  6. Introduction to the object-oriented world ❖ Basics ▪ Object • An object is a scheme (an example) of a class. ▪ Instance • 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
  7. Introduction to the object-oriented world ❖ Basics ▪ Method • Methods are responsible for the capabilities of objects: the methods of myCar: Brake(), Ignition(), ... • 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. • On the code level, message passing is realised by calling a method in C++. 7
  8. Introduction to the object-oriented world ❖ Basics ❖ Basic principles ❖ An object-oriented example code 8
  9. 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
  10. 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
  11. 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. • Multiple inheritance means that a derived class inherits the members of more direct parent classes. 11
  12. Introduction to the object-oriented world ❖ Basic principles ▪ Inheritance Multiple inheritance 12
  13. 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. • Abstraction can be achieved through composition. • An interface determines how to send element or receive from element messages and it gives information about the interaction between the components of the class. 13
  14. Introduction to the object-oriented world ❖ Basic principles ▪ Abstraction 14
  15. 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. 15
  16. Introduction to the object-oriented world ❖ Basics ❖ Basic principles ❖ An object-oriented example code 16
  17. Introduction to the object-oriented world ❖ An object-oriented example code 17
  18. Chapter III. Object-oriented programming in C++ ❖ Introduction to the object-oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates 18
  19. 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 class body is enclosed within curly brackets followed by a semi-colon ➔ contain the data members, member functions, and the keywords regulating access to the members and followed by a colon: public, private (hidden) and protected. 19
  20. Classes and objects ❖General form of a class: 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
3=>0