EE3491 - Kỹ thuật lập trình
EE3490E – Programming Techniques
Chapter 6: Class and Objects – Part 1
Lecturer: Dr. Hoang Duc Chinh (Hoàng Đức Chính)
Department of Automation Engineering
School of Electrical and Electronics Engineering
Email:
[email protected] © HĐC 2024.1
Content
6.1. Definitions
6.2. From structure to classs
6.3. Member variables
6.4. Member functions
6.5. Access control
© HDC 2024.1 Chapter 6: Class and Objects 2
6.1. Definitions - What is an object?
It is a software entity
Model/representative of a physical object:
Tank, heater, furnace
Motor, Pump, Valve
Sensor, Thermometer, Flowmeter
Control Loop, Control System
Or a logical/conceptual object:
Trend, Report, Button, Window
Matrix, Vector, Polynomial
© HDC 2024.1 Chapter 6: Class and Objects 3
An object should have…
Attributes
State
Data
Relationship
Behaviour
Operation
Response
Identity
Semantic/responsibility
© HDC 2024.1 Chapter 6: Class and Objects 4
Class
Class is the implementation of objects which have the same:
Semantic
Attributes
Relationship
Behaviour
Class = encapsulation [Data structure + functions]
Class of vectors, matrice (element data + accessing methods + basic
arithmetic operators)
Class of rectangles (coordinate data + draw, erase, …)
Class of transfer functions (coefficients of numerator/denominator
polynomials, stability verification, pole identification, …)
Data of a class member variables
Functions of a class member functions
Class variable an object, an instance
© HDC 2024.1 Chapter 6: Class and Objects 5
Object-oriented programming (OOP)
Abstraction: simplify a problem, easy to reuse
Data encapsulation/ information hiding: improve
reusability and reliability of the software
Subtyping/inheritance: make the code and its design easy
to reuse
Polymorphism: reflect the real world accurately and
enhance the software flexibility
OOP methodology enables highly abstract thinking but close to
the real world!
© HDC 2024.1 Chapter 6: Class and Objects 6
6.2 From Structure to Class
struct Time { void addSec(Time& t, int s) {
int hour; // gio t.sec+= s;
int min; // phut if (t.sec> 59) {
int sec; // giay addMin(t, t.sec/60);
};
t.sec%= 60;
void addHour(Time& t, int h) { }
t.hour+= h; else if (t.sec< 0) {
} addMin(t, t.sec/60 -1);
t.sec= (t.sec% 60) + 60;
void addMin(Time& t, int m) { }
t.min+= m; }
if (t.min> 59) { void main() {
t.hour+= t.min/60; Time t = {1, 0, 0};
t.min%= 60;
addMin(t,60);
}
else if (t.min< 0) { addMin(t,-5);
t.hour+= (t.min/60 -1); addSec(t,25);
t.min= (t.min% 60) + 60; ...
} }
}
© HDC 2024.1 Chapter 6: Class and Objects 7
Issues of Structure
Direct access to the data without strictly control would results in unsafe
operation
Time t1 = {1, 61, -3};// ??!
Time t2;// Uncertain values
int h = t2.hour;// ??!
int m = 50;
t2.min = m + 15;// ??!
There is no difference between “internal details” and “external interface”, a
trivial modification requires users to change the source code
E.g.: the name of one member variable in Time structure has been changed
struct Time {
int h, m, s;
};
The old code cannot be used:
Time t;
t.hour= 5;
© HDC 2024.1 Chapter 6: Class and Objects 8
Encapsulating or “Classifying”
class Time {
int hour; // gio
int min; // phut Member variable
int sec; // giay
public:
Time() {hour=min=sec=0;} Constructor
void setTime(int h, int m, int s) {
hour = h;
min = sec = 0;
addSec(s);
addMin(m); Member functions
}
int getHour() { return hour; }
int getMin() { return min; }
int getSec() { return sec; }
void addHour(int h) {hour += h;}
...
}
© HDC 2024.1 Chapter 6: Class and Objects 9
Encapsulating or “Classifying” cont.
void addMin(int m) { void main() {
min += m; Time t;
if (min > 59) { t.addHour(1);
hour += min/60; t.addMin(60);
min %= 60; t.addMin(-5);
} t.addSec(25);
else if (min < 0) { t.hour= 1; // error
hour += (min/60 -1); t.min= 65; // error
min = (min % 60) + 60; t.sec= -3; // error
} t.setTime(1, 65, -3);
} int h = t.getHour();
int m= t.getMin();
void addSec(int s) { int s= t.getSec();
sec += s; }
if (sec > 59) {
addMin(sec/60);
sec %= 60;
}
else if (sec < 0) {
addMin(sec/60 -1);
sec = (sec % 60) + 60;
}
}
};
© HDC 2024.1 Chapter 6: Class and Objects 10
6.3 Member variables
Variable declaration of a class is similar to that in structure
class Time {
public:
int hour, min, sec;
...
};
By default, member variables of the class can be neither accessed
externally (private variables) nor initialized conventionally:
Time t = {1, 0, 0};// error!
t.hour= 2;// error!
It is possible to make a member variable enable to be accessed
externally (public variables), however it is hardly required:
class Point {
public:
int x,y;
};
© HDC 2024.1 Chapter 6: Class and Objects 11
Member variables
Private variables should be accessed via member functions
The only way to initialize member variable is to use a constructor:
class Time {
...
public:
Time() {hour=min=sec=0;}
};
Time t;// t.hour= t.min= t.sec= 0;
Some member variables are used to store internal states of the
object, they should not be accessed by external entities, even
indirectly via member functions
class PID {
double Kp, Ti, Td;// controller parameters
double I;// internal state
...
};
© HDC 2024.1 Chapter 6: Class and Objects 12
6.4 Member functions
Definition of structure & functions Definition of classes
struct Time { class Time {
int hour, min, sec int hour,min,sec;
}; public:
void addHour(Time& t, int h) void addHour(int h) {
{
hour += h;
t.hour+= h;
}
}
...
...
};
Call of an object member function
Function call with structure
variable Time t;
Time t; ...
... t.addHour(5);
addHour(t,5);
Representation is not the same but it is not the key difference
© HDC 2024.1 Chapter 6: Class and Objects 13
Declaration and definition of member functions
Usually, class and member variables are declared in a header file (*.h).
E.g. in “mytime.h”:
class Time {
int hour,min,sec;
public:
void addHour(int h);
void addMin(int m);
void addSec(int s);
...
};
Functions are often defined in source file (*.cpp):
#include “mytime.h”
...
void Time::addHour(int h) {
hour += h;
}
© HDC 2024.1 Chapter 6: Class and Objects 14
Declaration and definition of member functions
It is possible to define a member function in header file as an inline function
(applying for simple one only), e.g.:
inline void Time::addHour(int h) { hour += h;}
A member function can be defined in class declaration it is an inline function
by default, e.g.:
class Time {
int hour,min,sec;
public:
void addHour(int h) { hour += h; }
};
When defining a member function, it is possible to use member variables and
other member functions without the class name, e.g.:
void Time::addSec(int s) {
...
addMin(sec/60);
...
}
© HDC 2024.1 Chapter 6: Class and Objects 15
Member function
class Time {
int hour,min,sec;
public:Time() { hour=min=sec=0; }
void addHour(int h) {
this->hour += h;// this pointer is the address of the object
} // which calls the member function
...
};
void main() {
Time t1,t2; // automatically call the constructor Time() for t1 and t2
t1.addHour(5); // same as addHour(&t1,5);
t2 = t1; // OK
t2.addHour(5); // same as addHour(&t2,5);
...
}
© HDC 2024.1 Chapter 6: Class and Objects 16
6.5 Access control
public: members are public, can be used externally
private: members are private, cannot be used externally
even in derived classes (will be discussed later)
class Time {
private:
int hour,min,sec;
...
};
By default, once class is declared, members are
private
protected: members are protected, cannot be accessed
externally but can be used by derived classes (will be
discussed later)
© HDC 2024.1 Chapter 6: Class and Objects 17
6.6 Object pointer
#include "mytime.h“
void main() {
Time t;// call constructor Time()
t.addHour(5);
Time *pt = &t;// pt is identical to this pointer
pt->addSec(70);
pt = new Time;// call constructor Time()
pt->addMin(25);
...
delete pt;
pt = new Time[5]; // call constructor 5 times
for (int i=0; i < 5; ++ i)
pt[i].addSec(10);
...
delete [] pt;
}
© HDC 2024.1 Chapter 6: Class and Objects 18
Homework
Based on Vector structure and related functions implemented
in Chapter 4, build Vector class with necessary member
functions
Declare a class in order to store student information, it
should include the following attributes:
Student number (identity): Integer
Full name: String
Year of birth: Integer
Declare and define the class to manage students by member
functions which perform:
Input student full name
Input student number
Input year of birth
Search and display student information with given student number
© HDC 2024.1 Chapter 6: Class and Objects 19
TO BE CONTINUED