Download presentation
Presentation is loading. Please wait.
Published byCordelia Walton Modified over 8 years ago
1
1 C++ Classes and Data Structures Course link….. http://www.acadox.com/class/27283#resources
2
2 Classes 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
3
3 Main Program Using Objects Object A Object B Object C Main Program
4
4 Main Program Using Objects Object A Object B Object C Main Program
5
5 Main Program Using Objects Object A Object B Object C Main Program
6
6 Main Program Using Objects Object A Object B Object C Main Program
7
7 Main Program Using Objects Object A Object B Object C Main Program
8
8 Main Program Using Objects Object A Object B Object C Main Program
9
9 Main Program Using Objects Object A Object B Object C Main Program
10
10 Main Program Using Objects Object A Object B Object C Main Program
11
11 Main Program Using Objects Object A Object B Object C Main Program
12
12 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
13
13 Main Program and Object Main Program Object public: functions private: data
14
14 Main Program Calls a Function in the Object Main Program Object public: functions private: data
15
15 The Function Accesses Data Main Program Object public: functions private: data
16
16 Function Returns a Value Back to the Main Program Main Program Object public: functions private: data
17
17 Main Program Calls a Different Function Main Program Object public: functions private: data
18
18 Function Calls Another Function Main Program Object public: functions private: data
19
19 Second Function Accesses Data Main Program Object public: functions private: data
20
20 Second Function Returns Back to First Function Main Program Object public: functions private: data
21
21 First Function Accesses Data Main Program Object public: functions private: data
22
22 Function Returns Back to Main Program Main Program Object public: functions private: data
23
23 Example of a Class 1 class Checkbook 2 { 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)
24
24 Example of a Class (cont.) 1 class Checkbook 2 { 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.
25
25 Example of a Class (cont.) 1 class Checkbook 2 { 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.
26
26 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19balance = 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).
27
27 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19balance = amount; 20 } Example of a Class (cont.) The balance variable is declared in the private section of the class definition.
28
28 15 #include “checkbook.h” 16 17 void Checkbook::setBalance( float amount ) 18 { 19balance = amount; 20 } Example of a Class (cont.) Special notation for class function definitions
29
29 21 bool Checkbook::writeCheck( float amount ) 22 { 23if ( amount > balance ) 24return false; 25balance -= amount; 26lastCheck = amount; 27return true; 28 } Example of a Class (cont.)
30
30 29 void Checkbook::deposit( float amount ) 30 { 31balance += amount; 32lastDeposit = amount; 33 } Example of a Class (cont.)
31
31 34 float Checkbook::getBalance( ) 35 { 36return balance; 37 } 38 39 float Checkbook::getLastCheck( ) 40 { 41return lastCheck; 42 } Example of a Class (cont.) end of checkbook.cpp
32
32 1 #include 2 #include 3 #include "checkbook.h" 4 5 using namespace std; 6 7 int menu( ); 8 9 const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; 10 11 int main( ) 12 { 13Checkbook cb; 14float balance, amount; 15int choice; A Program that Uses the Checkbook Class A main program that uses the Checkbook class is placed into a separate.cpp file
33
33 16cout << "Enter the initial balance: $"; 17cin >> balance; 18cb.setBalance( balance ); 19 20cout << fixed << showpoint << setprecision( 2 ); A Program that Uses the Checkbook Class (cont.)
34
34 21choice = menu( ); 22while ( choice != QUIT ) { 23 if ( choice == CHECK ) { 24 cout << "Enter check amount: $"; 25 cin >> amount; 26 if ( cb.writeCheck( amount ) ) 27cout << "Check accepted." << endl; 28 else { 29cout << "Your balance is not high "; 30cout << "enough for that check." << endl; 31} 32 } A Program that Uses the Checkbook Class (cont.) body of the while loop continues
35
35 33else 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
36
36 39 else { // must be a balance request 40 amount = cb.getBalance( ); 41 cout << "Your balance is: $" << amount << endl; 42 } 43 44 choice = menu( ); 45} 46 47return 0; 48 } 49 A Program that Uses the Checkbook Class (cont.) end of while loop
37
37 50 int menu( ) 51 { 52int choice; 53 54cout << endl; 55cout << "1Write a check" << endl; 56cout << "2Make a deposit" << endl; 57cout << "3Get the balance" << endl; 58cout << "4Quit" << endl << endl; 59cout << "Enter a number between 1 and 4: "; 60cin >> choice; 61return choice; 62 } A Program that Uses the Checkbook Class (cont.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.