CSCE 121 Chapter 8
« previous | Monday, September 20, 2010 | next »
Declaration vs Definition
Declaration makes function/variable available for code to use
Definition gives function/variable its value.
Things can be declared more than once, but only defined once
Why both?
Definitions "elsewhere": put in another file or later in the program
Use header files for sharing code:
Header files
Declares functions, types, constants, and other program components:
#include "std_lib_facilities.h"
Standard use:
// token.cpp
#include "token.h"
Token Token_stream::get() { /* ... */ }
// ...</code>
<code cpp>// token.h
class Token{ /* ... */ };
class Token_stream {
Token get();
// ...
};
// ...
// use.cpp
#include "Token.h"
Token_stream ts;
Token t = ts.get();
// ...
Scope
Region of program text:
- Global (outside language construct)
- Class (within a class)
- Local (between { and }
- Statement (in if, for, etc.)
Variable and function names are available only within the scope in which they are defined
Scopes can be nested
Functions
General Form:
Declaration:
return_type name(formal arguments);
Definition:
return_type name(formal arguments) body
body is a block or a try block
Call by Value
Values of parameters are copied to the new scope in the function (default)
int f(int a) { return ++a; }
int main() {
int xx = 0;
cout << f(xx) << endl; // writes 1
cout << xx << endl; // writes 0; xx was not changed by f()
int yy = 7;
cout << f(yy) << endl; // writes 8
cout << yy << endl; // writes 7; yy was not changed by f()
}
Call by Reference
The variable in the function scope is a reference or alias to the passed variable
int f(int& a) { return ++a; }
int main() {
int xx = 0;
cout << f(xx) << endl; // writes 1
cout << xx << endl; // writes 1; f() changed xx
int yy = 7;
cout << f(yy) << endl; // writes 8
cout << yy << endl; // writes 8; f() changed yy
}
Use wisely — references are not always essential
Call by const-reference
void f(int a, int& r, const int& cr) { ++a; ++r; ++cr; } // error: cr is const
void g(int a, int& r, const int& cr) { ++a; ++r; int x = cr; ++x; } // OK
int main() {
int x = 0;
int y = 0;
int z = 0;
g(x, y, z); // x==0; y==1; z==0
g(1, 2, 3); // error: reference argument r needs a variable to refer to
g(1, y, 3); // OK since cr is const we can pass "a temporary"
}
References
"reference" is a general concept (not just for calling by reference.
It can also be an alias or an alternate name for an object.
int i = 7;
int& r = i;
r = 9; // i becomes 9
const int& cr = i; // cr is now 9
// cr = 7; // error: cr refers to const
i = 8;
cout << cr << endl; // write out value of i (that's 8)
You can't modify an object through a const reference or redefine a reference refer to another object/variable after it has been initialized
Namespaces
If the same class is defined in two imported header files, compiler will throw an error.
Namespaces are named scopes
namespace My_namespace{
class My_class { /* ... */ };
}
int main() {
My_namespace::My_class v;
}
// OR
using std::cout; // cout refers to cout in the std namespace
using namespace std; // use the std namespace for all that it has
int main() {
cout << "blah";
}