1/17/2011
C++ Classes and Data Structures Structs
Jeffrey S. Childs
• A struct holds data, like an array
• Each unit of data in a struct is called a
Chapter 1 data member (or member)
– they are called “elements” in arrays
Structs and Classes • In a struct, each data member can have a
different data type
Jeffrey S. Childs
Clarion University of PA – in arrays, the data type of each element is the
© 2008, Prentice Hall same
1 2
Example Using a Struct Example Using a Struct
1 #include <iostream>
2 #include <iomanip>
(cont.)
3 #include <string>
4 15 int main( )
5 using namespace std; 16 {
6 17 CarType myCar, yourCar;
7 struct CarType { 18
8 string maker; 19 [Link] = "Mercedes";
9 int year; 20 [Link] = 2005;
10 float price; Don’t forget this 21 [Link] = 45567.75;
11 }; 22
semicolon.
12
13 void getYourCar( CarType & car );
14
3 4
Example Using a Struct Example Using a Struct
(cont.) (cont.)
23 getYourCar( yourCar ); 33 void getYourCar( CarType & car )
24 34 {
25 cout << "Your car is a: " << [Link] << endl;
35 cout << "Enter your maker: ";
26 cout << fixed << showpoint << setprecision( 2 ) <<
27 "I'll offer $" << [Link] - 100 <<
36 cin >> [Link];
28 " for your car." << endl; 37 cout << "Enter the year: ";
29 38 cin >> [Link];
30 return 0; 39 cout << "Enter the price: $";
31 } 40 cin >> [Link];
32 41 }
5 6
1
1/17/2011
Object Assignment Classes
• An object of a struct can be assigned to • A class is similar to a struct
another object of the same struct type: • A class contains data members, but it also
myCar = yourCar; contains function members
• Objects are made from classes, similarly to the
• This assigns each data member in yourCar
way that objects are made from structs
to the corresponding data member of myCar
• The main program communicates with the
• Also assigns any array data members objects
– data is passed from main program to object and from
object back to the main program
7 8
Main Program and
How the Main Program Uses a
Object
Class Object
Object
• main program does not access the data
within a class object directly public: private:
• It only accesses the functions of a class functions data
object
– communication occurs by passing data as Main
parameters into the object’s function
Program
– the object passes data to the main program
through its return type
9 10
Main Program Calls a Function The Function Accesses Data
in the Object
Object Object
public: private: public: private:
functions data functions data
Main Main
Program Program
11 12
2
1/17/2011
Function Returns a Value Back to Main Program Calls a
the Main Program Different Function
Object Object
public: private: public: private:
functions data functions data
Main Main
Program Program
13 14
Function Calls Another Second Function Accesses
Function Data
Object Object
public: private: public: private:
functions data functions data
Main Main
Program Program
15 16
Second Function Returns Back First Function Accesses Data
to First Function
Object Object
public: private: public: private:
functions data functions data
Main Main
Program Program
17 18
3
1/17/2011
Function Returns Back to
Example of a Class
Main Program
Object 1 class Checkbook
2 {
3 public:
public: private: 4 void setBalance(float amount );
5 bool writeCheck(float amount );
functions data
6 void deposit(float amount );
7 float getBalance(); This class definition is
Main 8 float getLastCheck(); placed into its own file,
9 float getLastDeposit(); called the class
Program 10 private: specification file,
11 float balance; named checkbook.h (by
12 float lastCheck; convention)
13 float lastDeposit;
19
14 }; 20
Example of a Class (cont.) Example of a Class (cont.)
15 #include “checkbook.h” 15 #include “checkbook.h”
16 16
17 void Checkbook::setBalance( float amount ) 17 void Checkbook::setBalance( float amount )
18 { 18 {
19 balance = amount; 19 balance = amount;
20 } 20 }
The function definitions are placed into a The balance variable is declared in the
separate file called the class implementation file. private section of the class definition.
This file would be called [Link] (by
convention).
21 22
Example of a Class (cont.) Example of a Class (cont.)
15 #include “checkbook.h”
16 21 bool Checkbook::writeCheck( float amount )
17 void Checkbook::setBalance( float amount ) 22 {
18 { 23 if ( amount > balance )
19 balance = amount; 24 return false;
20 } 25 balance -= amount;
26 lastCheck = amount;
Special notation for class function definitions 27 return true;
28 }
23 24
4
1/17/2011
Example of a Class (cont.) Example of a Class (cont.)
29 void Checkbook::deposit( float amount ) 34 float Checkbook::getBalance( )
30 { 35 {
31 balance += amount; 36 return balance;
32 lastDeposit = amount; 37 }
33 } 38
39 float Checkbook::getLastCheck( )
40 {
41 return lastCheck;
42 }
end of [Link]
25 26
A Program that Uses the A Program that Uses the
Checkbook Class Checkbook Class (cont.)
1 #include <iostream>
2 #include <iomanip> A main program that
3 #include "checkbook.h" uses the Checkbook
4 class is placed into a 16 cout << "Enter the initial balance: $";
5 using namespace std; separate .cpp file
6 17 cin >> balance;
7 int menu( ); 18 [Link]( balance );
8 19
9 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; 20 cout << fixed << showpoint << setprecision( 2 );
10 21 choice = menu( );
11 int main( )
12 {
13 Checkbook cb;
14 float balance, amount;
15 int choice;
27 28
A Program that Uses the A Program that Uses the
Checkbook Class (cont.) Checkbook Class (cont.)
22 while ( choice != QUIT ) {
23 if ( choice == CHECK ) {
24 cout << "Enter check amount: $"; 33 else if ( choice == DEPOSIT ) {
25 cin >> amount; 34 cout << "Enter deposit amount: $";
26 if ( [Link]( amount ) ) 35 cin >> amount;
27 cout << "Check accepted." << endl; 36 [Link]( amount );
28 else { 37 cout << "Deposit accepted." << endl;
29 cout << "Your balance is not high "; 38 }
30 cout << "enough for that check." << endl;
31 } body of the while loop continues
32 }
body of the while loop continues
29 30
5
1/17/2011
A Program that Uses the A Program that Uses the
Checkbook Class (cont.) Checkbook Class (cont.)
50 int menu( )
39 else { // assumes it must be a balance request 51 {
40 amount = [Link]( ); 52 int choice;
41 cout << "Your balance is: $" << amount << endl; 53
42 } 54 cout << endl;
43 55 cout << "1 Write a check" << endl;
44 choice = menu( ); 56 cout << "2 Make a deposit" << endl;
45 } 57 cout << "3 Get the balance" << endl;
46 end of while loop
58 cout << "4 Quit" << endl << endl;
47 return 0; 59 cout << "Enter a number between 1 and 4: ";
48 } end of main 60 cin >> choice;
49 61 return choice;
62 }
31 32
Keep In Mind Maintenance
• Maintenance refers to any work done on a
• The data members of a class cannot be program after it is put into operation
accessed directly by a main program.
• The world constantly changes
• The object always retains the current
• Programs have to be maintained
values of its data members, even when
(modified) to keep up with the changes
object code is no longer executing.
• When programs are maintained, the data
• Each function of a class can use the data
members sometimes have to change
members of the class as though they have
been declared within the function. • Private data members make it easier to
maintain a program
33 34
When Writing a Class Debugging a Program with Classes
• Your class may be used by hundreds or • First technique – make the classes and
even thousands of clients or drivers the main program and test everything all at
once
• Write your class for clients, not for – inefficient: takes a long time to track down
computer end-users each bug
• Do not put code in your class which gives • Second technique – test each class as it is
messages to users or asks users for made by writing a main program just to
information – let clients handle this the test it (called a test driver)
way they want
35 36
6
1/17/2011
Function Definitions in the Struct vs. Class
Class Specification
• Functions can be placed in a struct, but
only when necessary
• Function definitions can be placed into the
class specification file (with the .h • The public and private keywords can be
extension). left out of a class (rarely done).
• The public and private keywords can be
• Avoid doing this because it is a hindrance
placed into a struct (rarely done).
to program maintenance.
• So what is the difference between a struct
and a class?
37 38
Struct vs. Class (cont.) Conventions
• If public and private are not used in a • By convention, we use structs when we
struct, all data members are public by want all data members to be public
default. – structs are typically defined and used within
the client’s program, not in a separate file
• If public and private are not used in a – typically used for records of information
class, all data members are private by
default. • By convention, we use classes when we
want all data members to be private (for
maintenance, data verification, or other
purposes)
39 40