➢ 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 b
Trang 1KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ Programming Engineering in Mechatronics
1
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
Trang 4❖ Basic principles
❖ An object-oriented example code
Trang 5▪ 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
Trang 6▪ 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.
Trang 7▪ 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++
Trang 8❖ Basic principles
❖ An object-oriented example code
Trang 9▪ 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.
Trang 10▪ 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
Trang 11▪ 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.
members of more direct parent classes
Trang 12▪ Inheritance
Multiple inheritance
Trang 13▪ 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
Trang 14▪ Abstraction
Trang 15▪ 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
Trang 16❖ Basic principles
❖ An object-oriented example code
Trang 1717
Trang 19➢ 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.
Trang 2020
Trang 21❖ More about classes
❖ Operator overloading
Trang 22▪ A little revision
Trang 23▪ Grouping together data and operations
Trang 24▪ Data hiding
• In object-oriented programming it is required that the data
members of classes could not be accessed directly from the
outside
• The type struct offers complete access to its members by
default, whereas the class type completely hides its members
from the outside
• The access of class elements can be defined by programmers as
well with the keywords private, protected and public.
Trang 25▪ Data hiding
Trang 26▪ Constructors
• A constructor is a member function the name of which
corresponds to the name of the class and has no return type.
• A constructor only has to initialise the memory space already
allocated for the object
• A class has two constructors by default: a constructor without
parameters (default) and a copy constructor
Trang 27▪ Constructors
Trang 28▪ Constructors
Trang 29▪ Constructors
• Constructors with and without parameters are often contracted
by introducing default arguments:
Trang 30▪ Constructors
• Using member initialisation lists
Trang 31▪ Constructors
• Explicit initialisation of objects
Trang 32▪ Destructor
• C++ offers a special member function, the destructor, in which
we can free the allocated resources
• The name of a destructor has to be provided as a class name
with the tidle character (~)
• A destructor, just like constructors, does not return any value
Trang 33▪ Destructor
Trang 34▪ Destructor
• If a destructor is not written for a class, the compiler
automatically adds an empty destructor for that class
Trang 35▪ Objects of a class, the pointer this
The class Employee and its objects
Trang 36▪ Objects of a class, the pointer this
• Each member function has an invisible parameter (this) in which
a pointer to the actual object is passed to the function when it is called
• All references to data members are inserted in a program code
automatically in the following way:
Trang 37▪ Objects of a class, the pointer this
• Programmers may also use the pointer this within member
functions
Trang 38❖ More about classes
❖ Operator overloading
Trang 39▪ Static class members
• A static data member that is created in only one instance belongs
directly to the class; therefore it is available for it even if there
are no objects for that class
• A static data member should not be initialised within its class
(independently of its access restriction)
• If a static data member is public, then it can be used anywhere in
the program code by the name of the class and the scope
operator (::)
Trang 40▪ Static class members
Trang 41▪ Static class members
Trang 42▪ How to structure classes
• Implicit inline member functions
Trang 43▪ How to structure classes
• Class structures in C++/CLI applications
Trang 44▪ How to structure classes
• Storing member functions in separate modules
Trang 45▪ Friend functions and classes
• The friend mechanism makes it possible for us to access the
private and protected members of a class from a function
outside the class
Trang 46▪ Friend functions and classes
Trang 47▪ What can we also add to classes?
• Constant data members of objects
Trang 48▪ What can we also add to classes?
• Reference type data members
Trang 49▪ What can we also add to classes?
• Data members as objects
Trang 50▪ What can we also add to classes?
• Pointers to class members
type int,
void
(Class::*pf unct )(int);
pfunct may point to a member function
that is called with an argument of
type int and that returns no value.
In order to define pointers correctly, we have to use the name of the class and the scope operator:
Trang 51▪ What can we also add to classes?
• Pointers to class members
Trang 52▪ What can we also add to classes?
• Pointers to class members
By using typedef, expressions containing pointers are easier to
be handled:
Trang 53❖ More about classes
❖ Operator overloading
Trang 54• An operator function can be used if one of its parameters is a
class of type class or struct General declaration of operator
functions:
Where the sequence op can be replaced by any of the following
operators:
Trang 55• The following operators cannot be overloaded : member
selection (.), indirect member selection (.*), scope (::),
conditional (?:) and the operators sizeof and typeid since their
overloading would result in undesired side effects
• The assignment (=), the "address of" (&) and the comma (,)
operations can be applied to objects without overloading.
• Overloading operators does not result in modifying the operator
precedence and associativity, and it is not possible to introduce
new operations
Trang 56▪ Creating operator functions
Expression Operator( ♣ ) Member function External function
Trang 57▪ Creating operator functions
• The operators =, (), [] and -> can only be overloaded by
non-static member functions
• The operators new and delete are overloaded with static
member functions
• All other operator functions can be created as member functions
or external (in general friend) functions.
Trang 58▪ Creating operator functions
• Binary operands:
Trang 59▪ Creating operator functions
• Unary operands:
Trang 60▪ Creating operator functions
Trang 61▪ Creating operator functions
Trang 62▪ Creating operator functions
Trang 63▪ Using type conversion operator functions
Trang 64▪ Using type conversion operator functions
Trang 65▪ Extending classes with input/output operations
• To "teach" I/O data streams based on classes to handle the
objects of user-defined classes
Trang 66▪ Extending classes with input/output operations
Trang 67▪ Extending classes with input/output operations
Trang 68❖ Classes and objects
❖ Inheritance (derivation)
❖ Polymorphism
❖ Class templates
68
Trang 69• Derivation means that a new class inherits the public and
protected properties (data members) and behaviour (member
functions) of already existing classes and it then uses them as its own
Trang 70➢ already existing classes may be extended with a new class,
➢ new data members and member functions may be defined
➢ or inherited member functions may be reinterpreted (replaced) if they become deprecated concerning their functioning
(polymorphism).
Trang 71• class A, that is derived or from which
members are inherited: base class , ancestor
class , parent class, superclass
• the operation: inheritance , derivation ,
extending, subclassing
• class B, the result of derivation: descendant
class, derived class, extended class, child
class, subclass
Trang 72above:
class ClassA {//
};
class ClassB : public ClassA {//
};
Trang 73The multiple inheritance of I/O classes in C++
Trang 74Hierarchy of geometrical classes
Trang 75❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
Trang 76• A derived (descendant) class is a class that inherits its data
members and member functions from one or more already
defined class(es).
• The class from which a derived class inherits is called base class
(ancestor class)
• A derived class inherits all the members of its base class;
however, it only have access to the public and protected
members of its base class as its own
Trang 77• The place where a derivation is indicated in a program code is
the class header where the mode of derivation (public,
protected, private) is indicated before the names of base classes:
Trang 7878
Trang 7979
Trang 80• The keywords public, protected and private used in a derivation
list restrict the access of inherited (public and protected)
members in their new classes:
Mode of inheritance Access in the base class Access in the derived class
protected
public protected
protected
protected protected
protected
private
private
Trang 81• The access of any member (the access type of which is protected
or public in the base class) can be manually set directly.
Trang 82❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
Trang 83• In order that base class(es) be initialised, it is the extended
version of member initialisation lists that is used.
Trang 84❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
Trang 85▪ Accessing inherited members
Trang 86▪ Accessing inherited members
The members of the base
Point2D()::PrintOut(), GetPoint3D(), Move(int…), Move(const…), PrintOut()
Trang 87▪ The friend relationship in inheritance
• In a derived class, a friend of the base class can only access the
members inherited from the base class
• A "friend" of a derived class can only access public and
protected members from the base class
Trang 88❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
Trang 89• In case of multiple inheritance, it may be a problem if the same
base class appears as many instances in the derived class
• If virtual base classes are used, problems of that type can be
avoided
Trang 90Using virtual base classes
Trang 9191
Trang 9292
Trang 93❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
Trang 94• A big advantage of C++ programming language is that it supports
the reusability of program code
modifying the original one
Trang 95• If the object-oriented tools of C++ are used, there are three
approaches to choose from:
➢ The most simple and frequent reuse of a code stored in a given
class is when an object instance is created or when already
existing objects (cin , cout , string , STL etc.) are used in a
program
Trang 96➢ Another possibility is to place objects of other classes in our own
codes as member objects ➔ This method is called composition
If the new object will only contain a pointer or a reference to
other objects, it is called an aggregation.
Trang 97➢ The third solution: when a new class is created by public
derivation from other classes, then the relationship is of an
is-a type ➔ is-a derived object behis-aves exis-actly the sis-ame wis-ay is-as its
ancestor class
Trang 9898
Trang 99▪ Reuse with composition
Trang 100▪ Reuse by public inheritance
Trang 101❖ Classes and objects
❖ Inheritance (derivation)
❖ Polymorphism
❖ Class templates
101
Trang 102• Coercion polymorphism means implicit and explicit type casts
➢ In that case, the polymorphism of a given operation is made
possible by different types that may be converted if needed
• As an opposite to coercion, the so-called ad-hoc (“for that
purpose”) polymorphism is better known by the name of
function overloading
➢ In that case, a compiler chooses the appropriate function from
the variants on the basis of parameter types
Trang 103the same code with any type
➢ In C++, parametric polymorphism is realised by function and
class templates Using templates actually means reusing a C++
source code
Trang 104❖ Redefining virtual functions
❖ Early and late binding
❖ Virtual destructors
❖ Abstract classes and interfaces
❖ Run-time type informations in case of classes
Trang 105• A virtual function is a public or protected member function of
the base class
• It can be redefined in the derived class in order that the behavior
of the class would change
• A virtual function is generally called by a reference or a pointer
of a public base class, the actual value of which is determined at run-time (dynamic binding, late binding)
• Declaration of the virtual function:
Trang 107❖ Redefining virtual functions
❖ Early and late binding
❖ Virtual destructors
❖ Abstract classes and interfaces
❖ Run-time type informations in case of classes
Trang 108108
Trang 109109
Trang 110110
Trang 111• Virtual functions and public inheritance make it possible to create external functions that can be called by every object in the class hierarchy:
Trang 112❖ Redefining virtual functions
❖ Early and late binding
❖ Virtual destructors
❖ Abstract classes and interfaces
❖ Run-time type informations in case of classes
Trang 113▪ Static early binding
• During early binding, compilers integrate statically direct
member function calls into the code
Early binding example