C++

21
C++ Lecture 5 Monday, 18 July 2005

description

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 - PowerPoint PPT Presentation

Transcript of C++

Page 1: C++

C++

Lecture 5Monday, 18 July 2005

Page 2: C++

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

Page 3: C++

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.

Page 4: C++

Const Member Function

void Time::getHour( ) const { return hour;}

A const function cannot modify the object. C.f. 7.1-7.3

Page 5: C++

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

Page 6: C++

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.

Page 7: C++

Composition: Objects as Members of Classes

class Employee {public: …private: char firstName[25]; char lastName[25]; const Date birthDate; const Date hireDate;};

Page 8: C++

Constructor for composition

Employee(const char *, const char *, const Date &, const Date &);

How to initialize the objects inside a class like birthDate and hireDate?

Page 9: C++

Composition

Example Fig.7.6-7.10 Date class and implementation Employee class and

implementation main( ) function

Page 10: C++

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.

Page 11: C++

Declare a class as Friend

class ClassA { friend class ClassB; …

}

Page 12: C++

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

Page 13: C++

"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

Page 14: C++

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

Page 15: C++

Dynamic Memory Allocation

The "new" operator allocate memory; the "delete" operator free memory

TypeName *typeNamePtr;typeNamePtr = new TypeName;delete typeNamePtr;

Page 16: C++

Allocate an Array

int *arrayPtr = new int [10];

delete [ ] arrayptr;

new automatically invokes the constructor and delete automatically invokes the class destructor

Page 17: C++

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;}

Page 18: C++

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

Page 19: C++

Problem 1

Why the following code segment is a programming error?#include <cstring>int main( ){

char *p, s[ ] = “source”; strcat(p,s);

Page 20: C++

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;

Page 21: C++

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;}