· The const Parameter Modifier
o
If you do not want a programmer to change the value of a parameter, use
the const modifier before the
parameter name.
o
The const Parameter modifier is
usually used with Call-By-Reference (CBR) parameters for classes.
o
The const Parameter modifier is also
good to use for very large parameters because it is more efficient than
Call-By-Value (CBV).
§ Remember, a CBV parameter is
a local variable that is a copy of the function argument (thus, two copies are
exist).
§ With CBR, the parameter is a
placeholder that is replaced by the actual argument (thus, there is only one
copy that exists).
// Example
// Declaration of a function to
determine an
// elder person. Parameters are const
CBR.
bool isOlder(const Person& P1, const Person& P2);
// Definition of a function to
determine an
// elder person. Parameters are const CBR.
bool isOlder(const Person& P1, const Person& P2)
{
return (P1.getAge() > P2.getAge());
}
// Snippet of code from main()
Person A, B;
// Get all applicable information
A.getInformation();
B.getInformation();
// Check to see which person is older.
if(isOlder(A,
B))
{
A.displayName();
cout << “ is older.\n”;
}
else
{
B.displayName();
cout << “ is older.\n”;
}
// A snippet of the class definition
for Person.
class
Person
{
public:
…
void getInformation();
int getAge()
const;
void displayName() const;
…
private:
…
string _fullName;
int _age;
…
};
// Implementation of the accessor
function
// getAge(). Note: the use of the const
// modifier.
int Person::getAge() const
{
return _age;
}
// Implementation of the member
function
// displayName(). Note: the use of the const
// modifier.
void Person::displayName() const
{
cout << _fullName;
}
// Summary of compiler results for Parameter types and
// argument types that are passed to a function that
// attempts to change the formal parameter.
|
Parameter and Argument |
What Function Does |
Compilation Result |
|
CBV Parameter and argument passed is declared as const |
Function tries to change Parameter |
No Error. Copy made to the formal parameter; hence, no change to declared const argument. |
|
CBV Parameter and argument passed is not declared as const |
Function tries to change Parameter |
No Error. There is nothing declared as const (parameter or argument). |
|
CBR Parameter and argument passed is declared as const |
Function tries to change Parameter |
Error. The Parameter is a placeholder for a const argument. |
|
CBR Parameter and argument passed is not declared as const |
Function tries to change Parameter |
No Error. There is nothing declared as const (parameter or argument). |
|
const CBR Parameter and argument passed is declared as const |
Function tries to change Parameter |
Error. Formal parameter is a const parameter |
|
const CBR Parameter and argument passed is not declared as const |
Function tries to change Parameter |
Error. Formal parameter is a const parameter |
· Inline functions
o
An inline function is a function that is preceded by the keyword inline. It
is treated special by the compiler in that the code is inserted at each
location where the function is invoked.
o
An inline function is also a function whose full definition is given
within the definition of a class. It
does not need to be preceded by the keyword inline. It is treated in the same manner by the
compiler.
o
An advantage of an inline function for smaller functions is that the
code is more efficient.
o
A disadvantage of an inline function for very large functions is that
the code is less efficient.
o
Another disadvantage is that it mixes the interface and implementation
of a class. This goes against the
principal of encapsulation.
// Example
#include <iostream>
using
namespace std;
class
Person
{
public:
void setAge(int
age) { _age = age; }
int getAge()
const { return _age; }
private:
int _age;
};
int
main()
{
Person A;
A.setAge(50);
cout << "The Age is
" << A.getAge() << endl;
return 0;
}
· Static Members of a class
o
Static variables are variables that are declared static within a class definition. The variable may be shared by all objects of
the class.
§ Static variables are
initialized outside of the class definition.
The keyword static is not used in the
initialization; only in the class definition.
§ Static variables may be
initialized only once.
o
Static functions are functions that are declared static within a class definition that are usually
used to access static variables of the class.
§ Static functions are usually
defined (implemented) outside of the class definition as most member functions
are. The keyword static is not used in the initialization; only in
the declaration (prototype) that is within the class definition.
// Example
#include
<iostream>
using namespace std;
class SomeObject
{
public:
SomeObject() { objectCount++; }
~SomeObject() { objectCount--; }
static int getObjectCount();
void setNumber(int number) { _number =
number; }
int
getNumber() const { return _number; }
private:
static int objectCount;
int
_number;
};
int SomeObject::objectCount
= 0;
int SomeObject::getObjectCount()
{
return objectCount;
}
int main()
{
SomeObject A, B, C, D, E, F, G, H, *I;
D.setNumber(200);
cout
<< "Object D number is set to " << D.getNumber()
<< endl;
cout
<< "Total number of objects declared is "
<< SomeObject::getObjectCount() << endl;
I = new SomeObject;
cout
<< "Total number of objects declared is "
<< SomeObject::getObjectCount() << endl;
delete I;
cout
<< "Total number of objects declared is "
<< SomeObject::getObjectCount() << endl;
return 0;
}
//
Output
Object D number is set to 200
Total number of objects declared is 8
Total number of objects declared is 9
Total number of objects declared is 8
· Vectors
o
Vectors serve the same purpose as arrays except that they can change
length while the program is running.
o
Vectors are formed from a template class in the Standard Template
Library (STL).
o
Vector elements are indexed starting at zero. Square brackets may be used to access the
elements provided the element has memory allocated for it.
o
A Vector has a base type and stores a collection of values of its base
type.
//Example
#include <iostream>
#include
<vector>
using namespace std;
int
main()
{
vector<int>
a, b(5), c;
int
i;
// Vector ‘a’ must be initialized using push_back();
a.push_back(4);
a.push_back(3);
a.push_back(2);
a.push_back(1);
a.push_back(5000);
// We can use []
on ‘b’, but not on ‘a’ or a runtime error will
//
occur.
for(i = 0; i < 5; i++)
b[i] = i;
// We can add to ‘b’ further using push_back();
b.push_back(10000);
// We can set
vector ‘c’
c = a;
cout
<< "The Current Arrays:\n\n";
cout
<< "The A vector:\n";
for(i = 0; i < a.size();
i++)
cout << a[i] <<
endl;
cout
<< "The B vector:\n";
for(i = 0; i < b.size();
i++)
cout << b[i] <<
endl;
cout
<< "The C vector:\n";
for(i = 0; i < c.size();
i++)
cout << c[i] <<
endl;
return 0;
}
//
Output
The Current Arrays:
The A vector:
4
3
2
1
5000
The B vector:
0
1
2
3
4
10000
The C vector:
4
3
2
1
5000
// Some useful member functions:
|
Member Function |
Description |
|
push_back(arg) |
Adds an element that is the same type as arg. Starts at position 0, then 1, 2, 3, etc … |
|
size() |
Returns the number of vector elements. |
|
capacity() |
Returns the amount of capacity allocated for the vector elements. This is greater than or equal to size(). |
|
reserve(number) |
Allows you to set the capacity of the vector to number elements. This function cannot decrease the size of the vector; only increase. |
|
resize(number) |
Allows you to resize the vector to number elements. If the current size is less than number, new elements are added and initialized according to the appropriate constructor. If the current size is greater than number, the elements that are higher than number are removed. |