Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer 2003 1 Summary of Topics Related to Classes Class definition Defining member.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer 2003 1 Summary of Topics Related to Classes Class definition Defining member."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member functions outside Class definition Defining member functions inside Class definition (sometimes used for small functions) Accessing Class members Constant ( const ) objects and functions Initializing const data members friend functions static members The this pointer

2 2 Basic Class definition example 1 class Time { 2 3 public: 4 Time(); // constructor 5 void setTime( int, int, int ); // set hour, minute, second 6 void printUniversal(); // print universal-time format 7 void printStandard(); // print standard-time format 8 9 private: 10 int hour; // (24-hour clock format) 11 int minute; // int second; // }; // end class Time Definition of class begins with keyword class. Class body starts with left brace. Class body ends with right brace. Definition terminates with semicolon. Function prototypes for public member functions. private data members accessible only to member functions.

3 3 14 class Time { public: 17 Time(); // constructor 18 void setTime( int, int, int ); // set hour, minute, second 19 void printUniversal(); // print universal-time format 20 void printStandard(); // print standard-time format private: 23 int hour; // (24-hour clock format) 24 int minute; // int second; // }; // end class Time Time::Time() 30 { 31 hour = minute = second = 0; } // end Time constructor void Time::setTime( int h, int m, int s ) 36 { 37 hour = ( h >= 0 && h < 24 ) ? h : 0; 38 minute = ( m >= 0 && m < 60 ) ? m : 0; 39 second = ( s >= 0 && s < 60 ) ? s : 0; } // end function setTime... Defining member functions outside class definition ReturnType ClassName::MemberFunctionName( ) { … } Each class must have a constructor function (same name as class): initializes data members of a class object when called

4 4 14 class Time { public: 17 Time(); // constructor 18 void setTime( int h, int m, int s ){ 19 hour = ( h >= 0 && h < 24 ) ? h : 0; 20 minute = ( m >= 0 && m < 60 ) ? m : 0; 21 second = ( s >= 0 && s < 60 ) ? s : 0; 22 } // end function setTime void printUniversal(); // print universal-time format 25 void printStandard(); // print standard-time format private: 28 int hour; // (24-hour clock format) 29 int minute; // int second; // }; // end class Time... Defining member functions inside Class definition (sometimes used for small functions)

5 5 66 int main() 67 { 68 Time t; // instantiate object t of class Time t.setTime( 13, 27, 6 ); // change time return 0; } // end main Set data members using public member function. Accessing Class members object.memberfunction; OR objectPtr = &object; objectPtr->memberfunction; same as (*objectPtr ).memberfunction

6 6 8 class Time { 9 public: 10 Time( int = 0, int = 0, int = 0 ); // default constructor 11 void setTime( int, int, int ); // set time 12 void printUniversal() const; // print universal time private: 15 int hour; // (24-hour clock format) 16 int minute; // int second; // }; // end class Time void Time::setTime( int hour, int minute, int second ) 21 { setHour( hour ); 22 setMinute( minute ); 23 setSecond( second ); } // end function setTime void Time::printUniversal() const 26 { cout << setfill( '0' ) << setw( 2 ) << hour << ":" 27 << setw( 2 ) << minute << ":" 28 << setw( 2 ) << second; } // end function printUniversal int main() 31 { 32 Time wakeUp( 6, 45, 0 ); // non-constant object 33 const Time noon( 12, 0, 0 ); // constant object 34 noon.printUniversal(); // 35 return 0; 36 } // end main Constant ( const ) objects and functions Member functions for const objects must also be const const member functions cannot modify object Specify const in both prototype and definition

7 7 fig07_04.cpp (1 of 3) 10 class Increment { 11 public: 12 Increment( int c = 0, int i = 1 ); // default constructor 13 void addIncrement() 14 { 15 count += increment; } // end function addIncrement 16 void print() const; // prints count and increment 17 private: 18 int count; 19 const int increment; // const data member 20 }; // end class Increment Increment::Increment( int c, int i ) 23 : count( c ), //initializer for non-const data 24 increment( i )//required initializer for const data 25 { 26 // empty body 27 } // end Increment constructor void Increment::print() const 30 { 31 cout << ", increment = " << increment << endl; } // end function print Initializing const data members (data) Member initializer syntax Can be used for all data members Must be used for const data members Member initializer list separated from parameter list by colon. Member initializer syntax can be used for non- const data member count. Member initializer syntax must be used for const data member increment. Member initializer consists of data member name ( increment ) followed by parentheses containing initial value ( i ). Note that the member initializer list is separated by commas, and the list executes before the body of the constructor.

8 8 20 class Count { 21 friend void setX( Count &, int ); // friend declaration 22 public: 23 void print() const 24 { 25 cout << x << endl; } // end function print private: 30 int x; // data member 31 }; // end class Count void setX( Count &c, int val ) 34 { 35 c.x = val; // legal: setX is a friend of Count 36 } // end function setX int main() 39 { 40 Count counter; // create Count object counter.print(); setX( counter, 8 ); // set x with a friend counter.print(); 47 return 0; 48 } // end main Use friend function to access and modify private data member x. setX is a function; it is not part of the class Count. Pass Count object since C-style, standalone function. friend functions function defined outside class’s scope but has right to access non-public members Precede function prototype with keyword friend is class definition c is an object with data member x.

9 9 10 class Employee { 11 public: 12 const char *getLastName() const; 13 static int getCount(); private: 16 char *firstName; 17 char *lastName; 18 static int count; 19 }; int Employee::count = 0; int Employee::getCount() 24 { 25 return count; 26 } int main() 29 { 30 cout << Employee::getCount() << endl; return 0; } // end main static member function can only access static data members and member functions. Initialize static data member exactly once at file scope. static member function accesses static data member count. static members static data is “Class-wide” data; property of class, not specific object of class private static variables Can only be accessed via public static member function class::memberfunction public static variables class::object

10 10 8 class Test { 9 10 public: 11 void print() const; private: 14 int x; }; // end class Test void Test::print() const 19 { 20 // implicitly use this pointer to access member x 21 cout << " x = " << x; // explicitly use this pointer to access member x 24 cout x = " x; // explicitly use dereferenced this pointer and 27 // the dot operator to access member x 28 cout << "\n(*this).x = " << ( *this ).x << endl; } // end function print int main() 33 { 34 Test testObject( 12 ); 35 testObject.print(); 36 return 0; 37 } The this pointer Allows object to access own address Implicit argument to non-static member function call Implicitly reference member data and functions


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer 2003 1 Summary of Topics Related to Classes Class definition Defining member."

Similar presentations


Ads by Google