7-classes - Notice Exam Exam 1 will be on next Monday Cover...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Notice Exam Exam 1 will be on next Monday (2/7/11) Cover Cover material taught till this week (lab & assignment 4). Closed book and notes. Review Review Lecture Slides, Textbook (Ch 1-4), Textbook 4) Lab Lab & Homework ( 1-4). 1Ask Ask for help if you need (visit my office). 2/1/2011 1 Lecture 7 Meng Su 2/1/2011 2 Today’s Topics 1. 2. 3. 2/1/2011 Review class Pointer and class Standard C++ string library 3 1 How to link all three files to form a single project? Use #include as below! // Time.h --interface --interface // Time.cpp --implementation --implementation class Time{ #include “Time.h” public: void set(…) private: …… …… Define class: the cookie cutter void Time:: set(…) }; { …… } The separation of the interface (declaration) from its implementation is the third important feature of ADT !!! //main.cpp --user program or client --user #include “Time.h” int main( ){ Time mealTime; set(…); public mealTime.set(…); //Only public members ……} Client: produce cookies and consume them 2/1/2011 4 How How to define constructors? Constructors Constructors are methods in the class and are used to initialize the data members in the classes. classes. Special Special syntax of constructors: In In class declaration part, add : class ClassName { public: ClassName(); // same as the class name same ClassName(parameter_list); // no return type no // may have many with different parameters may Declarations of other public members private: …… }; 2/1/2011 5 Example: Figure 4.5A- Expanded Time.h #include <iostream> <iostream> using namespace std; class Time { public: Time(); Time(unsigned initHours,unsigned initMinutes, char initMinutes, initAMPM); initAMPM); void Set(unsigned hours, unsigned minutes, char am_pm); am_pm); void Display(ostream & out); Display(ostream private: unsigned myHours, myMinutes; myHours, myMinutes; char myAMorPM; myAMorPM; }; Note: Note: existence of multiple functions with the same name, but different parameter lists – are called Overloaded Functions 2/1/2011 6 2 - Implementation of constructors ClassName :: ClassName (empty or parameter_list) : member_initializer_list { // body of constructor definition } - Example: Figure 4.5B- Expanded Time.cpp 4.5B// Definition of default constructor Time :: Time(): myHours(12), myMinutes(0), Time(): myHours(12), myMinutes(0), myAMorPM('A'), myMilTime(0) myAMorPM('A'), myMilTime(0) { } // Definition of explicit-value constructor explicitTime :: Time(unsigned initHours, unsigned initHours, initMinutes, initMinutes, char initAMPM) initAMPM) { myHours = hours; myMinutes = minutes; myAMorPM = am_pm; } 2/1/2011 7 Or defined by // Definition of default constructor Time :: Time(): myHours(12), myMinutes(0), Time(): myHours(12), myMinutes(0), myAMorPM('A'), myMilTime(0) myAMorPM('A'), myMilTime(0) { } // Definition of explicit-value constructor explicitTime :: Time(unsigned initHours, unsigned initHours, initMinutes char initAMPM initMinutes, char initAMPM) { Set(initHours, initMinutes, initAMPM); Set(initHours, initMinutes, initAMPM); } 2/1/2011 8 - In the user part: main function, it is called when you declare the object (not called directly) Results Results of default constructor, in client: Time mealTime; Results of explicit-value Results licitconstructor, in client: Time bedTime(11,30,’P’); See Figure 4.5A, 4.5B on page 159-163 for more details 1592/1/2011 9 3 Other Typical Methods : Accessors Accessors Accessors or get functions See See "get" functions in expand Time class page 167-8 page 1671. In the class declaration: unsigned getHours() const; 2. In the class implementation: Time::getHours() const { return myHours; } 3. In the driver, instead of cout cout <<mealTime.myHour; // Can we do this in main? //error! Why! cout <<mealTime. getHours(); 2/1/2011 10 Other Other Typical Methods : Mutators Mutators Mutators or set functions “set" functions in Time class 1. 1. In the class declaration: void set( unsigned hours, unsigned minutes, char am_pm); am_pm); 2. In the class implementation: void Time::set(unsigned hours, unsigned minutes, char am_pm) am_pm) {… myHours = hours; myMinutes = minutes; ……} 3. In the driver, instead of mealTime.myHour mealTime.myHour = 8; //error! Can’t use! Why? mealTime.set(8, mealTime.set(8, …); 2/1/2011 11 2. Pointers to Class Objects You You can declare pointers to class objects Time * timePtr1 = &t; // static allocation // Time * timePtr2 = new Time; // dynamic allocation dynamic // Time constructor is called automatically NOTE: NOTE: Access members using “*” or “->” as following: timePtr- getMilTime() timePtr->getMilTime() or (*timePtr).getMilTime() (*timePtr).getMilTime() 2/1/2011 12 4 The this Pointer this Every Every class has a keyword, this this It It is a pointer whose value is the address of the object itself Value Value of *this would be the object itself *this 2/1/2011 13 An example of using this Pointer this In the following, the external parameter name is same as the class data member: class Time { public: void Set(unsigned hours unsigned minutes void Set(unsigned hours, unsigned minutes, char ampm); ampm); void Display(ostream & out); private: unsigned hours, minutes; minutes; char ampm; ampm; unsigned myMilTime; }; 2/1/2011 14 In In the definition of this method we use this->hours for the data member to distinguish the parameter with the same name in the methods. void Time::Set(unsigned hours, unsigned minutes, char hours, minutes, ampm) ampm) { if (hours >= 1 && hours <= 12 && minutes >= 0 && minutes (hours <= 59 && (ampm = = 'A' || ampm == 'P')) (amp { thisthis->hours = hours; thisthis-> minutes = minutes; thisthis-> ampm = ampm; myMilTime = ToMilitary(hours, minutes, am_pm); } else cerr << "*** Can't set these values ***\n"; ***\ } 2/1/2011 15 5 Standard C++ String Class #include <string> #include Variety Variety of constructors provided for defining strings Define Define (instantiate) an empty string object string string s; Define Define a string initialized with another string string s("some other string”); // also convert C-string to C++ string CNote Note further options in text, page 216 Table 5-7 5String Constructors 2/1/2011 16 The C++ String Class Methods If If you have a string object s, you can: you Get Get the size of the string by using s.length() s.length() or s.size() or s.size() Check Check if string is empty by using: s.empty() () Note Note Table 5-8 in text page 217 5- 2/1/2011 17 More More string Member Functions Categories: Categories: assignment: assign, assignment: assign, copy, data modification: append, modification: append, clear, erase, erase, insert, replace, swap substrings: find, substr find, string string is a big class defined in the library. Read text sec5.2 and table D.4 page D7 to D14 for more MSDN MSDN string members and examples @ http://msdn2.microsoft.com/enhttp://msdn2.microsoft.com/en-us/library/5zz6weyz.aspx 2/1/2011 18 6 I/0 for a string Object string Output: Output: use any ostream such as cout << s cout Input Input Can Can use >> operators >> Use getline Use getline function to put a line of input, including spaces, into a string: string address; cout << "Enter your address: "; getline(cin, getline(cin, address); Read text page 218-219 for the comparison!!! 218- 2/1/2011 19 string Overloading Operators OPERATOR MEANING >> input characters from stream up to whitespace, into string output string into stream << = assigns string on right to string object on left += appends string on right to end of contents on left + concatenates two strings references character in string using array notation >, >=, <, <=, ==, != relational operators for string comparison. Return true true or false false 2/1/2011 20 string Comparison Can Can use relational operators directly to compare string objects: string str1 = “Cmpsc465", “Cmpsc465", str2 = “Cse122"; if (str1 < str2) cout << str1 << " is less than " << str2; Comparison Comparison is performed similar to strcmp function. Result is true or false true false 2/1/2011 21 7 string Operators Example string word1, phrase; string word2 = "Dog"; cin >> word1; // user enters "Hot Tamale" // word1 has "Hot" phrase = word1 + word2; // phrase has // // "Hot Dog" phrase += " on a bun"; for (int i = 0; i < 16; i++) (int cout << phrase[i]; // displays phrase[i // "Hot Dog on a bun" 2/1/2011 22 Converters Between C++ String and C-String CFrom CFrom C-string (char array) -> to C++ string: to use the constructor: char cs[80] = “cmpsc 122” cs[80] “cmpsc string s(cs); s(cs); cout << s; // print: cmpsc 122 From From C++ string -> to C-string (char array) : to Cuse the string methods: c_str() or data() c_str() data() char * cs; cs; string s, filename; …… cs = s.c_str();// or use s.data() s.c_str();// s.data() // especially used in ifstream iso(filename.data()); // or iso(filename.data()); ifstream iso; iso; iso.open(filename.c_str()); iso.open(filename.c_str()); 2/1/2011 23 Comparing C-string and C++-string CC++C-string is an array of chars with null character at the end of the array. has has about twenty functions defined in the library <cstring> cstring> C++C++-string is a class object. has has more than half dozen of constructors and near a hundred meth methods in the library <string> th lib can can use many operators such as +, +=, <, <=,… C++ C++ string is safer and arguably easier than using C-strings CWe’ll We’ll practice standard C++ string in our new assignments. 2/1/2011 24 8 Reference Some Some slides are adapted from slides of text: ADTs, Data Structures and Problem Solving with C++, Second Edition by Nyhoff Text website: http://cs.calvin.edu/books/c++/ds/2e/ Clip Clip images are selected from WebPages of Microsoft Clip Art and Media 2/1/2011 25 9 ...
View Full Document

This note was uploaded on 10/21/2011 for the course CMPSC 122 taught by Professor Formitchev during the Spring '08 term at Penn State.

Ask a homework question - tutors are online