Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Lecture 5 Monday, 18 July 2005. Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.

Similar presentations


Presentation on theme: "C++ Lecture 5 Monday, 18 July 2005. Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes."— Presentation transcript:

1 C++ Lecture 5 Monday, 18 July 2005

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

3 Const Objects Time wakeup(6, 45, 0); // non-const const Time noon(12, 0, 0) // const l A const object can not change its value; a const object is initialized once (by its constructor). l 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; } l 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 l 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 l Example Fig.7.6-7.10 l Date class and implementation l Employee class and implementation l main( ) function

10 Friend Functions and Classes l A friend function of a class is defined outside that class's scope, yet has the right to access private members of the class. l 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 l 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 l 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; l new automatically invokes the constructor and delete automatically invokes the class destructor

17 Static Class Members l 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 l Public static: accessible through any object of that class. l Private static: accessible only through public member functions. l Static member function cannot access nonstatic data/function. C.f. Fig. 7.17-7.19

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

20 Problem 2 l 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; }


Download ppt "C++ Lecture 5 Monday, 18 July 2005. Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes."

Similar presentations


Ads by Google