PMT...c c c j cc c c 3 (cc c c c c cˇ c cˇ c c cˇ c cˇ c c c
C++
description
Transcript of C++
C++
Lecture 5Monday, 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-constconst 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.
Const Member Function
void Time::getHour( ) const { return hour;}
A const function cannot modify the object. C.f. 7.1-7.3
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
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.
Composition: Objects as Members of Classes
class Employee {public: …private: char firstName[25]; char lastName[25]; const Date birthDate; const Date hireDate;};
Constructor for composition
Employee(const char *, const char *, const Date &, const Date &);
How to initialize the objects inside a class like birthDate and hireDate?
Composition
Example Fig.7.6-7.10 Date class and implementation Employee class and
implementation main( ) function
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.
Declare a class as Friend
class ClassA { friend class ClassB; …
}
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
"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
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
Dynamic Memory Allocation
The "new" operator allocate memory; the "delete" operator free memory
TypeName *typeNamePtr;typeNamePtr = new TypeName;delete typeNamePtr;
Allocate an Array
int *arrayPtr = new int [10];
delete [ ] arrayptr;
new automatically invokes the constructor and delete automatically invokes the class destructor
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;}
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
Problem 1
Why the following code segment is a programming error?#include <cstring>int main( ){
char *p, s[ ] = “source”; strcat(p,s);
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;
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;}