Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in.

Similar presentations


Presentation on theme: "1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in."— Presentation transcript:

1 1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in Ch. 1–10, NOT Ch. 18 No alternate dates and no makeup  Quiz #2 today  Classes: a Deeper Look, Part 2 (Ch. 10) Using const Composition Keyword “friend” The “this” pointer static members  Demo – A Simple Library Application  Midterm review 21-Jun-2006

2 2 Quiz #2 Closed book 20 minutes Please clear your desks and log off from the computer

3 3 Classes: Part 2 Chapter 10

4 4 Using const  Use const to specify that no changes should be made  An object can be const  A member function can be const  If an object is declared const, then only its const members can be used Classes: Part 2 (Deitel, 524–534)

5 5 Example class Person{ public: Person( string nm="" ):name(nm){} string getName() const { return name; } void setName( string nm ){ name = nm; } private: string name; }; int main(){ const Person admin( "Bob" ); cout << admin.getName() << endl; } Classes: Part 2 (Deitel, 524–534)

6 6 Initializing const Data Members  A class can have a const data member  It must be initialized in the initializer of the constructor class List{ public: List( int cp=1024 ) : CAPACITY(cp), sz(0){ myData = new int[CAPACITY]; } private: const int CAPACITY; int sz; int *myData; }; Classes: Part 2 (Deitel, 524–534)

7 7 Composition  When a data member is an object of another class class Date{ public: Date( int mo=1, int dy=1, int yr=1970 ) : month(mo), day(dy),year(yr){} void printDate(){ cout << month << "/" << day << "/" << year; } private: int month, day, year; }; class Person{ public: Person( string nm="", Date bd=Date() ):name(nm), birthdate(bd){} string getName() const { return name; } Date getBirthdate(){ return birthdate; } private: string name; Date birthdate; }; Classes: Part 2 (Deitel, 534–541)

8 8 friend Functions  In some classes, it is convenient to give non- member functions access to its private data members  friend function Defined outside the class Can access the private data members of the class  Advantage More efficient than using accessors in the function’s definition Appropriate when the function uses more than one object Classes: Part 2 (Deitel, 541–545)

9 9 Example class Person{ friend bool equal( Person, Person ); public: Person( string nm="" ) : name(nm) {} string getName() const { return name; } private: string name; }; bool equal( Person p1, Person p2 ){ return p1.name == p2.name; } int main(){ Person admin( "Bob" ); Person user( "Bob" ); if( equal( admin, user ) ) cout << "admin and user are equal." << endl; } Classes: Part 2 (Deitel, 541–545)

10 10 The this Pointer  Every member function has access to the address of its object through a pointer called “ this ”  Inside the function definition, “ this ” is implicit when a data member or function is used  We can also use “ this ” explicitly //Two member functions with identical meanings void setName( string nm ){ name = nm; } void setName( string nm ){ this->name = nm; }  Returning *this Used to return a reference to the object itself Permits cascading function calls Classes: Part 2 (Deitel, 545–550)

11 11 Example class Person{ public: Person( string nm="" ) : name(nm) {} string getName() const { return name; } Person& setName( string nm ){ name = nm; return *this; } void printPerson(){ cout << name << endl; } private: string name; }; int main(){ Person bob; bob.setName("Bob").printPerson(); } Left part is evaluated first, and returns a reference to bob The next part is evaluated as bob.printPerson() Classes: Part 2 (Deitel, 545–550)

12 12 Review of new and delete  When we cannot predict how many objects our program will need, we can use “ new ” to allocate them dynamically, as we need them, while the program is running  new Allocates memory from the free store (the big block of memory that the compiler gives your program to use when it needs it) Calls the constructor so that the memory is initialized and values are stored in that memory location Returns a pointer to the memory  It is important to keep track of the pointer returned by new Memory allocated by new must be freed by delete when it is no longer needed, and delete has to be used with a pointer If delete is not used correctly, then a memory leak can occur Classes: Part 2 (Deitel, 550–552)

13 13 Bad Example Person& addPerson(){ Person *pTempPerson = new Person; return *pTempPerson; //Don’t ever do this } Person& refPerson = addPerson();  In this example, the pointer variable pTempPerson goes out of scope when the function ends  Since delete can only be used with a pointer, the memory allocated in the function may never be recovered  Solution – allocate memory for the object in the calling function Classes: Part 2 (Deitel, 550–552)

14 14 Static Members  Used when we want only one copy of a data member that will be shared by all objects of a class  Typical uses Counting the number of objects Data that must be identical for all objects, e.g. a data member that holds the interest rate for all Account objects  Can be private or public  If it is private, create a public static member function to access it  Place the initialization in the cpp file containing the class definition instead of the header file Classes: Part 2 (Deitel, 552–558)

15 15 Example class Person{ public: Person( string nm="" ) : name(nm) { personCount++; } string getName() const { return name; } static int getPersonCount(){ return personCount; } private: string name; static int personCount; }; int Person::personCount = 0; //Initialize int main(){ Person admin( "Bob" ); Person user( "Carol" ); cout << "Count = " << Person::getPersonCount() << endl; } Classes: Part 2 (Deitel, 552–558)

16 16 Drawing UML Diagrams  Microsoft Visio Excellent tool, available in PC Lab, can be purchased from UHCL Category = Software Template = UML Model Diagram Save as jpg, and then Insert into MS Word  Microsoft PowerPoint Good tool, available in PC Lab Save as jpg, and then Insert into MS Word Classes: Part 2

17 17 Demo A Simple Library Application

18 18 A Simple Library Application Phase 1 – What are we making? Requirements 1.The library will have books and journals. 2.Multiple copies of these library items will be stocked. 3.The library user will need to view a list of the library items that are in the library’s collection. Demo Library user View a list of library items Add a library item Use case diagram

19 19 Phase 2: How will we build it? Analysis and Design Requirements 1.The library will have books and journals. 2.Multiple copies of these library items will be stocked. 3.The library user will need to view a list of the library items that are in the library’s collection. Demo

20 20 Phase 2: How will we build it? Analysis and Design Requirements 1.The library will have books and journals. 2.Multiple copies of these library items will be stocked. 3.The library user will need to view a list of the library items that are in the library’s collection. Demo LibraryItem – title : string – copies : int Library Class diagram List BookJournal 1 * 11

21 21 Phase 3: Build the first version Get it working, and create tests Classes 1.Book 2.BookList 3.Library Tests Some simple functions that we can call in main() Demo **

22 22 Midterm Review

23 23 Midterm Exam  25% of your grade for the course  June 26 at 6 p.m.  Delta D202 (Our regular classroom)  No makeup exam  Closed book  Closed notes Midterm Review

24 24 Material Covered  Anything in the slides and handouts  Chapters 1–10 of our textbook Object-Oriented Programming Basic C++ Programming Control Structures Functions Arrays Pointers and Strings Classes Midterm Review

25 25 Test Format  Approximately 20 questions  Short C++ programs Write a short C++ program – it must be complete and compilable Write the C++ code for a class from a given class diagram  Short answers – write a line of C++ code  Diagrams – e.g., memory contents of pointers  Multiple choice  Locate errors in code Midterm Review

26 26 Suggestions for Studying  Look at the Learning Objectives on the course syllabus  Concentrate your study time on the major topics that we have covered in class  Make sure that you know what the basic C++ features do and how to use them Can you write a program to get integers from the user? Can you write a program to print the result of a calculation? Can you write a simple C++ class, including the data members and member functions, like the examples on the slides?  Review the handouts distributed in class Make sure that you can do the problems You can download the handouts from the class discussion group page Midterm Review

27 27 References Budd, T., An Introduction to Object-Oriented Programming, Third Edition. Boston: Addison Wesley, 2002. Deitel, H. M., and P. J. Deitel, C++ How to Program, Fourth Edition. Upper Saddle River, NJ: Prentice Hall, 2003. Goodrich, M. T., R. Tamassia, and D. Mount, Data Structures and Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004.


Download ppt "1 Today’s Objectives  Announcements Homework #2 is due next Monday, 26-Jun, at the beginning of class Midterm Exam is Monday, 26-Jun – over material in."

Similar presentations


Ads by Google