1. Trang chủ
  2. » Giáo án - Bài giảng

Kỹ thuật lập trình hệ cơ điện tử= programming engineering in mechatronics chapter iii object oriented programming in c++

165 0 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

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 Chapter III Object-oriented programming in C++ ❖ Introduction to the object-oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates Chapter III Object-oriented programming in C++ ❖ Introduction to the object-oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates Introduction to the object-oriented world ❖ Basics ❖ Basic principles ❖ An object-oriented example code 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 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 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++ Introduction to the object-oriented world ❖ Basics ❖ Basic principles ❖ An object-oriented example code 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 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 Class templates ❖ Value parameters and default template parameters • Example 151 Class templates ❖ Value parameters and default template parameters • Example 152 Class templates ❖ A step-be-step tutorial for creating and using class templates ❖ Defining a generic class ❖ Instantiation and specialisation ❖ Value parameters and default template parameters ❖ The "friends" and static data members of a class template ❖ The Standard Template Library (STL) of C++ 153 Class templates ❖ The "friends" and static data members of a class template • A class template may also have friends, which may behave differently 154 Class templates ❖ The "friends" and static data members of a class template 155 Class templates ❖ The "friends" and static data members of a class template 156 Class templates ❖ A step-be-step tutorial for creating and using class templates ❖ Defining a generic class ❖ Instantiation and specialisation ❖ Value parameters and default template parameters ❖ The "friends" and static data members of a class template ❖ The Standard Template Library (STL) of C++ 157 Class templates ❖ The Standard Template Library (STL) of C++ ▪ The structure of STL The elements of the Library can be grouped into five groups: • containers – data structures making it possible to store data in memory (vector, list, map, set, deque, …) • adaptors – higher-level data structures based on containers (stack, queue, priority_queue) • algorithms - operations that can be carried out on data stored in containers (sort, copy, search, min, max, …) 158 Class templates ❖ The Standard Template Library (STL) of C++ ▪ The structure of STL The elements of the Library can be grouped into five groups: • iterators – generic pointers that ensure access to the data stored in containers (iterator, const_iterator, ostream_iterator, … ) • function objects – functions are covered by classes, for other components (divides, greater_equal, logical_and, …) 159 Class templates ❖ The Standard Template Library (STL) of C++ ▪ The structure of STL Short description Header file Managing, sorting data in containers and searching in them Associative container for storing bits: bitset Associative containers that store elements: multiset (may have the same elements more times), and set (stores only unique elements) Associative container storing key/value pairs in a 1:1 relation (map), or in a 1:n relation (multiset) Predefined iterators, datastream iterators Container: dynamic array Container: double ended queue Container: linear list Container adaptor: queue Container adaptor: stack 160 Class templates ❖ The Standard Template Library (STL) of C++ ▪ STL and C++ arrays 161 Class templates ❖ The Standard Template Library (STL) of C++ ▪ STL and C++ arrays 162 Class templates ❖ The Standard Template Library (STL) of C++ ▪ Using STL containers 163 Class templates ❖ The Standard Template Library (STL) of C++ ▪ Using STL container adaptors • The adapted stack functions are summarised in the following table: void push(const value_type& a) inserting in the stack, void pop() removing the top element of the stack, value_type& top() accessing the top element of the stack, const value_type& top() const accessing the top element of the stack, bool empty() const returns true, if the stack is empty, size_type size()const the number of elements in the stack, operator== and operator< the operations "equals" and "smaller than" 164 Class templates ❖ The Standard Template Library (STL) of C++ ▪ Using STL container adaptors 165 .. .Chapter III Object- oriented programming in C++ ❖ Introduction to the object- oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates Chapter III Object- oriented. .. Object- oriented programming in C++ ❖ Introduction to the object- oriented world ❖ Classes and objects ❖ Inheritance (derivation) ❖ Polymorphism ❖ Class templates Introduction to the object- oriented. .. message passing is realised by calling a method in C++ Introduction to the object- oriented world ❖ Basics ❖ Basic principles ❖ An object- oriented example code Introduction to the object- oriented

Ngày đăng: 15/02/2022, 19:02

Xem thêm:

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN