lecture1 classes3

Post on 02-Jul-2015

121 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

+

Lecture 1 – Introduction to

C++ Classes (Part 3)

TTTK1924 – Program Design and Problem

Solving

+Part 3

Classes

+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>;

+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)

+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)

+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

+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

+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

+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;}

+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

+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

+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

+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";

}

};

+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;

}

:};

+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:

+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:

+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:

+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

+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;

}:};

+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

+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

+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

+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

+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

+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

+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

+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

+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

+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

+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

+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:

+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;}

:

+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

+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

+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

+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

+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>

+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

+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>

+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

+

Member variables and member functions can also be declared

as protected

For further explanation, please refer the class inheritance topic

Class Definition Variations

+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

+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

+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;

+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

+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

+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;

+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

+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

+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

+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

+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

+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

+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");:

+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

+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

+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

+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

+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();

+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

+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

+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

+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

+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

+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

+References:

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

Including Data Structures (5th ed), Thomson Course

Technology.

Chapter 12 – Classes and Data Abstraction

top related