570 likes | 592 Views
Learn how to define and use structs and classes in object-oriented programming, with examples demonstrating their usage and differences. Understand the communication between main programs and objects.
E N D
CSCI-383 Object-Oriented Programming & Design Lecture 5
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
Example Using a Struct • 1 #include <iostream> • 2 #include <iomanip> • 3 #include <string> • 4 • 5 using namespace std; • 6 • 7 struct CarType { • 8 string maker; • 9 int year; • 10 float price; • 11 }; • 12 • 13 void getYourCar( CarType & car ); • 14 Don’t forget this semicolon.
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
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
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 }
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
Classes • A classis 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
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
Main Program Using Objects Object A Main Program Object C Object B
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
Main Program and Object Object public: functions private: data Main Program
Main Program Calls a Function in the Object Object public: functions private: data Main Program
The Function Accesses Data Object public: functions private: data Main Program
Function Returns a Value Back to the Main Program Object public: functions private: data Main Program
Main Program Calls a Different Function Object public: functions private: data Main Program
Function Calls Another Function Object public: functions private: data Main Program
Second Function Accesses Data Object public: functions private: data Main Program
Second Function Returns Back to First Function Object public: functions private: data Main Program
First Function Accesses Data Object public: functions private: data Main Program
Function Returns Back to Main Program Object public: functions private: data Main Program
Example of a Class • 1 class Checkbook • 2 { • 3 public: • 4 void setBalance( float amount ); • bool writeCheck( float amount ); • 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)
Example of a Class (cont.) • 1 class Checkbook • 2 { • 3 public: • 4 void setBalance( float amount ); • bool writeCheck( float amount ); • 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.
Example of a Class (cont.) • 1 class Checkbook • 2 { • 3 public: • 4 void setBalance( float amount ); • bool writeCheck( float amount ); • 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.
Example of a Class (cont.) • 15 #include “checkbook.h” • 16 • 17 void Checkbook::setBalance( float amount ) • 18 { • 19 balance = amount; • 20 } The function definitions are placed into a separate file called the class implementation file. This file would be called checkbook.cpp (by convention).
Example of a Class (cont.) • 15 #include “checkbook.h” • 16 • 17 void Checkbook::setBalance( float amount ) • 18 { • 19 balance = amount; • 20 } The balance variable is declared in the private section of the class definition.
Example of a Class (cont.) • 15 #include “checkbook.h” • 16 • 17 void Checkbook::setBalance( float amount ) • 18 { • 19 balance = amount; • 20 } Special notation for class function definitions
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 }
Example of a Class (cont.) • 29 void Checkbook::deposit( float amount ) • 30 { • 31 balance += amount; • 32 lastDeposit = amount; • 33 }
Example of a Class (cont.) • 34 float Checkbook::getBalance( ) • 35 { • 36 return balance; • 37 } • 38 • 39 float Checkbook::getLastCheck( ) • 40 { • 41 return lastCheck; • 42 } end of checkbook.cpp
A Program that Uses the Checkbook Class • 1 #include <iostream> • 2 #include <iomanip> • 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 { • 13 Checkbook cb; • 14 float balance, amount; • 15 int choice; A main program that uses the Checkbook class is placed into a separate .cpp file
A Program that Uses the Checkbook Class (cont.) • 16 cout << "Enter the initial balance: $"; • 17 cin >> balance; • 18 cb.setBalance( balance ); • 19 • 20 cout << fixed << showpoint << setprecision( 2 );
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 } • 32 } body of the while loop continues
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
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 } • 46 • 47 return 0; • 48 } • 49 end of while loop
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 }
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
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
If Data Members were Accessed Directly… (cont.) Class Main Program int arr[10] . . . Then we need to change the way the data is repre-sented (for maintenance)
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!
Data Members ShouldBe Private Class Main Program int foo( int x ) private: int a; int b; int c; . . . Here, the main program calls foo from 100 places.
Data Members ShouldBe Private (cont.) Class Main Program int foo( int x ) private: int a; int b; int c; . . . Then foo accesses the private data members.
Data Members ShouldBe 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.