chap3 - 1 Chapter 3 More About Classes 2 The const...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 3 More About Classes 2 The const Specifier When added to the end of a function heading, it tells the compiler that no changes should be made to any private members during the execution of that function The use of const specifiers aids in debugging programs: the compiler will catch the error if ( numDependents = 3 ) 3 The const Specifier (cont.) const can also be used for parameters Objects, or parameters that can be objects (DataType parameters) are often passed by reference for speed in pass by value, it can take a long time to copy in pass by reference, only the address is copied The use of const here specifies that the parameter should not change called passing by const reference 4 Rules for Passing Objects Pass objects by value when the function will change them and you dont want the change to be reflected to the caller Pass objects by reference when you want changes to be reflected to the caller Pass objects by const reference for speed when objects wont be changed the compiler will catch mistaken changes 5 Constructors Constructors are special class functions that have unusual features: No return type Name must be the same as the class name Called automatically when an object is declared Constructors are commonly used to initialize data members 6 Constructors (cont.) More than one constructor can be written for a class; constructors differ by the number and types of parameters A constructor with no parameters is called a default constructor If a constructor is not written by the programmer, the compiler supplies a default constructor which does nothing 7 1 template <class DataType> 2 class Checkbook 3 { 4 public: 5 Checkbook( ); 6 Checkbook( float initBalance ); 7 void setBalance( float amount ); 8 bool writeCheck( const DataType & amount ); 9 void deposit( float amount ); 10 float getBalance( ) const; 11 DataType getLastCheck( ) const; 12 float getLastDeposit( ) const; 13 private: . . . Example 8 1 // checkbook.cpp -- The function definitions of the class 2 // template for the Checkbook 3 4 template <class DataType> 5 Checkbook<DataType>::Checkbook( ) 6 { 7 } 8 9 template <class DataType> 10 Checkbook<DataType>::Checkbook( float initBalance ) 11 { 12 balance = initBalance; 13 } Constructor Definitions 9 1 int main( ) 2 { 3 float bal; 4 cout << "Enter your initial balance: "; 5 cin >> bal; 6 7 Checkbook<float> cbook( bal ); 8 9 cbook.deposit( 500.00 ); ....
View Full Document

Page1 / 84

chap3 - 1 Chapter 3 More About Classes 2 The const...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online