DOC PREVIEW
UD CISC 181 - Lecture 23

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. McCoyLecture 23November 19, 2009Another Look at Classes - const• We have declared constants before – they are objects of a type that cannot be modified.• Recall –the compiler complains if you try ppyyto modify an object that is declared to be a constant.• With classes, must assure the compiler that a constant is not being modified – so need to declare some member functions to take const objects237.2 const (Constant) Objects and constMember Functions• Principle of least privilege– Only allow modification of necessary objects•Keyword const– Specify object not modifiable– Compiler error if attempt to modify const object 2003 Prentice Hall, Inc. All rights reserved.–Exampleconst Time noon( 12, 0, 0 ); • Declares const object noon of class Time• Initializes to 1247.2 const (Constant) Objects and constMember Functions• const 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 2003 Prentice Hall, Inc. All rights reserved.p• Definition– Before beginning left brace57.2 const (Constant) Objects and constMember Functions• Constructors and destructors– Cannot be const– Must be able to modify objects• Constructor– Initializes objects•Destructor 2003 Prentice Hall, Inc. All rights reserved.•Destructor– Performs termination housekeepingExample D&D Exercise 7.7Create a date class with the following capabilities:1. Output the date in multiple formats such as: ddd yyyy; mm/dd/yy, June 14, 19922. Use overloaded constructors to create Date objects initialized with dates of the formats in part (1).•~/Class/cisc181/examples6•~/Class/cisc181/examples• Driver: 22-date.cc; Header: date.h;implementation: date.cc• This particular solutions does not use const objects – but you can see that the print functions allow the date variable to be const (see next example in slides)277.2 const (Constant) Objects and constMember Functions• Member initializer syntax– Initializing with member initializer syntax• Can be used for– All data members• Must be used for–constdata members 2003 Prentice Hall, Inc. All rights reserved.constdata members– Data members that are referencesOutline8fig07_04.cpp(1 of 3)1 // Fig. 7.4: fig07_04.cpp2 // Using a member initializer to initialize a3 // constant of a built-in data type.4 #include <iostream>5 6 using std::cout;7 using std::endl;8 9 class Increment {10 11 public:12 Increment( int c = 0, int i = 1 ); // default constructor13 14 void addIncrement() 15 {  2003 Prentice Hall, Inc.All rights reserved.16 count += increment; 17 18 } // end function addIncrement19 20 void print() const; // prints count and increment21 Outline9fig07_04.cpp(2 of 3)22 private:23 int count;24 const int increment; // const data member25 26 }; // end class Increment27 28 // constructor 29 Increment::Increment( int c, int i )30 : count( c ), // initializer for non-const member 31 increment( i ) // required initializer for const member32 { 33 // empty body34 35 } // end Increment constructor36 Declare increment as constdata member.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  2003 Prentice Hall, Inc.All rights reserved.37 // print count and increment values38 void Increment::print() const39 {40 cout << "count = " << count41 << ", increment = " << increment << endl;42 43 } // end function print44 (increment) followed by parentheses containing initial value (c).Outline10fig07_04.cpp(3 of 3)fig07_04.cppoutput (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();56 }57 58 return 0;59  2003 Prentice Hall, Inc.All rights reserved.60 } // end mainBefore incrementing: count = 10, increment = 5After increment 1: count = 15, increment = 5After increment 2: count = 20, increment = 5After increment 3: count = 25, increment = 5Outline11fig07_05.cpp(1 of 3)1 // Fig. 7.5: fig07_05.cpp2 // Attempting to initialize a constant of3 // a built-in data type with an assignment.4 #include <iostream>5 6 using std::cout;7 using std::endl;8 9 class Increment {10 11 public:12 Increment( int c = 0, int i = 1 ); // default constructor13 14 void addIncrement() 15 {  2003 Prentice Hall, Inc.All rights reserved.16 count += increment; 17 18 } // end function addIncrement19 20 void print() const; // prints count and increment21 Outline12fig07_05.cpp(2 of 3)22 private:23 int count;24 const int increment; // const data member25 26 }; // end class Increment27 28 // constructor 29 Increment::Increment( int c, int i ) 30 { // Constant member 'increment' is not initialized31 count = c; // allowed because count is not constant32 increment = i; // ERROR: Cannot modify a const object 33 34 } // end Increment constructor 35 36 // print count and increment valuesDeclare increment as constdata member.Attempting to modify constdata member incrementresults in error. 2003 Prentice Hall, Inc.All rights reserved.37 void Increment::print() const38 {39 cout << "count = " << count40 << ", increment = " << increment << endl;41 42 } // end function print433Outline13fig07_05.cpp(3 of 3)fig07_05.cppoutput (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();55 }56 57 return 0;58 Not using member initializer syntax to initialize const 2003 Prentice Hall, Inc.All rights reserved.59 } // end


View Full Document
Download Lecture 23
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 23 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 23 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?