CS 596 OODP
Introduction to C++
[To Lecture Notes Index]
San Diego State University -- This page last updated August 21, 1995

Contents of Introduction to C++ Lecture
- Intro To C++
- Compatibility with C
- Simple Differences Between C and C++
- Variables
- Constants
- Parameter Passing
Most C programs must be modified to run as C++ programs
Differences between C and C++
- C++ has stronger type-checking
-
- C++ has defined constants
-
- Must specify function parameters in C++
-
- Can provide default parameter values in C++
-
- Can overload function names in C++
-
- C++ has pass-by-reference (var parameters in Pascal)
-
- new and delete replace malloc()
-
- Variables can be declared anywhere in the program
-
- New comment delimiter //
-
- Inline functions remove overhead of function call
Which C++?
AT&T C++ Versions 1 - 3
ANSI Standard C++ (1996)
Langauge features not always implemented
- Templates
-
- Exception handling
Which Main?
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;
}
Important Variable Terms
rvalue ( value )
- value of a variable
- stored at some location
lvalue ( location, address )
- address where rvalue of variable is stored
- X = X + 2;
#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;
}
Use const instead of #define
Const allows type checking
Const Issues
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
- pc can be changed to an address of a different variable
- The value of the object addressed by pc cannot be modified through pc
Const Issues - ContinuedPointer to Constant
const char* pc = "hi mom";
pc[3] = 'a'; // compile error
pc = "hi dad" // ok
Constant Pointer
char *const cp = "Hi Dad";
cp[3] = 'a'; // ok
cp = "Send Money"; // compile error
Constant Pointer to a Constant
const char *const cpc = "Cat"; //
cp[3] = 'a'; // compile error
cp = "Send Money"; // compile error
To Const or not to Const?
Const parameters can not be modified
void PrintScores( const int courseGrades[100] )
{
courseGrades[10] = 4; // compile error
}
No cheating allowed
void VerifyScores( int suspectGrades[100] )
{
suspectGrades[10] = 4;
}
void SafePrintScores( const int courseGrades[100] )
{
VerifyScores( courseGrades ); // compile error
}
- Use const parameters when ever possible or never use them
- Do not change your mind in the middle of a project about using const
Modular Design Rules
Const supports Protection
Measure Twice, Cut Once
Object-Oriented programming requires more early planning then procedural
programming
Supports:
Decomposability Composability Protection Continuity Understandability
Default parameter passing is pass-by-value
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;
}
Pass
by Reference
What does it do?
void test( int& parameter ) {
int local;
local = parameter * 2;
parameter = local + 3;
}
int main() {
int x = 10;
test( x );
return 0;
}
The above program will operate as:
void test( int* parameter ) {
int local;
local = *parameter * 2;
*parameter = local + 3;
}
int main() {
int x = 10;
test( &x );
return 0;
}
Modular Design Rule
Managers Rule: Delegate
Computers are good with detail
People are not
Whenever possible let the computer do it
Return-by-value
int ReturnACopy() {
int local = 1;
return local;
}
int main() {
int Safe;
Safe = ReturnACopy() + 2;
return 0;
}
Return-by-reference
int& ReturnTheOrignal() {
int local = 1;
return local;
}
int main() {
int NotSafe;
NotSafe = ReturnTheOrignal() + 2;
cout << NotSafe;
return 0;
}
Output could be garbage!
Return-by-reference
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
}