classes and data abstraction andrew davison noppadon kamolvilassatian department of computer...

26
Classes and Dat a Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1 www.bookspar.com | Website for students | VTU NOTES

Upload: mae-oliver

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Classes and Data Abstraction

Andrew Davison Noppadon Kamolvilassatian

Department of Computer EngineeringPrince of Songkla University 1

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 2: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Contents• 1. What is Data Abstraction?• 2. C++: Classes and Data Abstraction• 3. Implementing a User-Defined Type Time with a Struct• 4. Implementing a Time Abstract Data Type with a Class• 5. Classes as User-Defined Types• 6. Using Constructors

2

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 3: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

1. What is Data Abstraction?• Abstract Data Types (ADTs)• type implementation & operations• hidden implementation

• types are central to problem solving

• a weapon against complexity

• built-in and user-defined types are ADTs

3

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 4: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Clients and Manufacturers

4

client

Implementation

Interface

manufacturer’sresponsibility

ADT

useclient

client

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 5: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Benefits• Manufacturer Benefits:• easy to modify, maintain• profitable• reusable

• Client Benefits:• simple to use, understand• familiar• cheap• component-based

5

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 6: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

How Well are ADTs Supported in C?• Does C enforce the use of the ADTs interface and the hiding of

its implementation?

• No

6

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 7: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

C++ (Chapters 15-21)

• C++ is a superset of C, which has added features to support object-oriented programming.

• C++ supports classes.• things very like ADTs

7

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 8: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

2. C++: Classes and Data Abstraction• C++ supports Object-Oriented Programming (OOP).

• OOP models real-world objects with software counterparts.

• OOP encapsulates data (attributes) and functions (behavior) into packages called objects.

• Objects have the property of information hiding.

8

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 9: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

• Objects communicate with one another across interfaces.

• The interdependencies between the classes are identified

• makes use of• a part of• a specialisation of• a generalisation of• etc

9

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 10: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

C and C++• C programmers concentrate on writing functions.

• C++ programmers concentrate on creating their own user-defined types called classes.

• Classes in C++ are a natural evolution of the C notion of struct.

10

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 11: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

3. Implementing a User-Defined Type Time with a Struct// FIG16_1.CPP

// Create a structure, set its members, and print it.

#include <iostream.h>

struct Time { // structure definition

int hour; // 0-23

int minute; // 0-59

int second; // 0-59

};

void printMilitary(const Time &); // prototype

void printStandard(const Time &); // prototype11

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 12: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

main(){ Time dinnerTime; // variable of new type Time

// set members to valid values dinnerTime.hour = 18; dinnerTime.minute = 30; dinnerTime.second = 0;

cout << "Dinner will be held at "; printMilitary(dinnerTime); cout << " military time,\nwhich is "; printStandard(dinnerTime); cout << " standard time." << endl;

12

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 13: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

// set members to invalid values

dinnerTime.hour = 29;

dinnerTime.minute = 73;

dinnerTime.second = 103;

cout << "\nTime with invalid values: ";

printMilitary(dinnerTime);

cout << endl;

return 0;

}

13

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 14: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

// Print the time in military format

void printMilitary(const Time &t)

{

cout << (t.hour < 10 ? "0" : "") << t.hour << ":"

<< (t.minute < 10 ? "0" : "") << t.minute << ":"

<< (t.second < 10 ? "0" : "") << t.second;

}

14

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 15: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

// Print the time in standard format

void printStandard(const Time &t)

{

cout << ((t.hour == 0 || t.hour == 12) ? 12 : t.hour % 12)

<< ":" << (t.minute < 10 ? "0" : "") << t.minute

<< ":" << (t.second < 10 ? "0" : "") << t.second

<< (t.hour < 12 ? " AM" : " PM");

}

15

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 16: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Comments• Initialization is not required --> can cause problems.

• A program can assign bad values to members of Time.

• If the implementation of the struct is changed, all the programs that use the struct must be changed. [No “interface”]

16

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 17: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

4. Implementing a Time Abstract Data Type with a Class#include <iostream.h>

// Time abstract data type (ADT) definition

class Time {

public:

Time(); // default constructor

void setTime(int, int, int);

void printMilitary();

void printStandard();

private:

int hour; // 0 - 23

int minute; // 0 - 59

int second; // 0 - 59

};

17

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 18: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

// Time constructor initializes each data member to zero.// Ensures all Time objects start in a consistent state.Time::Time() { hour = minute = second = 0; }

// Set a new Time value using military time.// Perform validity checks on the data values.// Set invalid values to zero (consistent state)void Time::setTime(int h, int m, int s){ hour = (h >= 0 && h < 24) ? h : 0; minute = (m >= 0 && m < 60) ? m : 0; second = (s >= 0 && s < 60) ? s : 0;}

18

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 19: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

// Print Time in military formatvoid Time::printMilitary(){ cout << (hour < 10 ? "0" : "") << hour << ":"<< (minute < 10 ? "0" : "") << minute << ":"<< (second < 10 ? "0" : "") << second;

}

// Print time in standard formatvoid Time::printStandard(){ cout << ((hour == 0 || hour == 12) ? 12 : hour % 12)<< ":" << (minute < 10 ? "0" : "") << minute<< ":" << (second < 10 ? "0" : "") << second<< (hour < 12 ? " AM" : " PM");

} 19

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 20: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

// Driver to test simple class Timemain(){ Time t; // instantiate object t of class Time

cout << "The initial military time is "; t.printMilitary(); cout << "\nThe initial standard time is "; t.printStandard();

t.setTime(13, 27, 6); cout << "\n\nMilitary time after setTime is "; t.printMilitary(); cout << "\nStandard time after setTime is "; t.printStandard();

20

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 21: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

t.setTime(99, 99, 99);

// attempt invalid settings

cout << "\n\nAfter attempting invalid settings:\n"

<< "Military time: ";

t.printMilitary();

cout << "\nStandard time: ";

t.printStandard();

cout << endl;

return 0;

}

21

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 22: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Output• The initial military time is 00:00:00• The initial standard time is 12:00:00 AM

• Military time after setTime is 13:27:06• Standard time after setTime is 1:27:06 PM

• After attempting invalid settings:• Military time: 00:00:00• Standard time: 12:00:00 AM

22

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 23: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Comments

• hour, minute, and second are private data members. They are normally not accessible outside the class. [Information Hiding]

• Use a constructor to initiailize the data members. This ensures that the object is in a consistate state when created.

• Outside functions set the values of data members by calling the setTime method, which provides error checking.

23

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 24: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

5. Classes as User-Defined Types• Once the class has been defined, it can be used as a type in de

clarations as follows:

Time sunset, //object of type Time

arrayOfTimes[5], //array of Time objects

*pointerToTime, //pointer to a Time object

24

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 25: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

6. Using Constructors

• Constructors can be overloaded, providing several methods to initialize a class.

• Time example:InterfaceTime(); Time(int hr, int min, int sec);

ImplementationTime::Time() { hour = minute = second = 0; }Time::Time(int hr, int min, int sec) { setTime(hr, min, sec); }

25

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES

Page 26: Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1

Use:

Time t1;

Time t2(08,15,04);

26

ww

w.b

ooksp

ar.

com

|

Websi

te f

or

students

| V

TU

N

OTES