1 / 21


C++. Lecture 5 Monday, 18 July 2005. Chapter 7 Classes, continued. const objects and const member functions Composition: objects as members of classes friend functions and friend classes “this” pointer Dynamic memory allocation. Const Objects. Time wakeup(6, 45, 0); // non-const

Download Presentation


An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.


Presentation Transcript

  1. C++ Lecture 5 Monday, 18 July 2005

  2. Chapter 7 Classes, continued • const objects and const member functions • Composition: objects as members of classes • friend functions and friend classes • “this” pointer • Dynamic memory allocation

  3. Const Objects Time wakeup(6, 45, 0); // non-const const Time noon(12, 0, 0) // const • A const object can not change its value; a const object is initialized once (by its constructor). • A const object can not call a member function, unless the member function is also declared a const.

  4. Const Member Function void Time::getHour( ) const { return hour; } • A const function cannot modify the object. C.f. 7.1-7.3

  5. Time Class Example, 7.1-3 class Time { public: Time( int = 0, int = 0, int = 0 ); // default constructor // set functions void setTime( int, int, int ); // set time void setHour( int ); // set hour … // get functions (normally declared const) int getHour() const; // return hour … // print functions (normally declared const) void printUniversal() const; // print universal time void printStandard(); // print standard time private: int hour; // 0 - 23 (24-hour clock format) … }; // end class Time

  6. Software Engineering Principles • Principle of least privilege: If objects or variables do not change, or functions that do not modify objects, declare them as const.

  7. Composition: Objects as Members of Classes class Employee { public: … private: char firstName[25]; char lastName[25]; const Date birthDate; const Date hireDate; };

  8. Constructor for composition Employee(const char *, const char *, const Date &, const Date &); How to initialize the objects inside a class like birthDate and hireDate?

  9. Composition • Example Fig.7.6-7.10 • Date class and implementation • Employee class and implementation • main( ) function

  10. Friend Functions and Classes • A friend function of a class is defined outside that class's scope, yet has the right to access private members of the class. • Friendship is neither symmetric nor transitive.

  11. Declare a class as Friend class ClassA { friend class ClassB; … }

  12. Declare a function as friend class Count { friend void setX(Count &, int); public: Count() {x = 0;} void print( ); private: int x; }; C.f. Fig.7.11-7.12

  13. "this" Pointer • Every object has access to its own address through a pointer called “this”. I.e., “this” is a pointer pointing to the object itself. C.f. Fig.7.13

  14. Cascading member function calls using "this" Class Time { public: Time &setTime( …) } // SetTime() returns a reference t.setHour(18).setMinute(30). … C.f. Fig.7.14-7.16

  15. Dynamic Memory Allocation • The "new" operator allocate memory; the "delete" operator free memory TypeName *typeNamePtr; typeNamePtr = new TypeName; delete typeNamePtr;

  16. Allocate an Array int *arrayPtr = new int [10]; delete [ ] arrayptr; • new automatically invokes the constructor and delete automatically invokes the class destructor

  17. Static Class Members • If a data member is declared as static, only one copy exists and is shared by all the instances of the class. class Employee { … static int count; }

  18. Public and Private Static Class Members • Public static: accessible through any object of that class. • Private static: accessible only through public member functions. • Static member function cannot access nonstatic data/function. C.f. Fig. 7.17-7.19

  19. Problem 1 • Why the following code segment is a programming error? #include <cstring> int main( ) { char *p, s[ ] = “source”; strcat(p,s);

  20. Problem 2 • What are printed by cout? char s1[50] = “jack”; char s3[50], s2[50] = “jill”; cout << strcpy(s3,s2) << endl; cout <<strcat(strcat(strcpy(s3,s1), “and ” ), s2) << endl; cout << strlen(s1) + strlen(s2) << endl; cout << strlen(s3) << endl;

  21. Problem 3, Find error class Example { public: Example(int y = 10) : data(y) { } int getIncData() const { return ++data; } static int getCount( ) { cout << “Data is ” << data << endl; return count; } private: int data; static int count; }

More Related