csci-383 object-oriented programming & design lecture 5

57
CSCI-383 Object-Oriented Programming & Design Lecture 5

Upload: cassandra-oliver

Post on 03-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSCI-383 Object-Oriented Programming & Design Lecture 5

CSCI-383

Object-Oriented Programming & Design

Lecture 5

Page 2: CSCI-383 Object-Oriented Programming & Design Lecture 5

2

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Structs

• A struct holds data, like an array• Each unit of data in a struct is called a data

member (or member)– they are called “elements” in arrays

• In a struct, each data member can have a different data type– in arrays, the data type of each element is the same

Page 3: CSCI-383 Object-Oriented Programming & Design Lecture 5

3

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Example Using a Struct

1 #include <iostream>2 #include <iomanip>3 #include <string>45 using namespace std;67 struct CarType {8 string maker;9 int year;10 float price;11 };1213 void getYourCar( CarType & car );14

Don’t forget this semicolon.

Page 4: CSCI-383 Object-Oriented Programming & Design Lecture 5

4

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

15 int main( )16 {17 CarType myCar, yourCar;1819 myCar.maker = "Mercedes"; // I wish20 myCar.year = 2005;21 myCar.price = 45567.75;22

Example Using a Struct (cont.)

Page 5: CSCI-383 Object-Oriented Programming & Design Lecture 5

5

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

23 getYourCar( yourCar );2425 cout << "Your car is a: " <<

yourCar.maker << endl;26 cout << fixed << showpoint <<

setprecision( 2 ) << 27 "I'll offer $" << yourCar.price - 100 << 28 " for your car." << endl;2930 return 0;31 }32

Example Using a Struct (cont.)

Page 6: CSCI-383 Object-Oriented Programming & Design Lecture 5

6

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

33 void getYourCar( CarType & car )34 {35 cout << "Enter your maker: ";36 cin >> car.maker;37 cout << "Enter the year: ";38 cin >> car.year;39 cout << "Enter the price: $";40 cin >> car.price;41 }

Example Using a Struct (cont.)

Page 7: CSCI-383 Object-Oriented Programming & Design Lecture 5

7

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Object Assignment

• An object of a struct can be assigned to another object of the same struct type:

myCar = yourCar;

• This assigns each data member in yourCar to the corresponding data member of myCar

• Also assigns any array data members

Page 8: CSCI-383 Object-Oriented Programming & Design Lecture 5

8

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Classes

• A class is similar to a struct

• A class contains data members, but it also contains function members

• Objects are made from classes, similarly to the way that objects are made from structs

• The main program communicates with the objects– data is passed from main program to object and from object back

to the main program

Page 9: CSCI-383 Object-Oriented Programming & Design Lecture 5

9

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 10: CSCI-383 Object-Oriented Programming & Design Lecture 5

10

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 11: CSCI-383 Object-Oriented Programming & Design Lecture 5

11

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 12: CSCI-383 Object-Oriented Programming & Design Lecture 5

12

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 13: CSCI-383 Object-Oriented Programming & Design Lecture 5

13

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 14: CSCI-383 Object-Oriented Programming & Design Lecture 5

14

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 15: CSCI-383 Object-Oriented Programming & Design Lecture 5

15

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 16: CSCI-383 Object-Oriented Programming & Design Lecture 5

16

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 17: CSCI-383 Object-Oriented Programming & Design Lecture 5

17

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Using Objects

Object A

Object B

Object C

Main

Program

Page 18: CSCI-383 Object-Oriented Programming & Design Lecture 5

18

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

How the Main ProgramUses A Class Object

• The main program does not access the data within a class object

• The main program only accesses the functions of a class object– communication occurs by passing data as parameters into the

object’s function

– the object passes data to the main program through its return type

Page 19: CSCI-383 Object-Oriented Programming & Design Lecture 5

19

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program and Object

Main

Program

Object

public:

functions

private:

data

Page 20: CSCI-383 Object-Oriented Programming & Design Lecture 5

20

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Calls a Function in the Object

Main

Program

Object

public:

functions

private:

data

Page 21: CSCI-383 Object-Oriented Programming & Design Lecture 5

21

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

The Function Accesses Data

Main

Program

Object

public:

functions

private:

data

Page 22: CSCI-383 Object-Oriented Programming & Design Lecture 5

22

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Function Returns a Value Back to the Main Program

Main

Program

Object

public:

functions

private:

data

Page 23: CSCI-383 Object-Oriented Programming & Design Lecture 5

23

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Main Program Calls a Different Function

Main

Program

Object

public:

functions

private:

data

Page 24: CSCI-383 Object-Oriented Programming & Design Lecture 5

24

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Function Calls Another Function

Main

Program

Object

public:

functions

private:

data

Page 25: CSCI-383 Object-Oriented Programming & Design Lecture 5

25

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Second Function Accesses Data

Main

Program

Object

public:

functions

private:

data

Page 26: CSCI-383 Object-Oriented Programming & Design Lecture 5

26

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Second Function Returns Back to First Function

Main

Program

Object

public:

functions

private:

data

Page 27: CSCI-383 Object-Oriented Programming & Design Lecture 5

27

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

First Function Accesses Data

Main

Program

Object

public:

functions

private:

data

Page 28: CSCI-383 Object-Oriented Programming & Design Lecture 5

28

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Function Returns Back to Main Program

Main

Program

Object

public:

functions

private:

data

Page 29: CSCI-383 Object-Oriented Programming & Design Lecture 5

29

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Example of a Class

1 class Checkbook2 {3 public:4 void setBalance( float amount );5 bool writeCheck( float amount ); 6 void deposit( float amount );7 float getBalance( );8 float getLastCheck( );9 float getLastDeposit( ); 10 private:11 float balance;12 float lastCheck;13 float lastDeposit;14 };

This class definition is placed into its own file, called the class specification file, named checkbook.h (by convention)

Page 30: CSCI-383 Object-Oriented Programming & Design Lecture 5

30

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Example of a Class (cont.)

1 class Checkbook2 {3 public:4 void setBalance( float amount );5 bool writeCheck( float amount ); 6 void deposit( float amount );7 float getBalance( );8 float getLastCheck( );9 float getLastDeposit( ); 10 private:11 float balance;12 float lastCheck;13 float lastDeposit;14 };

The writeCheck function returns false if the amount of the check is greater than the balance; returns true otherwise.

Page 31: CSCI-383 Object-Oriented Programming & Design Lecture 5

31

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Example of a Class (cont.)

1 class Checkbook2 {3 public:4 void setBalance( float amount );5 bool writeCheck( float amount ); 6 void deposit( float amount );7 float getBalance( );8 float getLastCheck( );9 float getLastDeposit( ); 10 private:11 float balance;12 float lastCheck;13 float lastDeposit;14 };

Don’t forget the semicolon.

Page 32: CSCI-383 Object-Oriented Programming & Design Lecture 5

32

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

15 #include “checkbook.h”1617 void Checkbook::setBalance( float amount )18 {19 balance = amount;20 }

Example of a Class (cont.)

The function definitions are placed into a separate file called the class implementation file. This file would be called checkbook.cpp (by convention).

Page 33: CSCI-383 Object-Oriented Programming & Design Lecture 5

33

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

15 #include “checkbook.h”1617 void Checkbook::setBalance( float amount )18 {19 balance = amount;20 }

Example of a Class (cont.)

The balance variable is declared in the private section of the class definition.

Page 34: CSCI-383 Object-Oriented Programming & Design Lecture 5

34

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

15 #include “checkbook.h”1617 void Checkbook::setBalance( float amount )18 {19 balance = amount;20 }

Example of a Class (cont.)

Special notation for class function definitions

Page 35: CSCI-383 Object-Oriented Programming & Design Lecture 5

35

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

21 bool Checkbook::writeCheck( float amount )22 {23 if ( amount > balance )24 return false;25 balance -= amount;26 lastCheck = amount;27 return true;28 }

Example of a Class (cont.)

Page 36: CSCI-383 Object-Oriented Programming & Design Lecture 5

36

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

29 void Checkbook::deposit( float amount )30 {31 balance += amount;32 lastDeposit = amount;33 }

Example of a Class (cont.)

Page 37: CSCI-383 Object-Oriented Programming & Design Lecture 5

37

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

34 float Checkbook::getBalance( ) 35 {36 return balance;37 }3839 float Checkbook::getLastCheck( )40 {41 return lastCheck;42 }

Example of a Class (cont.)

end of checkbook.cpp

Page 38: CSCI-383 Object-Oriented Programming & Design Lecture 5

38

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

1 #include <iostream>2 #include <iomanip>3 #include "checkbook.h"45 using namespace std;67 int menu( );89 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; 1011 int main( )12 {13 Checkbook cb;14 float balance, amount;15 int choice;

A Program that Uses the Checkbook Class

A main program that uses the Checkbook class is placed into a separate .cpp file

Page 39: CSCI-383 Object-Oriented Programming & Design Lecture 5

39

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

16 cout << "Enter the initial balance: $";17 cin >> balance;18 cb.setBalance( balance );1920 cout << fixed << showpoint << setprecision( 2 );

A Program that Uses the Checkbook Class (cont.)

Page 40: CSCI-383 Object-Oriented Programming & Design Lecture 5

40

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

21 choice = menu( );22 while ( choice != QUIT ) {23 if ( choice == CHECK ) {24 cout << "Enter check amount: $";25 cin >> amount;26 if ( cb.writeCheck( amount ) )27 cout << "Check accepted." << endl;28 else {29 cout << "Your balance is not high ";30 cout << "enough for that check." << endl; 31 }32 }

A Program that Uses the Checkbook Class (cont.)

body of the while loop continues

Page 41: CSCI-383 Object-Oriented Programming & Design Lecture 5

41

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

33 else if ( choice == DEPOSIT ) {34 cout << "Enter deposit amount: $";35 cin >> amount;36 cb.deposit( amount );37 cout << "Deposit accepted." << endl;38 }

A Program that Uses the Checkbook Class (cont.)

body of the while loop continues

Page 42: CSCI-383 Object-Oriented Programming & Design Lecture 5

42

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

39 else { // must be a balance request40 amount = cb.getBalance( );41 cout << "Your balance is: $" << amount << endl;42 }4344 choice = menu( );45 } 4647 return 0;48 }49

A Program that Uses the Checkbook Class (cont.)

end of while loop

Page 43: CSCI-383 Object-Oriented Programming & Design Lecture 5

43

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

50 int menu( )51 {52 int choice;5354 cout << endl;55 cout << "1 Write a check" << endl;56 cout << "2 Make a deposit" << endl;57 cout << "3 Get the balance" << endl;58 cout << "4 Quit" << endl << endl;59 cout << "Enter a number between 1 and 4: ";60 cin >> choice;61 return choice;62 }

A Program that Uses the Checkbook Class (cont.)

Page 44: CSCI-383 Object-Oriented Programming & Design Lecture 5

44

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Keep In Mind

• The data members of a class cannot be accessed by a main program

• The object always retains the current values of its data members, even when object code is no longer executing

• Each function of a class can use the data members of the class as though they have been declared within the function

Page 45: CSCI-383 Object-Oriented Programming & Design Lecture 5

45

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

If Data Members were Accessed Directly…

Class

int a;int b;int c;

Main Program

.

.

.Suppose the variables of a class were accessed by 100 places in a program

Page 46: CSCI-383 Object-Oriented Programming & Design Lecture 5

46

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

If Data Members were Accessed Directly… (cont.)

Class

int arr[10]

Main Program

.

.

.Then we need to change the way the data is repre-sented (for maintenance)

Page 47: CSCI-383 Object-Oriented Programming & Design Lecture 5

47

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

If Data Members were Accessed Directly… (cont.)

Class

int arr[10]

Main Program

.

.

.We need to change 100 lines of code in the main program!

Page 48: CSCI-383 Object-Oriented Programming & Design Lecture 5

48

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private

Class

private:int a;int b;int c;

Main Program

.

.

.Here, the main program calls foo from 100 places.

int foo( int x )

Page 49: CSCI-383 Object-Oriented Programming & Design Lecture 5

49

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private (cont.)

Class

private:int a;int b;int c;

Main Program

.

.

.Then foo accesses the private data members.

int foo( int x )

Page 50: CSCI-383 Object-Oriented Programming & Design Lecture 5

50

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private (cont.)

Class

private:

int arr[10]

Main Program

.

.

.If the data needs to change, the body of foo will need to be rewritten.

int foo( int x )

Page 51: CSCI-383 Object-Oriented Programming & Design Lecture 5

51

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private (cont.)

Class

private:

int arr[10]

Main Program

.

.

.However, the function call of foo and return type will stay the same.

int foo( int x )

Page 52: CSCI-383 Object-Oriented Programming & Design Lecture 5

52

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private (cont.)

Class

private:

int arr[10]

Main Program

.

.

.No changes need to be made in the main program!

int foo( int x )

Page 53: CSCI-383 Object-Oriented Programming & Design Lecture 5

53

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private (cont.)

Class

private:

int arr[10]

Main Program

.

.

.Program maintenance is easier this way…

int foo( int x )

Page 54: CSCI-383 Object-Oriented Programming & Design Lecture 5

54

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Data Members ShouldBe Private (cont.)

Class

private:

int arr[10]

Main Program

.

.

.especially if there is more than one program using the class.

int foo( int x )

Page 55: CSCI-383 Object-Oriented Programming & Design Lecture 5

55

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Struct vs. Class

• Functions can be placed in a struct, but only when necessary

• The public and private keywords can be left out of a class (rare)

• The public and private keywords can be placed into a struct (rare)

• So what is the difference between a struct and a class?

Page 56: CSCI-383 Object-Oriented Programming & Design Lecture 5

56

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

• If public and private are not used in a struct, all data members are public by default

• If public and private are not used in a class, all data members are private by default

Struct vs. Class (cont.)

Page 57: CSCI-383 Object-Oriented Programming & Design Lecture 5

57

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

Conventions

• By convention, we use structs when we want all data members to be public– structs are typically defined and used within the client’s

program, not in a separate file

– typically used for records of information

• By convention, we use classes when we want all data members to be private (for maintenance purposes)