26
1 CMSC 202 More C++ Classes

1 CMSC 202 More C++ Classes. 2 Announcements Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March

Embed Size (px)

Citation preview

1

CMSC 202

More C++ Classes

2

Announcements

• Project 1 due midnight Sunday 2/25/01

• Quiz #2 this week

• Exam 1 Wednesday/Thursday

• Project 2 out Monday March 5th

3

const

• Use “const” (constant) whenever possible

• Supports the concept of “least privilege”

• Objects may be const

• Member functions may be const

• Parameters may be const

4

const Objects

• We’ve already seenconst float PI = 3.14;

that creates a float that cannot be changed

• We can also create const objectsconst Time noon(12, 0, 0);

– noon is not modifiable

5

const member functions

• Not all member functions modify the object– Accessors– Others later

• And so should be declared ‘const’int GetHour ( void ) const;

• The compiler doesn’t “know” that GetHour( ) doesn’t modify the Time object

6

(non-)const objects and(non-)const member functions

• Rules of engagement:– const member functions may be invoked for

const and non-const objects– non-const member functions can only be

invoked for non-const objects– you cannot invoke a non-const member

function on a const object

• Constructors and Destructors may not be const

7

Modified Timeclass Time {

public:Time (int h = 0, int m = 0, int s = 0 );void InitTime (int h, int m, int s);void PrintMilitary ( void ) const;void PrintStd ( void ) const;void Increment ( void );

…..

8

Modified Time (cont’d)

// mutatorsvoid SetHour (int h);void SetMinute (int m);void SetSecond (int s);

// accessors – should be constint GetHour (void) const;int GetMinute (void) const;int GetSecond (void) const;

9

Modified Time (cont’d)

private:int hour;int minute;

int second;

};

10

int main ( ) {

Time t1 (2, 4, 7); const Time noon (12);

cout << “Initial Military Time for t1 is :” ; t1.PrintMilitary ( ); // okay cout << endl;

cout << “Noon in Military Time:” noon.PrintMilitary ( ); // okay cout << endl;

cout << “Increment Noon:” ; noon.Increment ( ); // compiler error cout << endl;

}

11

Non-member functions should have const reference parameters

wherever possible

• Rules of engagement:– Non-const objects may be passed to const and

non-const arguments– Const objects may only be passed to const

arguments

• Bottom line – you can’t pass a const object to a non-const argument

12

Non-member PrintTime

void PrintTime (Time& t)

{cout << t.GetHour( ) << “:”;

cout << t.GetMinute( ) << “:”;

cout << t.GetSecond( );

}

13

Calling PrintTime

void PrintTime (Time& t);

• Callable using t1 – PrintTime ( t1 );

• Not callable with noon – PrintTime (noon);

• Why the difference?

14

A better PrintTime

• Since PrintTime( ) doesn’t modify the Time object passed to it, the argument should be const

void PrintTime (const Time& t)

• Doing so doesn’t change the code, but now does allow noon to use the function

PrintTime (noon);

15

A subtlety

• What happens if ‘noon’ is passed to the new PrintTime ( ) function, but the accessors are not const?

16

Composition

• Sometimes objects are used as members of other objects. For example, an AlarmClock class may include a Time object.

• This is known as “composition” or “aggregation”.

• Member objects are constructed automatically

• A common form of code reuse.

17

AlarmClock

class AlarmClock { public:

AlarmClock ( );void SetAlarm (int h, int m, int s);. . .

private:. . .void ring( ) const;Time currentTime;const Time alarmTime;

}

18

“this” and that

• Every object has access to it’s own address through a pointer called this.

• The this is implicitly used to reference both data members and member functions of an object… it can also be used explicitly.

• This is passed implicitly as the first argument of every (non-static) member function

19

A “this” example

// should have error checking

void Time::SetMinute (int m){

this->minute = m;}

• More valuable later

20

Dynamic Memory Allocation

• In C we used mallocint *intp;intp = (int *)malloc (sizeof(int) );

• In C++ we use newint *intp;intp = new int;

or intp = new int (7);

21

Allocating Dynamic Arrays

• Again, in C we used malloc:int *ip2;int *ip2 = malloc (50 * sizeof(int));

• In C++ we use new and the sizeint *ip2 = new int[10];

22

Freeing Dynamic Memory

• In C we used freefree (intp); // for a single intfree (ip2); // or an array

• In C++ we use deletedelete intp; // for a single intdelete [ ] ip2; // for an array

23

A simple class that uses dynamic memory

Class Array { public:

Array (int size = 100);void putElement (int x);int getElement (void);. . . .

private:int *theArray;

}

24

Array Constructor

• Allocate the memory in the constructor

Array::Array (int size)

{theArray = new int [size];

}

25

The Array Destructor

• Free the memory in the destructor

Array::~Array ( )

{delete [ ] theArray;

}

26

Good Programming Practices

• Use const wherever possible• Declare member functions that do not change the

object as const• Avoid friends• Use member initialization lists• Allocate dynamic memory only in the constructor• Delete dynamic memory only in the destructor