You are on page 1of 22

Classes and Abstraction

by

Loyd Tinarwo

1
Objectives

In this lecture, you will:

• Learn about classes


• Learn about private, protected, and public members of a class
• Explore how classes are implemented
• Learn about setters and getters
• Examine constructors & destructors
• Learn about the abstract data type (ADT)
• Explore how classes are used to implement ADTs

2
How to Represent an Object ?

Class

class Clock {
Members
public:
void setTime(int, int, int);
Member Functions (Operations)
void printTime() const;

private: Member Variables (Data)


int hr;
int min; Member Access Specifiers
int sec;
}; User Defined Data Type

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

void Clock::setTime (int h, int m, int s) {


class Clock { if (0 <= h && h < 24)
public: hr = h;
void setTime(int, int, int); else
void printTime() const; hr = 0;
if (0 <= m && m < 60)
private: min = m;
int hr; else
int min; min = 0;
int sec; if (0 <= s && s < 60)
}; sec = s;
else
sec = 0;
}

5
Let's Use setTime

void Clock::setTime (int h, int m, int s) {


if (0 <= h && h < 24) int main() {
hr = h; clock myClock;
else
hr = 0; myClock.setTime(5, 4, 30);
if (0 <= m && m < 60)
min = m; return 0;
else }
min = 0; myClock
if (0 <= s && s < 60)
sec = s; hr
else
min
sec = 0;
} sec

6
Displaying

class Clock { void Clock::printTime() const { myClock


public: cout << hr << ":" << min << ":"<< sec;
hr 5
void setTime(int, int, int); }
void printTime() const; min 4

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

void Clock::setTime (int h, int m, int s) {


if (0 <= h && h < 24) int main() {
hr = h; clock myClock;
else int h, m, s;
hr = 0;
if (0 <= m && m < 60) cout << "Please enter time" ;
min = m; cin >> h >> m >> s;
else
min = 0; myClock.setTime (h, m, s); myClock
if (0 <= s && s < 60) myClock.printTime() ;
sec = s; return 0; hr
else } min
sec = 0;
} sec

8
Display Again

int main() { myClock


clock myClock;
int h, m, s; hr 5

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

class Clock { int main() {


public: clock myClock;
void setTime(int, int, int); myClock.setTime (5, 23, 59);
void printTime() const;
void getTime(int& h,int& m,int& s) const; myClock.getTime (h, m, s);
private: cout << h << ":" << m << ":"<< s;
int hr, min, sec; return 0;
}; }
myClock
void Clock::getTime(int& h,int& m,int& s) const { hr 5
h = hr;
m = min; min 23
output:
s = sec;
5:23:59 sec 59
}

10
Setters & Getters

class Clock { int main() {


public: clock myClock;
void setTime(int, int, int); myClock.setTime (5, 23, 59);
void printTime() const;
void getTime(int& h,int& m,int& s) const; myClock.getTime (h, m, s);
private: cout << h << ":" << m << ":"<< s;
int hr, min, sec; return 0;
}; }
myClock
void Clock::getTime(int& h,int& m,int& s) const { hr 5
h = hr;
m = min; min 23
output:
s = sec;
5:23:59 sec 59
}

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

 guarantee member variables  constructors w/params


• are initialized • & w/out params (default constructor)
class Clock {
public:
 properties:
void setTime(int, int, int); • same name as class, has no type
void printTime() const;
clockType(int, int, int); • class can have more than one constructors
clockType(); • but, must have diff formal params lists
private: • diff number or diff data type
int hr;
int min; • constructors execute automatically
int sec; • depending on type of values passed
};

13
W/ & W/Out Params

Clock::Clock (int h, int m, int s) {


if (0 <= h && h < 24)
class Clock { hr = h;
public: else
void setTime(int, int, int); hr = 0;
void printTime() const; if (0 <= m && m < 60)
Clock(int, int, int); clockType::clockType()
min = m; {
Clock(); else hr = 0;
min = 0; min = 0;
private: if (0 <= s && s < 60)
int hr; sec = 0;
sec = s; }
int min; else
int sec; sec = 0;
}; }

14
Invoking the Constructor

Clock::Clock (int h, int m, int s) {


int main() {
if (0 <= h && h < 24)
clock myClock;
hr = h;
clock yourClock(5, 12, 44);
else
hr = 0;
clockType::clockType() return 0;
if (0 <= m && m < 60)
{ }
min = m;
hr = 0;
else
min = 0; myClock yourClock
min = 0;
sec = 0;
if (0 <= s && s < 60)
} hr hr
sec = s;
else min min
sec = 0;
} sec sec

15
Constructor W/Params & setTime

Clock::Clock (int h, int m, int s) { void Clock::setTime (int h, int m, int s) {


if (0 <= h && h < 24) if (0 <= h && h < 24)
hr = h; hr = h;
else else
hr = 0; hr = 0;
if (0 <= m && m < 60) if (0 <= m && m < 60)
min = m; min = m;
else else
min = 0; min = 0;
if (0 <= s && s < 60) if (0 <= s && s < 60)
sec = s; sec = s;
else else
sec = 0; sec = 0;
} }

16
Call setTime

Clock::Clock (int h, int m, int s) { void Clock::setTime (int h, int m, int s) {


if (0 <= h && h < 24) if (0 <= h && h < 24)
hr = h; hr = h;
else else
hr = 0; hr = 0;
if (0 <= m && m < 60) if (0 <= m && m < 60)
min = m; Clock::Clock (int h,int m,int s){ min = m;
else setTime (h, m, s) else
min = 0; } min = 0;
if (0 <= s && s < 60) if (0 <= s && s < 60)
sec = s; sec = s;
else else
sec = 0; sec = 0;
} }

17
Constructor W/ Default Params

class Clock { default value


public: preestablished/preset value i.e. 0
void setTime(int, int, int); used when a value is not specified
void printTime() const;
Clock(int, int, int); replace both by:
Clock(); Clock (int = 0, int = 0, int = 0)

private: def/code of this constructor


int hr; is same as that of constructor w/params
int min; ex.
int sec; clock myClock1; 0: 0: 0
}; clock myClock2(5); 5: 0: 0
clock myClock3(2, 30); 2: 20: 0
clock myClock4(7, 34, 18); 7: 40: 18

18
Destructors

class Clock { are functions


public: • w/out a type or parameters
void setTime(int, int, int); • w/ same name as class name
void printTime() const; • preceded by a tilde
Clock(int, int, int);
Clock(); destructs, destroys or deletes an object
~clockType();
executes automatically
private: when object goes out of scope:
int hr; • function ends
int min; • program ends.
int sec; • scope ({ block}) ends
}; • delete operator is called

19
Data Abstraction & Classes

we are interested in using items(use),


abstraction can be applied to data.
not how items work (design).

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)

separates logical properties from operations


implementation details. set , return , print time etc.

ADT has: to implement an ADT,


• name (type name) • represent data &
• set of values (domain) • write algorithms to perform the operations
• set of operations on the data
ex.
Clock ADT can be defined as: • def of class Clock had only specs of the ops
type name • functions were done separately
Clock
domain classes are a way to implement ADTs.
each Clock value is a time of day in
form of hours, minutes & seconds.

21
What Did You Learn ?

In this lecture, you:

• Learned about classes


• Learned about members access specifiers
• Explored how classes are implemented
• Learned about setters & getters
• Examined constructors & destructors
• Learned about the abstract data type (ADT)
• Explored how classes are used to implement ADTs

the end

22

You might also like