C++: Classes and Data Structures Jeffrey S. Childs

Download Report

Transcript C++: Classes and Data Structures Jeffrey S. Childs

C++ Classes and Data Structures
Jeffrey S. Childs
Chapter 1
Structs and Classes
Jeffrey S. Childs
Clarion University of PA
© 2008, Prentice Hall
1
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
2
Example Using a Struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct CarType {
string maker;
int year;
float price;
};
Don’t forget this
semicolon.
void getYourCar( CarType & car );
3
Example Using a Struct
(cont.)
15 int main( )
16 {
17
CarType myCar, yourCar;
18
19
myCar.maker = "Mercedes"; // I wish
20
myCar.year = 2005;
21
myCar.price = 45567.75;
22
4
Example Using a Struct
(cont.)
23
getYourCar( yourCar );
24
25
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;
29
30
return 0;
31 }
32
5
Example Using a Struct
(cont.)
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 }
6
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
7
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
8
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
9
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
10
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
11
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
12
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
13
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
14
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
15
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
16
Main Program Using
Objects
Object A
Main
Program
Object C
Object B
17
How the Main Program
Uses 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
18
Main Program and
Object
Object
public:
private:
functions
data
Main
Program
19
Main Program Calls a Function
in the Object
Object
public:
private:
functions
data
Main
Program
20
The Function Accesses Data
Object
public:
private:
functions
data
Main
Program
21
Function Returns a Value Back to
the Main Program
Object
public:
private:
functions
data
Main
Program
22
Main Program Calls a
Different Function
Object
public:
private:
functions
data
Main
Program
23
Function Calls Another
Function
Object
public:
private:
functions
data
Main
Program
24
Second Function Accesses
Data
Object
public:
private:
functions
data
Main
Program
25
Second Function Returns Back
to First Function
Object
public:
private:
functions
data
Main
Program
26
First Function Accesses Data
Object
public:
private:
functions
data
Main
Program
27
Function Returns Back to
Main Program
Object
public:
private:
functions
data
Main
Program
28
Example of a Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Checkbook
{
public:
void setBalance( float amount );
bool writeCheck( float amount );
void deposit( float amount );
float getBalance( );
This class definition is
float getLastCheck( );
placed into its own file,
float getLastDeposit( );
called the class
private:
specification file,
float balance;
named checkbook.h (by
float lastCheck;
convention)
float lastDeposit;
};
29
Example of a Class (cont.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Checkbook
{
public:
void setBalance( float amount );
bool writeCheck( float amount );
void deposit( float amount );
float getBalance( );
The writeCheck
float getLastCheck( );
function returns false
float getLastDeposit( );
if the amount of the
private:
check is greater than
float balance;
the balance; returns
float lastCheck;
true otherwise.
float lastDeposit;
};
30
Example of a Class (cont.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Checkbook
{
public:
void setBalance( float amount );
bool writeCheck( float amount );
void deposit( float amount );
float getBalance( );
Don’t forget the
float getLastCheck( );
semicolon.
float getLastDeposit( );
private:
float balance;
float lastCheck;
float lastDeposit;
};
31
Example of a Class (cont.)
15
16
17
18
19
20
#include “checkbook.h”
void Checkbook::setBalance( float amount )
{
balance = amount;
}
The function definitions are placed into a
separate file called the class
implementation file. This file would be
called checkbook.cpp (by convention).
32
Example of a Class (cont.)
15
16
17
18
19
20
#include “checkbook.h”
void Checkbook::setBalance( float amount )
{
balance = amount;
}
The balance variable is declared in the
private section of the class definition.
33
Example of a Class (cont.)
15
16
17
18
19
20
#include “checkbook.h”
void Checkbook::setBalance( float amount )
{
balance = amount;
}
Special notation for class function
definitions
34
Example of a Class (cont.)
21 bool Checkbook::writeCheck( float amount )
22 {
23
if ( amount > balance )
24
return false;
25
balance -= amount;
26
lastCheck = amount;
27
return true;
28 }
35
Example of a Class (cont.)
29 void Checkbook::deposit( float amount )
30 {
31
balance += amount;
32
lastDeposit = amount;
33 }
36
Example of a Class (cont.)
34
35
36
37
38
39
40
41
42
float Checkbook::getBalance( )
{
return balance;
}
float Checkbook::getLastCheck( )
{
return lastCheck;
}
end of checkbook.cpp
37
A Program that Uses the
Checkbook Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
#include "checkbook.h"
using namespace std;
A main program that
uses the Checkbook
class is placed into a
separate .cpp file
int menu( );
const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4;
int main( )
{
Checkbook cb;
float balance, amount;
int choice;
38
A Program that Uses the
Checkbook Class (cont.)
16
17
18
19
20
cout << "Enter the initial balance: $";
cin >> balance;
cb.setBalance( balance );
cout << fixed << showpoint << setprecision( 2 );
39
A Program that Uses the
Checkbook Class (cont.)
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
}
body of the while loop continues
32
}
40
A Program that Uses the
Checkbook Class (cont.)
33 else if ( choice == DEPOSIT ) {
34 cout << "Enter deposit amount: $";
35 cin >> amount;
36 cb.deposit( amount );
37 cout << "Deposit accepted." << endl;
38 }
body of the while loop continues
41
A Program that Uses the
Checkbook Class (cont.)
39
else { // must be a balance request
40
amount = cb.getBalance( );
41
cout << "Your balance is: $" << amount << endl;
42
}
43
44
choice = menu( );
45 } end of while loop
46
47 return 0;
48 }
49
42
A Program that Uses the
Checkbook Class (cont.)
50 int menu( )
51 {
52
int choice;
53
54
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 }
43
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.
44
Maintenance
• Maintenance refers to any work done on a
program after it is put into operation
• The world constantly changes
• Programs have to be maintained
(modified) to keep up with the changes
• When programs are maintained, the data
members sometimes have to change
• Private data members make it easier to
maintain a program
45
If Data Members were
Accessed Directly…
Class
Main
Program
int a;
int b;
int c;
Suppose the variables of
a class were accessed by
100 places in a program
.
.
.
46
If Data Members were
Accessed Directly… (cont.)
Class
Main
Program
int arr[10]
Then we need to change
the way the data is represented (for maintenance)
.
.
.
47
If Data Members were
Accessed Directly… (cont.)
Class
Main
Program
int arr[10]
We need to change 100
lines of code in the main
program!
.
.
.
48
Data Members Should
Be Private
Class
Main
Program
int foo( int x )
private:
int a;
int b;
int c;
Here, the main program
calls foo from 100 places.
.
.
.
49
Data Members Should
Be Private (cont.)
Class
Main
Program
int foo( int x )
private:
int a;
int b;
int c;
Then foo accesses the
private data members.
.
.
.
50
Data Members Should
Be Private (cont.)
Class
Main
Program
int foo( int x )
private:
int arr[10]
If the data needs to
change, the body of foo
will need to be rewritten.
.
.
.
51
Data Members Should
Be Private (cont.)
Class
Main
Program
int foo( int x )
private:
int arr[10]
However, the function call
of foo and return type will
stay the same.
.
.
.
52
Data Members Should
Be Private (cont.)
Class
Main
Program
int foo( int x )
private:
int arr[10]
No changes need to be
made in the main
program!
.
.
.
53
Data Members Should
Be Private (cont.)
Class
Main
Program
int foo( int x )
private:
int arr[10]
Program maintenance is
easier this way…
.
.
.
54
Data Members Should
Be Private (cont.)
Class
Main
Program
int foo( int x )
private:
int arr[10]
especially if there is more
than one program using
the class.
.
.
.
55
When Writing a Class
• Your class may be used by hundreds or
even thousands of clients (main
programmers)
• Write your class for clients, not for
computer users
• Do not put code in your class which gives
messages to users or asks users for
information – let clients handle this the
way they want
56
Debugging a Program with
Classes
• First technique – make the classes and
the main program and test everything all at
once
– inefficient: takes a long time to track down
each bug
• Second technique – test each class as it is
made by writing a main program just to
test it (called a test driver or driver)
57
First Technique
Class 1
bugs in the classes
Class 2
Main
Class 3
Class 4
Program
Let’s assume it takes 2
hours, on average, to fix
each problem.
58
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
20
0:00
Main
Class 3
Program
Class 4
59
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
19
2:00
Main
Class 3
Program
Class 4
60
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
18
4:00
Main
Class 3
Program
Class 4
61
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
17
6:00
Main
Class 3
Program
Class 4
62
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
16
8:00
Main
Class 3
Program
Class 4
63
First Technique (cont.)
Class 1
15
10:00
Runtime Errors:
Class 2
Time:
Main
Class 3
Program
Class 4
64
First Technique (cont.)
Class 1
14
12:00
Runtime Errors:
Class 2
Time:
Main
Class 3
Program
Class 4
65
First Technique (cont.)
Class 1
13
14:00
Runtime Errors:
Class 2
Time:
Main
Class 3
Program
Class 4
66
First Technique (cont.)
Class 1
12
16:00
Runtime Errors:
Class 2
Time:
Main
Class 3
Program
Class 4
67
First Technique (cont.)
Class 1
11
18:00
Runtime Errors:
Class 2
Time:
Main
Class 3
Program
Class 4
68
First Technique (cont.)
Class 1
10
20:00
Runtime Errors:
Class 2
Time:
Main
Class 3
Program
Class 4
69
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
9
22:00
Main
Class 3
Program
Class 4
70
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
8
24:00
Main
Class 3
Program
Class 4
71
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
7
26:00
Main
Class 3
Program
Class 4
72
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
6
28:00
Main
Class 3
Program
Class 4
73
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
5
30:00
Main
Class 3
Program
Class 4
74
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
4
32:00
Main
Class 3
Program
Class 4
75
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
3
34:00
Main
Class 3
Program
Class 4
76
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
2
36:00
Main
Class 3
Program
Class 4
77
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
1
38:00
Main
Class 3
Program
Class 4
78
First Technique (cont.)
Class 1
Runtime Errors:
Class 2
Time:
0
40:00
Main
Class 3
Program
Class 4
79
Using Drivers
Class 1
Class 2
Class 3
Class 4
We’ll try the second
technique – write a
driver to test each
class.
Assume it takes one
hour to write a driver.
Fixing a runtime error
will be faster – let’s
say it takes a half an
hour.
Time for first technique:
40:00
80
Using Drivers (cont.)
Class 1
Runtime Errors:
Class 2
Time:
20
0:00
Class 3
Class 4
Time for first technique:
40:00
81
Using Drivers (cont.)
Class 1
Write a Driver
Class 2
Runtime Errors:
Time:
20
0:00
Class 3
Class 4
Time for first technique:
40:00
82
Using Drivers (cont.)
Class 1
Driver
Class 2
Runtime Errors:
Time:
20
1:00
Class 3
Class 4
Time for first technique:
40:00
83
Using Drivers (cont.)
Class 1
Driver
Class 2
Runtime Errors:
Time:
19
1:30
Class 3
Class 4
Time for first technique:
40:00
84
Using Drivers (cont.)
Class 1
Driver
Class 2
Runtime Errors:
Time:
18
2:00
Class 3
Class 4
Time for first technique:
40:00
85
Using Drivers (cont.)
Class 1
Driver
Class 2
Runtime Errors:
Time:
17
2:30
Class 3
Class 4
Time for first technique:
40:00
86
Using Drivers (cont.)
Class 1
Driver
Class 2
Runtime Errors:
Time:
16
3:00
Class 3
Class 4
Time for first technique:
40:00
87
Using Drivers (cont.)
Class 1
Driver
Class 2
Runtime Errors:
Time:
15
3:30
Class 3
Class 4
Time for first technique:
40:00
88
Using Drivers (cont.)
Class 1
Class 2
Runtime Errors:
Time:
15
3:30
Class 3
Class 4
Time for first technique:
40:00
89
Using Drivers (cont.)
Class 1
Class 2
Write a Driver
Runtime Errors:
Time:
15
3:30
Class 3
Class 4
Time for first technique:
40:00
90
Using Drivers (cont.)
Class 1
Class 2
Driver
Runtime Errors:
Time:
15
4:30
Class 3
Class 4
Time for first technique:
40:00
91
Using Drivers (cont.)
Class 1
Class 2
Driver
Runtime Errors:
Time:
14
5:00
Class 3
Class 4
Time for first technique:
40:00
92
Using Drivers (cont.)
Class 1
Class 2
Driver
Runtime Errors:
Time:
13
5:30
Class 3
Class 4
Time for first technique:
40:00
93
Using Drivers (cont.)
Class 1
Class 2
Driver
Runtime Errors:
Time:
12
6:00
Class 3
Class 4
Time for first technique:
40:00
94
Using Drivers (cont.)
Class 1
Class 2
Driver
Runtime Errors:
Time:
11
6:30
Class 3
Class 4
Time for first technique:
40:00
95
Using Drivers (cont.)
Class 1
Class 2
Driver
Runtime Errors:
Time:
10
7:00
Class 3
Class 4
Time for first technique:
40:00
96
Using Drivers (cont.)
Class 1
Class 2
Class 3
Runtime Errors:
Time:
10
7:00
Class 4
Time for first technique:
40:00
97
Using Drivers (cont.)
Class 1
Class 2
Class 3
Write a Driver
Runtime Errors:
Time:
10
7:00
Class 4
Time for first technique:
40:00
98
Using Drivers (cont.)
Class 1
Class 2
Class 3
Driver
Runtime Errors:
Time:
10
8:00
Class 4
Time for first technique:
40:00
99
Using Drivers (cont.)
Class 1
Class 2
Class 3
Driver
Runtime Errors:
Time:
9
8:30
Class 4
Time for first technique:
40:00
100
Using Drivers (cont.)
Class 1
Class 2
Class 3
Driver
Runtime Errors:
Time:
8
9:00
Class 4
Time for first technique:
40:00
101
Using Drivers (cont.)
Class 1
Class 2
Class 3
Driver
Runtime Errors:
Time:
7
9:30
Class 4
Time for first technique:
40:00
102
Using Drivers (cont.)
Class 1
Class 2
Class 3
Driver
Runtime Errors:
Time:
6
10:00
Class 4
Time for first technique:
40:00
103
Using Drivers (cont.)
Class 1
Class 2
Class 3
Driver
Runtime Errors:
Time:
5
10:30
Class 4
Time for first technique:
40:00
104
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Time:
5
10:30
105
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Write a Driver
Time:
5
10:30
106
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
5
11:30
107
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
4
12:00
108
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
3
12:30
109
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
2
13:00
110
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
1
13:30
111
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
0
14:00
112
Using Drivers (cont.)
Class 1
Time for first technique:
40:00
Class 2
Class 3
Class 4
Runtime Errors:
Driver
Time:
0
14:00
113
Function Definitions in the
Class Specification
• The following slides show that function
definitions can be placed into the class
specification.
• In general, we should avoid doing this
because it is a hindrance to program
maintenance.
• It is shown here because you will see code
written this way during your career.
114
Function Definitions in the
Class Specification (cont.)
1
2
3
4
5
6
7
8
9
10
11
// checkbook.h – A class for a checkbook
class Checkbook
{
public:
void setBalance( float amount )
{ balance = amount; }
bool writeCheck( float amount ); // returns false if
// amount is greater than balance;
// otherwise returns true
void deposit( float amount ) { balance += amount;
lastDeposit = amount; }
115
Function Definitions in the
Class Specification (cont.)
12
float getBalance( ) { return balance; }
13
float getLastCheck( ) { return lastCheck; }
14
float getLastDeposit( ) { return lastDeposit; }
15 private:
16
float balance;
17
float lastCheck;
18
float lastDeposit;
19 };
116
Function Definitions in the
Class Specification (cont.)
20
21
22
23
24
25
26
27
28
29
30
31
// checkbook.cpp – function definitions for the
//
Checkbook class
#include “checkbook.h”
bool Checkbook::writeCheck( float amount )
{
if ( amount > balance )
return false;
balance -= amount;
lastCheck = amount;
return true;
}
117
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?
118
Struct vs. Class (cont.)
• 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.
119
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)
120