Professional Documents
Culture Documents
C++ Features Constant Arguments
C++ Features Constant Arguments
Constant Arguments
The keyword const is added to the beginning of an argument declaration to prevent the
argument from being modified within the called function.
• This will also prevent temporary modification of the argument.
void printValue( const int value ) { int updateValue( const int oldValue ) {
cout << value << endl; int value;
return; value = oldValue * 3;
} return value
// lower camel case naming }
int calculateSum( const int intArray[], const int size ) // Definition header
oldStack uses Pass-by-Reference so that the copy constructor is not called twice.
• Otherwise, the copy constructor is called once to create a temporary copy of oldStack
that is placed on the runtime stack and once to create the new Stack class instance.
Including Modules/Libraries in C++
Modules/libraries are used in C++ to declare and define Abstract Data Types. A module
in C++ consists of:
• a header file (.h)
• a implementation file (.cpp)
When adding a module to an application only include the header file and never include
the implementation file.
Both the driver/main file and the implementation file will include the header file only.
• This will provide them both with the required declarations and definitions.
1. Manager
a. Responsible for the creation and destruction of a class instance.
b. Examples: constructors and destructors
2. Accessor
a. Responsible for retrieving the state information of a class instance without
modifying its state.
b. These functions are commonly referred to as “getter” functions.
c. Examples: getValue, empty, full, print, peek
d. These function are generally constant functions
3. Mutator or Modifier
a. Responsible for altering the state information of a class instance.
b. These functions are commonly referred to as “setter” functions
c. Examples: setValue, push, pop
// implementation file
Class1::Class1( ) { Class1::~Class1( ) {
} }
int Class1::getValue( ) const { void Class1::setValue( const int oldValue ) {
return value; value = oldValue;
} return;
// upper camel case naming – MyClass }
Class Instantiation in C++
When a new class instance is created or a new class object is instantiated there are three
things that will automatically occur:
1. Storage allocation
2. Initialization using Base/Member initialization list
3. Assignment using the body of the constructor
The “this” pointer is an explicit reference to the class instance that called the active class
function.