summary of topics related to classes

10
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 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

Upload: jaimin

Post on 04-Jan-2016

31 views

Category:

Documents


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Summary of Topics Related to Classes

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

Page 2: Summary of Topics Related to Classes

2

Basic Class definition example

1 class Time {2 3 public:4 Time(); // constructor5 void setTime( int, int, int ); // set hour, minute, second6 void printUniversal(); // print universal-time format7 void printStandard(); // print standard-time format8 9 private:10 int hour; // 0 - 23 (24-hour clock format)11 int minute; // 0 - 5912 int second; // 0 - 5913 14 }; // 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.

Page 3: Summary of Topics Related to Classes

314 class Time { 15 16 public: 17 Time(); // constructor 18 void setTime( int, int, int ); // set hour, minute, second 19 void printUniversal(); // print universal-time format20 void printStandard(); // print standard-time format 2122 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 definition

ReturnType ClassName::MemberFunctionName( ){

…}

Each class must have a constructor function (same name as class): initializes data members of a class object when called

Page 4: Summary of Topics Related to Classes

414 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 format25 void printStandard(); // print standard-time format 2627 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)

Page 5: Summary of Topics Related to Classes

566 int main()67 {68 Time t; // instantiate object t of class Time69 70 t.setTime( 13, 27, 6 ); // change time71 72 return 0; 73 74 } // end main

Set data members using public member function.

Accessing Class members

object.memberfunction; ORobjectPtr = &object;objectPtr->memberfunction;

same as (*objectPtr ).memberfunction

Page 6: Summary of Topics Related to Classes

68 class Time { 9 public:10 Time( int = 0, int = 0, int = 0 ); // default constructor 11 void setTime( int, int, int ); // set time12 void printUniversal() const; // print universal time13 14 private:15 int hour; // 0 - 23 (24-hour clock format)16 int minute; // 0 - 5917 int second; // 0 - 59 18 }; // end class Time1920 void Time::setTime( int hour, int minute, int second )21 { setHour( hour );22 setMinute( minute );23 setSecond( second ); } // end function setTime2425 void Time::printUniversal() const26 { cout << setfill( '0' ) << setw( 2 ) << hour << ":"27 << setw( 2 ) << minute << ":"28 << setw( 2 ) << second; } // end function printUniversal2930 int main()31 {32 Time wakeUp( 6, 45, 0 ); // non-constant object33 const Time noon( 12, 0, 0 ); // constant object34 noon.printUniversal(); // 35 return 0;36 } // end main

Constant (const) objects and functions

Member functions for const objectsmust also be const

const member functions cannot modify object

Specify const in both prototype and definition

Page 7: Summary of Topics Related to Classes

7

fig07_04.cpp(1 of 3)

10 class Increment { 11 public:12 Increment( int c = 0, int i = 1 ); // default constructor13 void addIncrement() 14 { 15 count += increment; } // end function addIncrement16 void print() const; // prints count and increment17 private:18 int count;19 const int increment; // const data member 20 }; // end class Increment21 22 Increment::Increment( int c, int i )23 : count( c ), //initializer for non-const data 24 increment( i )//required initializer for const data25 { 26 // empty body27 } // end Increment constructor28 29 void Increment::print() const30 {31 cout << ", increment = " << increment << endl;32 33 } // 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.

Page 8: Summary of Topics Related to Classes

820 class Count {21 friend void setX( Count &, int ); // friend declaration22 public:23 void print() const 24 { 25 cout << x << endl; 26 27 } // end function print28 29 private:30 int x; // data member 31 }; // end class Count32 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 object41 42 counter.print();43 44 setX( counter, 8 ); // set x with a friend45 46 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.

Page 9: Summary of Topics Related to Classes

910 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 21 int Employee::count = 0; 22 23 int Employee::getCount() 24 { 25 return count;

26 }27 28 int main()29 {30 cout << Employee::getCount() << endl;31 32 return 0;33 34 } // 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 variablesCan only be accessed via public static member function class::memberfunction

public static variables class::object

Page 10: Summary of Topics Related to Classes

108 class Test {9 10 public:11 void print() const;12 13 private:14 int x;15 16 }; // end class Test17 18 void Test::print() const 19 {20 // implicitly use this pointer to access member x21 cout << " x = " << x; 22 23 // explicitly use this pointer to access member x24 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 print31 32 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