0% found this document useful (0 votes)
18 views20 pages

Chapter6 Class and Objects

Kỹ thuật lập trình BKHN

Uploaded by

Minh Huệ Tô
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views20 pages

Chapter6 Class and Objects

Kỹ thuật lập trình BKHN

Uploaded by

Minh Huệ Tô
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

EE3491/EE3490E –

Kỹ thuật lập trình /


Programming Techniques
Chapter 6: Class and
Objects – Part 1
Lecturer: Dr. Hoang Duc Chinh (Hoàng Đức Chính)
Department of Industrial Automation
School of Electrical Engineering
Email: [email protected] © HĐC 2022.1
Content
6.1. Definitions
6.2. From structure to classs
6.3. Member variables
6.4. Member functions
6.5. Access control

Chapter 6: Class and Objects © DIA 2022.1 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

Chapter 6: Class and Objects © DIA 2022.1 3


An object should have…
 Attributes
 State
 Data
 Relationship
 Behaviour
 Operation
 Response
 Identity
 Semantic/responsibility

Chapter 6: Class and Objects © DIA 2022.1 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

Chapter 6: Class and Objects © DIA 2022.1 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!
Chapter 6: Class and Objects © DIA 2022.1 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) {
intsec; // giay addMin(t, t.sec/60);
}; t.sec%= 60;
}
void addHour(Time& t, int h) { else if (t.sec< 0) {
t.hour+= h; addMin(t, t.sec/60 -1);
} t.sec= (t.sec% 60) + 60;
}
void addMin(Time& t, int m) { }
t.min+= m; void main() {
if (t.min> 59) { Time t = {1, 0, 0};
t.hour+= t.min/60; addMin(t,60);
t.min%= 60; addMin(t,-5);
} addSec(t,25);
else if (t.min< 0) { ...
t.hour+= (t.min/60 -1); }
t.min= (t.min% 60) + 60;
}
}
Chapter 6: Class and Objects © DIA 2022.1 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;
Chapter 6: Class and Objects © DIA 2022.1 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;}
...

Chapter 6: Class and Objects © DIA 2022.1 9


Encapsulating or “Classifying” cont.
void addMin(int m) { void main() {
min += m; Time t;
if (min > 59) {
t.addHour(1);
hour += min/60;
min %= 60; t.addMin(60);
} t.addMin(-5);
else if (min < 0) { t.addSec(25);
hour += (min/60 -1); t.hour= 1; // error
min = (min % 60) + 60;
t.min= 65; // error
}
} t.sec= -3; // error
t.setTime(1, 65, -3);
void addSec(int s) { int h = t.getHour();
sec += s; int m= t.getMin();
if (sec > 59) {
int s= t.getSec();
addMin(sec/60);
sec %= 60; }
}
else if (sec < 0) {
addMin(sec/60 -1);
sec = (sec % 60) + 60;
}
}
};

Chapter 6: Class and Objects © DIA 2022.1 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;
};

Chapter 6: Class and Objects © DIA 2022.1 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
...
};

Chapter 6: Class and Objects © DIA 2022.1 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) {
t.hour+= h; hour += h;
} }
... ...
};
 Function call with structure variable  Call of an object member function
Time t; Time t;
... ...
addHour(t,5); t.addHour(5);

Representation is not the same but it is not the key difference


Chapter 6: Class and Objects © DIA 2022.1 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(inth);
void addMin(intm);
void addSec(ints);
...
};
 Functions are often defined in source file (*.cpp):
#include “mytime.h”
...
void Time::addHour(inth) {
hour += h;
}

Chapter 6: Class and Objects © DIA 2022.1 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(inth) { 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);
...
}

Chapter 6: Class and Objects © DIA 2022.1 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);
...
}

Chapter 6: Class and Objects © DIA 2022.1 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)

Chapter 6: Class and Objects © DIA 2022.1 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 (inti=0; i < 5; ++ i)
pt[i].addSec(10);
...
delete [] pt;
}

Chapter 6: Class and Objects © DIA 2022.1 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
Chapter 6: Class and Objects © DIA 2022.1 19
TO BE CONTINUED

Chapter 6: Class and Objects © DIA 2022.1 20

You might also like