Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Introduction to Object Oriented Programming (Continued) Cats II.

Similar presentations


Presentation on theme: "11 Introduction to Object Oriented Programming (Continued) Cats II."— Presentation transcript:

1 11 Introduction to Object Oriented Programming (Continued) Cats II

2 22 Objectives You will be able to: Create objects dynamically. Use dynamically created objects in a C++ program. Get keyboard input from a user in a C++ program.

3 33 Get the Cats Program If you don’t have the Cats program from last class on your computer, you can download it from the class web site: http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/ 2011_01_19_Cats/ http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/ 2011_01_19_Cats/

4 44 Dynamic Allocation We can dynamically create objects of any class using new. Similar to malloc in C. Allocates space on the heap. Invokes constructor to initialize the object. Returns a pointer to the object.

5 55 Dynamic Allocation #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat* Fluffy = new Cat("Fluffy", dob, 8.4); Fluffy->Display(); cout << endl; cin.get(); // Hold window open. return 0; } Note arrow. Fluffy is now a pointer.

6 66 Object Lifetime Objects allocated by declaration are deallocated when the function exits. Like all local variables. Objects allocated with new continue to exist until explicitly deleted. delete c1; c1 is a pointer to the object that is to be deleted.

7 77 Explicitly Deleting an Object #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat* Fluffy = new Cat("Fluffy", dob, 8.4); Fluffy->Display(); cout << endl; delete Fluffy; Fluffy = 0; cin.get(); // Hold window open. return 0; }

8 88 Getting User Input What if we want the user to specify the attributes of a Cat. Could overload the constructor and provide a version that asks for input. Better to provide a separate function outside the class definition. Separate UI from class logic. Let’s write a function that asks the user for information about a Cat, creates a Cat object, and returns a pointer to the object.

9 99 main.cpp Cat* Create_Cat() { char name[21]; Date date_of_birth; double weight; cout << "Please enter information for new Cat\n"; cout << "Name (up to 20 characters): "; cin.getline(name, 20); cout << "Date of Birth:\n"; cout << " Month: "; cin >> date_of_birth.Month; cout << " Day: "; cin >> date_of_birth.Day; cout << " Year: "; cin >> date_of_birth.Year; cout << "Weight: "; cin >> weight; Cat* cat = new Cat(name, date_of_birth, weight); return cat; } http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/Create_Cat.cpp.txt

10 10 Getting User Input int main() { cout << "This is program Cats\n"; Cat* cat1 = Create_Cat(); cat1->Display(); cout << endl; delete cat1; cat1 = 0; cin.get(); // Hold window open. cin.get(); return 0; } In file main.cpp

11 11 Running Program Cats

12 12 Running Program Cats

13 13 Problems with String Input C++ String input is tricky! What happens if the user inputs more than 20 characters for the cat's name? Try it! Use Debug > Start without Debugging Or Ctrl+F5

14 14 Avoiding the Problem Read into a large buffer array. Operating system will limit input to some reasonable amount. (~255 characters) Copy up the the max number of characters into the real input array. If the user enters too many characters? Output an error message and ask the user to try again. or Just ignore the excess input.

15 15 Function Get_String() void Get_String(char* prompt, char* input_array, size_t max_chars) { char input_buffer[1000]; while (true) { cout << prompt; cin.getline(input_buffer, 1000); if (strlen(input_buffer) <= max_chars) { break; } cout << "Please enter no more than " << max_chars << " characters\n"; } strcpy(input_array, input_buffer); } http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/Get_String.cpp.txt

16 16 Using Get_String() Cat* Create_Cat() { char name[21]; Date date_of_birth; double weight; cout << "Please enter information for new Cat\n"; Get_String("Name (up to 20 characters): ", name, 20); cout << "Date of Birth:\n";...

17 17 Program Running

18 18 Long Input String Exactly 20 characters

19 19 Multiple Cats int main() { cout << "This is program Cats\n"; while (true) { Cat* cat1 = Create_Cat(); cat1->Display(); cout << endl; delete cat1; cat1 = 0; }...

20 20 Program Running Whoa! What happened here?

21 21 A Problem with cin >> cin >> weight; User enters 8.4 (followed by "Enter") The >> operator extracts "8.4" from the keyboard input buffer. Leaves the newline character. Next input sees the newline character as the first thing in the buffer. No problem when next input is for a numeric value. Big problem when next input is for a string.

22 22 A Related Problem The final "." in input "8.4." was left in the keyboard input buffer and read as name of the next cat.

23 23 Solution After numeric input, clear the newline character (and potential garbage) from the keyboard input buffer before doing the next real input. while (cin.get() != '\n') ; Do nothing

24 24 Clear_Keyboard_Input_Buffer() void Clear_Keyboard_Input_Buffer() { while (cin.get() != '\n') ; } Call this function after getting a numeric value to clear the newline character following the numeric text. Also handles the problem when the user enters nonnumeric text after the numeric value.

25 25 Using Clear_Keyboard_Input_Buffer Cat* Create_Cat() {... cout << "Date of Birth:\n"; cout << " Month: "; cin >> date_of_birth.Month; cout << " Day: "; cin >> date_of_birth.Day; cout << " Year: "; cin >> date_of_birth.Year; cout << "Weight: "; cin >> weight; Clear_Keyboard_Input_Buffer(); Cat* cat = new Cat(name, date_of_birth, weight); return cat; }

26 26 Program in Action Garbage at end of numeric inputs was silently ignored.

27 27 Assignment Do today's examples for yourself if you have not done them in class. Read Chapter 10. Project 1 End of Presentation


Download ppt "11 Introduction to Object Oriented Programming (Continued) Cats II."

Similar presentations


Ads by Google