lecture1 classes3

66
+ Lecture 1 Introduction to C++ Classes (Part 3) TTTK1924 Program Design and Problem Solving

Upload: noor-faezah-mohd-yatim

Post on 02-Jul-2015

121 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture1 classes3

+

Lecture 1 – Introduction to

C++ Classes (Part 3)

TTTK1924 – Program Design and Problem

Solving

Page 2: Lecture1 classes3

+Part 3

Classes

Page 3: Lecture1 classes3

+Using Classes

1. Define a class according to the following syntax:

class <classIdentifier> {

private:

<declaration of member variables>

public:

<definition of member functions>

};

2. Declare a variable of the class – object variable

<classIdentifier> <object-variable>;

3. Access or modify the data by calling member functions with the dot (.) operator

<object-variable>.<member-function>;

or

<variable> = <object-variable>.<member-function>;

Page 4: Lecture1 classes3

+Class Definition

Includes

declaration of member variables(may contain different types

of data)

definition of member functions – used to manipulate data

items

Member variables are declared private to the class

– can only be accessed or modified by its own class

Member functions are declared public ( can be used

by other than its own class)

Page 5: Lecture1 classes3

+Class Definition - Example

class timeType {private:

int hour;int minute;int second;string zone;

public:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}

int getHour() {return hour;

}

int getMinute() {return minute;

}

int getSecond() {return second;

}:

:string getZone() {

return zone;}

void printTime() const {if (hour < 10)

cout << "0";cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}

timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}};

Name of class

(class identifier)

Page 6: Lecture1 classes3

+Class Definition - Example

class timeType {private:

int hour;int minute;int second;string zone;

public:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}

int getHour() {return hour;

}

int getMinute() {return minute;

}

int getSecond() {return second;

}:

:string getZone() {

return zone;}

void printTime() const {if (hour < 10)

cout << "0";cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}

timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}};

Declaration of member variables

Page 7: Lecture1 classes3

+Class Definition - Example

class timeType {private:

int hour;int minute;int second;string zone;

public:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}

int getHour() {return hour;

}

int getMinute() {return minute;

}

int getSecond() {return second;

}:

:string getZone() {

return zone;}

void printTime() const {if (hour < 10)

cout << "0";cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}

timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}};

Definition of

member functions

Page 8: Lecture1 classes3

+Using Classes

Defined classes can be used by

The main program, or

Other classes

Only public member functions can be called or used by the

main program or other classes

Page 9: Lecture1 classes3

+Using Classes - Example

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

Page 10: Lecture1 classes3

+Using Classes - Example

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

Declaration of

object variable

Page 11: Lecture1 classes3

+Using Classes - Example

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute()<< endl;

return 0;}

Accessing member variables using

member functions

Page 12: Lecture1 classes3

+Using Classes - Example

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

Modifying member variables

using member functions

Page 13: Lecture1 classes3

+Using Classes – Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

:timeType(){

hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

};

Page 14: Lecture1 classes3

+Using Classes – Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

:void printTime() const {

if (hour < 10)cout << "0";

cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}

:};

Page 15: Lecture1 classes3

+Using Classes - Example 7

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

:void printTime() const {

if (hour < 10)cout << "0";

cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}:

};

00:

Page 16: Lecture1 classes3

+Using Classes - Example 7

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

:void printTime() const {

if (hour < 10)cout << "0";

cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}:

};

00:00:

Page 17: Lecture1 classes3

+Using Classes - Example 7

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

:void printTime() const {

if (hour < 10)cout << "0";

cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}:

};

00:00:00:

Page 18: Lecture1 classes3

+Using Classes - Example 7

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

:void printTime() const {

if (hour < 10)cout << "0";

cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}:

};

00:00:00:Kuala Lumpur

Page 19: Lecture1 classes3

+Using Classes - Example 7

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

00:00:00:Kuala Lumpur

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}:};

Page 20: Lecture1 classes3

+Using Classes - Example 7

first

0

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

00:00:00:Kuala Lumpur

:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}:};

h

5

m

13

s

0

z

Singapore

Page 21: Lecture1 classes3

+Using Classes - Example 7

first

5

0

0

currentTime

hour

minute

second

Kuala Lumpurzone

00:00:00:Kuala Lumpur

:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}:};

h

5

m

13

s

0

z

Singapore

Page 22: Lecture1 classes3

+Using Classes - Example 7

first

5

13

0

currentTime

hour

minute

second

Kuala Lumpurzone

00:00:00:Kuala Lumpur

:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}:};

h

5

m

13

s

0

z

Singapore

Page 23: Lecture1 classes3

+Using Classes - Example 7

first

5

13

0

currentTime

hour

minute

second

Kuala Lumpurzone

00:00:00:Kuala Lumpur

:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}:};

h

5

m

13

s

0

z

Singapore

Page 24: Lecture1 classes3

+Using Classes - Example 7

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

:void setTime(int h, int m, int s, string z) {

hour = h;minute = m;second = s;zone = z;

}:};

h

5

m

13

s

0

z

Singapore

Page 25: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

:void printTime() const {

if (hour < 10)cout << "0";

cout << hour << ":";if (minute < 10)

cout << "0";cout << minute << ":";if (second < 10)

cout << "0";cout << second << ":";cout << zone << endl;

}

:};

first

5

13

0

currentTime

hour

minute

second

Singaporezone

Page 26: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

Page 27: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

The time in

Page 28: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

:string getZone() const {

return zone;}:

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

The time in Singapore

Page 29: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

The time in Singapore now is

Page 30: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

:int getHour() const {

return hour;}:

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

The time in Singapore now is 5

Page 31: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

The time in Singapore now is 5:

Page 32: Lecture1 classes3

+Using Classes - Example 7

int main (int argc, const char * argv[]){

timeType currentTime;currentTime.printTime();currentTime.setTime(5, 13, 0, "Singapore");currentTime.printTime();cout << "The time in " << currentTime.getZone() << " now is "

<< currentTime.getHour() << ":" << currentTime.getMinute() << endl;

return 0;}

first

5

13

0

currentTime

hour

minute

second

Singaporezone

00:00:00:Kuala Lumpur

05:13:00:Singapore

The time in Singapore now is 5:13

:int getMinute() const {

return minute;}

:

Page 33: Lecture1 classes3

+Class Definition Variations

A class in C++ is more commonly defined using the following syntax:

class <classIdentifier> {

public:

<member functions prototype>

private:

<declaration of member variables>

};

:

<member functions implementation>

In this case, we need to use the class name and scope resolution operator (::) in member functions implementation

Page 34: Lecture1 classes3

+Class Definition Variations

Eg.

class timeType {public:

void setTime(int h, int m, int s, string z);int getHour();int getMinute();int getSecond();string getZone();void printTime() const;timeType();

private:int hour;int minute;int second;string zone;

};:

:

void timeType::setTime(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

int timeType::getHour() {return hour;

}

int timeType::getMinute() {return minute;

}:timeType:: timeType(){

hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

Member functions

declaration

Page 35: Lecture1 classes3

+Class Definition Variations

Eg.

class timeType {public:

void setTime(int h, int m, int s, string z);int getHour();int getMinute();int getSecond();string getZone();void printTime() const;timeType();

private:int hour;int minute;int second;string zone;

};:

:

void timeType::setTime(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

int timeType::getHour() {return hour;

}

int timeType::getMinute() {return minute;

}:timeType:: timeType(){

hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}Definition ends here

Member functions

implementation

Page 36: Lecture1 classes3

+Class Definition Variations

Eg.

:void timeType::setTime(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}: Scope resolution operator

Name of the class

Page 37: Lecture1 classes3

+Class Definition Variations

The order of declarations can also be changed, like in the

following syntax:

class <classIdentifier> {

private:

<declaration of member variables>

public:

<member functions prototype>

};

:

<member functions implementation>

Page 38: Lecture1 classes3

+Class Definition Variations

Eg.

class timeType {private:

int hour;int minute;int second;string zone;

public:void setTime(int h, int m, int s, string z);int getHour();int getMinute();int getSecond();string getZone();void printTime() const;timeType();

};:

:

void timeType::setTime(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

int timeType::getHour() {return hour;

}

int timeType::getMinute() {return minute;

}:timeType:: timeType(){

hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

private member variables

are declared first

Page 39: Lecture1 classes3

+Class Definition Variations

The default specifier for a class member is private, therefore, if

private members are placed first, the keyword private can be

omitted.

The following syntax is the same as the previous one:

class <classIdentifier> {

<declaration of member variables>

public:

<member functions prototype>

};

:

<member functions implementation>

Page 40: Lecture1 classes3

+Class Definition Variations

Eg.

class timeType {int hour;int minute;int second;string zone;

public:void setTime(int h, int m, int s, string z);int getHour();int getMinute();int getSecond();string getZone();void printTime() const;timeType();

};:

:

void timeType::setTime(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

int timeType::getHour() {return hour;

}

int timeType::getMinute() {return minute;

}:timeType:: timeType(){

hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

private keyword is omitted

Page 41: Lecture1 classes3

+

Member variables and member functions can also be declared

as protected

For further explanation, please refer the class inheritance topic

Class Definition Variations

Page 42: Lecture1 classes3

+Accessor, Mutator and Constant Functions

Accessor functions – member functions that are used to (and only) access (do not modify) the value/s of member variables

Eg.

getHour(), getMinute(), getSecond(), getZone(), printTime()

Mutator functions – member functions that are used to modify the value/s of member variables

Eg.

setTime()

Constant functions – accessor functions that have the keyword const in its heading – as a safeguard not to modify member variables

Eg.

void printTime() const

A class definition must have accessor and mutator functions to let other classes use them

Page 43: Lecture1 classes3

+Accessor, Mutator and Constant Functions

class timeType {private::

public:void setTime(int h, int m, int s, string z);int getHour();int getMinute();int getSecond();string getZone();void printTime() const;timeType();

};:

Mutator function

Accessor functions

Constant function

Page 44: Lecture1 classes3

+Reference Parameters Let’s say we want to define a member function that calculates the time

difference in hours between two cities.

We can do this by passing an object variable as a parameter of a

function as follows:

Therefore, we can call the function like this:

int timeType::timeDifference(timeType otherTime){if (hour > otherTime.hour)

return (hour - otherTime.hour);else if (hour < otherTime.hour)

return (otherTime.hour - hour);else

return 0;}

cout << "The time difference between " << time1.getZone() << " and " << time2.getZone() << " is " << time1.timeDifference(time2) << " hours " << endl;

Page 45: Lecture1 classes3

+Reference Parameters

However, this is not a good programming practice, as we it

requires memory space to copy the value of the actual

parameters

Assuming we have the following statements :

We will have memory space allocated as follows:

timeType time1, time2;time2.setTime(5, 13, 0, "Paris");

first

0

0

0

time1

hour

minute

second

Kuala Lumpurzone

first

5

13

0

time2

hour

minute

second

Pariszone

Page 46: Lecture1 classes3

+Reference Parameters

When we call the timeDifference() function, the values of member variables of time2 are copied into otherTime

So, additional memory space is allocated for otherTime :

This can be improved by using reference parameters.

first

5

13

0

otherTime

hour

minute

second

Pariszone

Page 47: Lecture1 classes3

+Reference Parameters

The following member function uses a reference parameter:

Similarly, we can call the function:

int timeType::timeDifference (const timeType& otherTime){if (hour > otherTime.hour)

return (hour - otherTime.hour);else if (hour < otherTime.hour)

return (otherTime.hour - hour);else

return 0;}

cout << "The time difference between " << time1.getZone() << " and " << time2.getZone() << " is " << time1.timeDifference(time2) << " hours " << endl;

Page 48: Lecture1 classes3

+Reference Parameters

otherTime refers to the memory allocated for time2:

No additional memory needed

Can the values of the member variables be modified during the function call?

No, because of the keyword const in the function header

first

5

13

0

time2

hour

minute

second

Pariszone

otherTime

Page 49: Lecture1 classes3

+Constructors

A constructor is a special member function that is

called during the declaration of an object variable

A constructor is used to initialise the member

variables of a class

Notice that it has neither a return type nor void

A class can have more that one constructors, and

which constructor is called depends on the number

and type of the actual parameters (if any)when it is

called

A constructor without any parameters is called the

default constructor

Page 50: Lecture1 classes3

+Constructors Let’s say we have the following constructors in a class:

And we have the following statements in the main program:

timeType::timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

timeType::timeType(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

:timeType time1;timeType time2(5, 13, 0, "Paris");time1.printTime();time2.printTime();:

Default constructor

Page 51: Lecture1 classes3

+Constructors

timeType::timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

timeType::timeType(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

:timeType time1;timeType time2(5, 13, 0, "Paris");time1.printTime();time2.printTime();:

first

0

0

0

time1

hour

minute

second

Kuala Lumpurzone

Page 52: Lecture1 classes3

+Constructors

timeType::timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

timeType::timeType(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

:timeType time1;timeType time2(5, 13, 0, "Paris");time1.printTime();time2.printTime();:

first

0

0

0

time1

hour

minute

second

Kuala Lumpurzone

first

5

13

0

time2

hour

minute

second

Pariszone

Page 53: Lecture1 classes3

+Constructor With Default Values

We can also simplify the previous definition using a constructor with

default values:

class timeType2 {public:

void setTime(int h, int m, int s, string z);int getHour();int getMinute();int getSecond();string getZone();void getTime(int& h, int& m, int& s, string z) const;void printTime() const;int timeDifference(const timeType2& otherTime);timeType2(int = 0, int = 0, int = 0, string = "Kuala Lumpur");

private:int hour;int minute;int second;string zone;

};

Default values

Page 54: Lecture1 classes3

+Constructor With Default Values

And use only one constructor implementation:

Assuming we have the following statements in the main

program:

timeType::timeType(int h, int m, int s, string z) {hour = h;minute = m;second = s;zone = z;

}

:timeType time1;timeType time2(5, 13, 0, "Paris");:

Page 55: Lecture1 classes3

+Constructor With Default Values

We get the same result as before:

:timeType time1;timeType time2(5, 13, 0, "Paris");:

first

0

0

0

time1

hour

minute

second

Kuala Lumpurzone

Page 56: Lecture1 classes3

+Constructor With Default Values

We get the same result as before:

:timeType time1;timeType time2(5, 13, 0, "Paris");:

first

0

0

0

time1

hour

minute

second

Kuala Lumpurzone

first

5

13

0

time2

hour

minute

second

Pariszone

Page 57: Lecture1 classes3

+Destructors

A class can only have one destructor

Just like a constructor, it has no type nor return

value, and marked with tilda (~)

Called automatically when the program has finished

using an object

If no destructor is declared, the compiler will

implicitly insert one

Page 58: Lecture1 classes3

+Destructors - Example

class timeType {public:

void setTime(int h, int m, ints, string z);

int getHour();int getMinute();int getSecond();string getZone();void printTime() const;timeType();~timeType();

private:int hour;int minute;int second;string zone;

};:

:

timeType:: timeType(){hour = 0;minute = 0;second = 0;zone = "Kuala Lumpur";

}

timeType::~timeType() {}

Destructor declaration

Destructor

implementation

Page 59: Lecture1 classes3

+Array of Class Objects (Variables)

Just like other types of data, we can declare an array of object

variables

Eg.

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

Page 60: Lecture1 classes3

+Array of Class Objects (Variables)

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

first

0

0

0

hour

minute

second

Kuala Lumpurzone

time time[0]

first

0

0

0

hour

minute

second

Kuala Lumpurzone

time[1]

first

0

0

0

time[2]

hour

minute

second

Kuala Lumpurzonefirst

0

0

0

time[3]

hour

minute

second

Kuala Lumpurzonefirst

0

0

0

time[4]

hour

minute

second

Kuala Lumpurzone

Page 61: Lecture1 classes3

+Array of Class Objects (Variables)

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

time

first

1

0

0

time[1]

hour

minute

second

Tokyozone

Page 62: Lecture1 classes3

+Array of Class Objects (Variables)

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

time

first

5

0

0

time[2]

hour

minute

second

Pariszone

Page 63: Lecture1 classes3

+Array of Class Objects (Variables)

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

time

first

11

0

0

time[3]

hour

minute

second

New Yorkzone

Page 64: Lecture1 classes3

+Array of Class Objects (Variables)

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

time

first

3

0

0

time[4]

hour

minute

second

Sydneyzone

Page 65: Lecture1 classes3

+Array of Class Objects (Variables)

timeType2 time[5];time[1].setTime(1, 0, 0, "Tokyo");time[2].setTime(5, 0, 0, "Paris");time[3].setTime(11, 0, 0, "New York");time[4].setTime(3, 0, 0, "Sydney");for (int i=0; i<5; i++)

time[i].printTime();

00:00:00:Kuala Lumpur

01:00:00:Tokyo

05:00:00:Paris

11:00:00:New York

03:00:00:Sydney

Page 66: Lecture1 classes3

+References:

D.S. Malik (2012). C++ Programming: Program Design

Including Data Structures (5th ed), Thomson Course

Technology.

Chapter 12 – Classes and Data Abstraction