Docsity

Understanding Const Objects and Const Member Functions in C++, Slides of Programming for Engineers

Partial preview of the text

Download Understanding Const Objects and Const Member Functions in C++ and more Slides Programming for Engineers in PDF only on Docsity!

Chapter 10: Classes Part II

7.1 Introduction

  • Classes
  • Data abstraction
  • Object-based programming (OBP)
    • Chapters 9-
  • Inheritance and polymorphism
    • Chapters 12 and 13

7.2 const (Constant) Objects

  • const^ and^ member functionsconst^ Member Functions
    • Member functions for const objects must also be const - Cannot modify object
    • Specify const in both prototype and definition
      • Prototype
        • After parameter list
      • Definition
        • Before beginning left brace

7.2 const (Constant) Objects and const Member Functions

  • Constructors and destructors
    • Cannot be const
    • Must be able to modify objects
      • Constructor
        • Initializes objects
      • Destructor
        • Performs termination housekeeping

time5.h (2 of 2)

27 26 private: 28 int hour; 29 // 0 - 23 (24-hour clock format)int minute; // 0 - 59 30 int second; // 0 - 59 31 32 }; // end class Time 33 34 #endif

time5.cpp (1 of 4)

2 // Member-function definitions for class Time.^1 // Fig. 7.2: time5.cpp 3 #include 4 5 using std::cout; 6 7 #include 8 910 using std::setfill;using std::setw; 12 // include definition of class Time from time5.h^11 13 #include "time5.h" 14

1615 // calls member function setTime to set variables;// constructor function to initialize private data; 1817 Time::Time( int hour, int minute, int second )// default values are 0 (see class definition) 20 setTime( hour, minute, second );^19 { 22 } // end Time constructor^21 23

time5.cpp (3 of 4)

48 47 void Time::setSecond( int s )^ // set second value 50 second = ( s >= 0 && s < 60 )? s : 0;^49 { 52 } // end function setSecond^51 54 // return hour value^53 55 int Time::getHour() const 56 { 57 return hour; 58 59 } // end function getHour 60 6261 int Time::getMinute() const// return minute value 64 return minute;^63 { 66 } // end function getMinute^65 67

10

const functions do not modify objects.

time5.cpp (4 of 4)

6968 int Time::getSecond() const// return second value 71 return second;^70 { 73 } // end function getSecond^72 75 // print Time in universal format^74 76 void Time::printUniversal() const 77 { 78 79 cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) << minute << ":" 80 << setw( 2 ) << second; 81 82 } // end function printUniversal 83 85 void Time::printStandard() // note lack of const declaration^84 // print Time in standard format 87 cout << ( ( hour == 0 || hour == 12 )? 12 : hour % 12 )^86 { 88 89 << ":" << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) << second 90 << ( hour < 12? " AM" : " PM" ); 91^11

const functions do not modify objects.

fig07_03.cpp

(2 of 2)

fig07_03.cpp

output (1 of 1)

13 // OBJECT

MEMBER FUNCTION

14 wakeUp.setHour( 18 ); // non- const non-const 15 16 noon.setHour( 12 ); // const non-const 17 18 wakeUp.getHour(); // non- const const 19 20 noon.getMinute(); // const (^) 13

d:\cpphtp4_examples\ch07\fig07_01\fig07_01.cpp(16)'setHour' : cannot convert 'this' pointer from 'const : error class C2662: Time' to 'class Conversion Time &' loses qualifiers d:\cpphtp4_examples\ch07\fig07_01\fig07_01.cpp(23)'printStandard' : cannot convert 'this' pointer : errorfrom 'const C2662: class Time' (^) Conversionto 'class Timeloses &' qualifiers

Attempting to invoke non- const member function on const object results in compiler error.

Attempting to invoke non- const member function on const object results in compiler error even if function does not modify object.

7.2 const (Constant)

Objects and const Member

Functions

  • Member initializer syntax
    • Initializing with member initializer syntax
      • Can be used for
        • All data members
      • Must be used for
        • const data members
        • Data members that are references

fig07_04.cpp

(2 of 3)

22 private: 23 int count; 24 const int increment; // const data member 25 26 }; // end class Increment 27 28 // constructor 29 Increment::Increment( int c, int i )

30 : count( c ), // initializer for non- const member 31 increment( i ) // required^16

Declare increment as const data member.

Member initializer list separated from parameter list by colon. Member initializer syntax canbe used for non- const data member^ Member initializer syntaxmust be used for count. const data member increment. Member initializer consists of data member name ( increment ) followed by parentheses containing initial value ( c ).

fig07_04.cpp

(3 of 3)

fig07_04.cpp

output (1 of 1)

45 int main() 46 { 47 Increment value( 10, 5 ); 48 49 cout << "Before incrementing: "; 50 value.print(); 51 52 for ( int j = 0; j < 3; j++ ) { 53 value.addIncrement(); 54 cout << "After increment " << j + 1 << ": "; 55 value.print();^17

Before After incrementincrementing: count 1: count = =15, 10, increment increment = = 5 5 After After incrementincrement 2:3: countcount == 20,25, incrementincrement == (^55)

docsity.com

fig07_05.cpp

(2 of 3)

22 private: 23 int count; 24 const int increment; // const data member 25 26 }; // end class Increment 27 28 // constructor 29 Increment::Increment( int c, int i ) 30 { // Constant member 'increment' is not initialized

31 count = c; // allowed because^19

Declare increment as const Attempting to modify^ data member. const data member increment results in error.

fig07_05.cpp

(3 of 3)

fig07_05.cpp

output (1 of 1)

44 int main() 45 { 46 Increment value( 10, 5 ); 47 48 cout << "Before incrementing: "; 49 value.print(); 50 51 for ( int j = 0; j < 3; j++ ) { 52 value.addIncrement(); 53 cout << "After increment " << j + 1 << ": "; 54 value.print();^20

D:\cpphtp4_examples\ch07\Fig07_03\Fig07_03.cpp(30)'increment' : must be initialized in constructor : error base/member C2758: initializer D:\cpphtp4_examples\ch07\Fig07_03\Fig07_03.cpp(24) list :

D:\cpphtp4_examples\ch07\Fig07_03\Fig07_03.cpp(32)see^ declaration of^ 'increment' : error C2166: l-value specifies const object

Not using member initializersyntax to initialize const data member increment results in error. Attempting to modify const data member increment results in error.

docsity.com