cmsc 202 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CMSC 202 PowerPoint Presentation
Download Presentation
CMSC 202

Loading in 2 Seconds...

play fullscreen
1 / 26

CMSC 202 - PowerPoint PPT Presentation


  • 151 Views
  • Uploaded on

CMSC 202. More C++ Classes. Announcements. Project 1 due midnight Sunday 2/25/01 Quiz #2 this week Exam 1 Wednesday/Thursday Project 2 out Monday March 5 th. const. Use “const” (constant) whenever possible Supports the concept of “least privilege” Objects may be const

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CMSC 202' - alban


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cmsc 202

CMSC 202

More C++ Classes

announcements
Announcements
  • Project 1 due midnight Sunday 2/25/01
  • Quiz #2 this week
  • Exam 1 Wednesday/Thursday
  • Project 2 out Monday March 5th
const
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
const objects
const Objects
  • We’ve already seen const float PI = 3.14;that creates a float that cannot be changed
  • We can also create const objects const Time noon(12, 0, 0);
    • noon is not modifiable
const member functions
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
non const objects and non const member functions
(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
modified time
Modified Time

class 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 );

…..

modified time cont d
Modified Time (cont’d)

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

// accessors – should be const

int GetHour (void) const; int GetMinute (void) const;

int GetSecond (void) const;

modified time cont d1
Modified Time (cont’d)

private: int hour; int minute;

int second;

};

slide10

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;

}

non member functions should have const reference parameters wherever possible
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
non member printtime
Non-member PrintTime

void PrintTime (Time& t)

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

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

cout << t.GetSecond( );

}

calling printtime
Calling PrintTime

void PrintTime (Time& t);

  • Callable using t1 – PrintTime ( t1 );
  • Not callable with noon – PrintTime (noon);
  • Why the difference?
a better printtime
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);
a subtlety
A subtlety
  • What happens if ‘noon’ is passed to the new PrintTime ( ) function, but the accessors are not const?
composition
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.
alarmclock
AlarmClock

class AlarmClock { public: AlarmClock ( ); void SetAlarm (int h, int m, int s);

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

}

this and that
“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
a this example
A “this” example

// should have error checking

void Time::SetMinute (int m){

this->minute = m;}

  • More valuable later
dynamic memory allocation
Dynamic Memory Allocation
  • In C we used malloc int *intp; intp = (int *)malloc (sizeof(int) );
  • In C++ we use new int *intp; intp = new int;or intp = new int (7);
allocating dynamic arrays
Allocating Dynamic Arrays
  • Again, in C we used malloc: int *ip2; int *ip2 = malloc (50 * sizeof(int));
  • In C++ we use new and the size int *ip2 = new int[10];
freeing dynamic memory
Freeing Dynamic Memory
  • In C we used free free (intp); // for a single int free (ip2); // or an array
  • In C++ we use delete delete intp; // for a single int delete [ ] ip2; // for an array
a simple class that uses dynamic memory
A simple class that uses dynamic memory

Class Array { public: Array (int size = 100); void putElement (int x); int getElement (void); . . . . private: int *theArray;}

array constructor
Array Constructor
  • Allocate the memory in the constructor

Array::Array (int size)

{theArray = new int [size];

}

the array destructor
The Array Destructor
  • Free the memory in the destructor

Array::~Array ( )

{delete [ ] theArray;

}

good programming practices
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