main() { // No return value needed } void main() { // No return value // Some compilers do not accept } int main() { // Return exit status to caller return 0; } int main( int argc, char* argv[] ) ) { // Return exit status to caller return 0; }
#include <iostream.h> char a = 'a'; int main() { int b = 10; b = b + 5; float c; // Legal in C++ only cout << c; // prints garbage for ( unsigned int d = 1; d > 0; d = d -1 ) cout << d; }
#define OLDSTYLE 512 /* C version */ int main() { const int BufferSize = 512; // C++ only BufferSize = 1024; // static error - can't change constant const char NewLine = '\n'; const float Pi = 3.1415; const int ScoreTable[3] = {1, 2, 3}; const int DefaultSizeArray[] = {2, 5}; return 0; }
const double pi; // error: uninitialized const const int FixedInteger = 5; // constant integer int* p = &FixedInteger ; // compile error (why?) const int* pc; // pointer to constant integer pc = &FixedInteger ; // ok int NotConstant; pc = &tNotConstant; // ok NotConstant= 10; // ok *pc = 5; // compile error
const char* pc = "hi mom"; pc[3] = 'a'; // compile error pc = "hi dad" // ok
char *const cp = "Hi Dad"; cp[3] = 'a'; // ok cp = "Send Money"; // compile error
const char *const cpc = "Cat"; // cp[3] = 'a'; // compile error cp = "Send Money"; // compile error
void PrintScores( const int courseGrades[100] ) { courseGrades[10] = 4; // compile error }
void VerifyScores( int suspectGrades[100] ) { suspectGrades[10] = 4; } void SafePrintScores( const int courseGrades[100] ) { VerifyScores( courseGrades ); // compile error }
Decomposability Composability Protection Continuity Understandability
void swap ( int v1, int v2 ) { // pass-by-value int temp = v2; v2 = v1; v1 = temp; }
void Pswap ( int *v1, int *v2 ) { // pointer pass-by-value = int temp = *v2; // pass-by-reference *v2 = *v1; *v1 = temp; }
void Rswap( int &v1, int &v2 ) { // pass-by-reference C++ only!! int temp = v2; v2 = v1; v1 = temp; } int main() { int a = 10, b = 20; swap( a,b ); // no effect from this call Pswap( &a, &b ); // this works Rswap( a, b ); // this also works return 0; }
void test( int& parameter ) { int local; local = parameter * 2; parameter = local + 3; } int main() { int x = 10; test( x ); return 0; }
void test( int* parameter ) { int local; local = *parameter * 2; *parameter = local + 3; } int main() { int x = 10; test( &x ); return 0; }
int ReturnACopy() { int local = 1; return local; } int main() { int Safe; Safe = ReturnACopy() + 2; return 0; }
int& ReturnTheOrignal() { int local = 1; return local; } int main() { int NotSafe; NotSafe = ReturnTheOrignal() + 2; cout << NotSafe; return 0; }
int& DoubleReference( int& in ) { in = in + 1; return in; } int main() { int Start = 3; int Result; Result = DoubleReference( Start ) + 2; return 0; }In the above code, the procedure call DoubleReference adds one to the variable Start, and the function call is replaced with the new result of Start. The value of Start is not copied. After the function call, the value of Start is 4.
int& WhichIsSafe( const int failingGrade ) { int local = 5; int* localPointer = new int( 8 ); if ( failingGrade < 1 ) return local; // bad news else return *localPointer; // ok }