Professional Documents
Culture Documents
by
Loyd Tinarwo
1
Objectives
2
How to Represent an Object ?
Class
class Clock {
Members
public:
void setTime(int, int, int);
Member Functions (Operations)
void printTime() const;
3
Instance/Object
int main() {
class clock { clock myClock; //object
public:
void setTime(int, int, int); return 0;
void printTime() const; }
private:
int hr; myClock
int min; hr
int sec;
}; min
sec
4
setTime
5
Let's Use setTime
6
Displaying
sec 30
private:
int hr; int main() {
int min; clock myClock;
int sec;
}; myClock.setTime(5, 4, 30);
myClock.printTime() ;
return 0; output:
} 5:4:30
7
From the Keyboard
8
Display Again
min 23
cout << "Please enter time" ;
void Clock::printTime() const { cin >> h >> m >> s; sec 59
cout << hr << ":" << min << ":"<< sec;
} myClock.setTime (h, m, s);
myClock.printTime() ;
return 0;
} output:
5:23:59
9
getTime
10
Setters & Getters
11
Forgot setTime
myClock
int main() {
clock myClock; hr
int h, m, s;
min
void Clock::printTime() const { cout << "Please enter time" ; sec
cout << hr << ":" << min << ":"<< sec; cin >> h >> m >> s;
}
myClock.printTime() ;
return 0;
} output:
garbage: garbage: garbage
12
Constructors
13
W/ & W/Out Params
14
Invoking the Constructor
15
Constructor W/Params & setTime
16
Call setTime
17
Constructor W/ Default Params
18
Destructors
19
Data Abstraction & Classes
data abstraction
ex.
• process of separating logical properties
• we know how drive car (use)
• of data from its implementation.
• but not how car engine works (design)
ex.
design is separated from use
• def of class Clock & its operations are
• logical properties
that’s abstraction
• focuses on what an item does(use)
• storing of Clock objects & performing
• not how an item works (design).
operations is the implementation of Clock.
20
Abstract Data Types (ADTs)
21
What Did You Learn ?
the end
22