Return to the C++ Demos Home Page
SAMS Teach Yourself C++ in 21 Days
Chapter Nine
Prog 1 : Using References instead of pointers
// STYC++in21 Chapter 9
//
// references to class objects
#include <iostream.h>
class SimpleCat
{
public:
SimpleCat(int age, int weight);
~SimpleCat() {}
int GetAge() { return itsAge; }
int GetWeight() { return itsWeight; }
private:
int itsAge;
int itsWeight;
};
SimpleCat::SimpleCat(int Age, int Weight)
{
itsAge = Age;
itsWeight = Weight;
}
int main()
{
SimpleCat Lars(5,8);
SimpleCat &rLars = Lars;
cout << "Lars is : ";
cout << Lars.GetAge() << " years old.";
cout << " And Lars weighs... ";
cout << rLars.GetWeight() << " \n";
return 0;
}
Prog 2 : Passing params into a function with Pointers
// STYC++in21 Chapter 9 prog 2
//
// Passing a parameter by reference w/ pointers
//
#include <iostream.h>
void swap(int *x, int *y);
int main()
{
int x = 5, y = 10;
cout << "Before Swap : x = " << x << " and y = " << y << "\n\n";
swap( &x, &y);
cout << "After Swap : x = " << x << " and y = " << y << "\n\n";
return 0;
}
void swap(int *px, int *py)
{
int temp;
cout << " Before: In Swap.... *px = " << *px << " and *py = " << *py << "\n";
temp = *px;
*px = *py;
*py = temp;
cout << " After: In Swap.... *px = " << *px << " and *py = " << *py << "\n\n";
return;
}
Prog 3 : Same thing, only this time using References
// STYC++in21 Chapter 9 prog 3
//
// Passing a parameter by reference w/ references
//
#include <iostream.h>
void swap(int &x, int &y);
int main()
{
int x = 5, y = 10;
cout << "Before Swap : x = " << x << " and y = " << y << "\n\n";
swap( x, y );
cout << "After Swap : x = " << x << " and y = " << y << "\n\n";
return 0;
}
void swap(int &rx, int &ry)
{
int temp;
cout << " Before: In Swap.... rx = " << rx << " and ry = " << ry << "\n";
temp = rx;
rx = ry;
ry = temp;
cout << " After: In Swap.... rx = " << rx << " and ry = " << ry << "\n\n";
return;
}
Prog 4 : Returning more than the return value from a function. Passing Pointers.
// STYC++in21 chapter 9 prog 4
//
// Returning more than one value from a function using pointers.
// Use the return value as an error checker.....
//
#include <iostream.h>
int Factor( int n, int *pSquared, int *pCubed);
int main()
{
int number=0, squared, cubed;
int error;
while (number != 99 )
{
cout << "Enter a number (1-20) : ";
cin >> number;
error = Factor( number, &squared, &cubed );
if (!error)
{
cout << "The number is " << number << "\n";
cout << "The square is " << squared << "\n";
cout << "The cube is " << cubed << "\n";
}
else cout << "Please enter a valid number next time!\n";
} // end of while loop
return 0;
}// end of main loop
int Factor( int n, int *s, int *c )
{
if ( (n>0) && (n<21) )
{
*s = n*n;
*c = n*n*n;
return 0;
}
else return 1;
}
Prog 5 : Same as above with References
// STYC++in21 chapter 9 prog 4
//
// Returning more than one value from a function using references.
// Use the return value as an error checker.....
//
// I don't include enumerated error checking and typedefs
// that were in the book........
//
#include <iostream.h>
int Factor( int n, int &rSquared, int &rCubed);
int main()
{
int number=0, squared, cubed;
int error;
while (number != 99 )
{
cout << "Enter a number (1-20) : ";
cin >> number;
error = Factor( number, squared, cubed );
if (!error)
{
cout << "The number is " << number << "\n";
cout << "The square is " << squared << "\n";
cout << "The cube is " << cubed << "\n";
}
else cout << "Please enter a valid number next time!\n";
} // end of while loop
return 0;
}// end of main loop
int Factor( int n, int &s, int &c )
{
if ( (n>0) && (n<21) )
{
s = n*n;
c = n*n*n;
return 0;
}
else return 1;
}