1 / 10

Summary of Topics Related to Classes

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

jaimin
Télécharger la présentation

Summary of Topics Related to Classes

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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; // 0 - 23 (24-hour clock format) 11 int minute; // 0 - 59 12 int second; // 0 - 59 13 14 }; // end class Time Definition of class begins with keyword class. Class body starts with left brace. Function prototypes for public member functions. private data members accessible only to member functions. Definition terminates with semicolon. Class body ends with right brace.

  3. 14 class Time { 15 16 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 21 22 private: 23 int hour; // 0 - 23 (24-hour clock format) 24 int minute; // 0 - 59 25 int second; // 0 - 59 26 27 }; // end class Time 28 29 Time::Time() 30 { 31 hour = minute = second = 0; 32 33 } // end Time constructor 34 35 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; 40 41 } // end function setTime . . . Defining member functions outside class definitionReturnType ClassName::MemberFunctionName( ){ …}Each class must have a constructor function (same name as class): initializes data members of a class object when called

  4. 14 class Time { 15 16 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 23 24 void printUniversal(); // print universal-time format 25 void printStandard(); // print standard-time format 26 27 private: 28 int hour; // 0 - 23 (24-hour clock format) 29 int minute; // 0 - 59 30 int second; // 0 - 59 31 32 }; // end class Time . . . Defining member functions inside Class definition (sometimes used for small functions)

  5. 66 int main() 67 { 68 Time t; // instantiate object t of class Time 69 70 t.setTime( 13, 27, 6 ); // change time 71 72 return0; 73 74 } // end main Accessing Class membersobject.memberfunction;ORobjectPtr = &object;objectPtr->memberfunction;same as(*objectPtr ).memberfunction Set data members using public member function.

  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 13 14 private: 15 int hour; // 0 - 23 (24-hour clock format) 16 int minute; // 0 - 59 17 int second; // 0 - 59 18 }; // end class Time 19 20 void Time::setTime( int hour, int minute, int second ) 21 { setHour( hour ); 22 setMinute( minute ); 23 setSecond( second );} // end function setTime 24 25 void Time::printUniversal() const 26 { cout << setfill( '0' ) << setw( 2 ) << hour << ":" 27 << setw( 2 ) << minute << ":" 28 << setw( 2 ) << second;} // end function printUniversal 29 30 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 return0; 36 } // end main Constant (const) objects and functionsMember functions for const objectsmust also be constconst member functions cannot modify object Specify const in both prototype and definition

  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 21 22 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 28 29 void Increment::print() const 30 { 31 cout << ", increment = " << increment << endl; 32 33 } // end function print Member initializer list separated from parameter list by colon. Member initializer syntax can be used for non-const data member count. Initializing const data members (data) Member initializer syntaxCan be used for all data membersMust be used for const data members 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. 20 class Count { 21 friend void setX( Count &, int ); // friend declaration 22 public: 23 void print() const 24 { 25 cout << x << endl; 26 27 } // end function print 28 29 private: 30 int x; // data member 31 }; // end class Count 32 33 void setX( Count &c, int val ) 34 { 35 c.x = val; // legal: setX is a friend of Count 36 } // end function setX 37 38 int main() 39 { 40 Count counter; // create Count object 41 42 counter.print(); 43 44 setX( counter, 8 ); // set x with a friend 45 46 counter.print(); 47 return0; 48 } // end main friend functionsfunction defined outside class’s scope but has right to access non-public membersPrecede function prototype with keyword friend is class definition c is an object with data member x. setX is a function; it is not part of the class Count. Pass Count object since C-style, standalone function. Use friend function to access and modify private data member x.

  9. 10 class Employee { 11 public: 12 const char *getLastName() const; 13 static int getCount(); 14 15 private: 16 char *firstName; 17 char *lastName; 18 static int count; 19 }; 20 21int Employee::count = 0; 22 23int Employee::getCount() 24{ 25 return count; 26} 27 28 int main() 29 { 30 cout << Employee::getCount() << endl; 31 32 return0; 33 34 } // end main static membersstatic data is “Class-wide” data; property of class, not specific object of classprivatestatic variablesCan only be accessed via publicstatic member function class::memberfunctionpublicstatic variablesclass::object 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.

  10. 8 class Test { 9 10 public: 11 void print() const; 12 13 private: 14 int x; 15 16 }; // end class Test 17 18 void Test::print() const 19 { 20 // implicitly use this pointer to access member x 21 cout << " x = " << x; 22 23 // explicitly use this pointer to access member x 24 cout << "\n this->x = " << this->x; 25 26 // explicitly use dereferenced this pointer and 27 // the dot operator to access member x 28 cout << "\n(*this).x = " << ( *this ).x << endl; 29 30 } // end function print 31 32 int main() 33 { 34 Test testObject( 12 ); 35 testObject.print(); 36 return0; 37 } The this pointerAllows object to access own addressImplicit argument to non-static member function callImplicitly reference member data and functions

More Related