Page 265 - DCAP404 _Object Oriented Programming
P. 265
Object-oriented Programming
Notes // Function declaration use references and without default values.
void swap(float& first_indentifier, float& second_indentifier);
// Main program
int main(void)
{
int p = 3; // Output numeric precision.
float a = 2.5f; // Units and description of a (f indicates float
value).
float b = 7.5f; // Units and description of b.
ofstream fout; // Declare an output file object.
fout.open(“swap.out”, ios::out); // Open the output file.
if (!fout) // See if the file was opened successfully.
{
cout << “Can’t open output file!\n”;
return 1;
}
fout.setf(ios::showpoint); // Show decimal points.
fout << setprecision(p); // Set the precision.
fout << “Before swapping...\n”;
fout << “a = “ << a << “ and b = “ << b << endl;
swap(a, b);
fout << “After swapping...\n”;
fout << “a = “ << a << “ and b = “ << b << endl;
fout.close(); // Close the out file.
return 0;
}
// Define the swap function. Use references so argument changes are
returned.
void swap(float& x, float& y)
{
float hold;
hold = x;
x = y;
y = hold;
return;
}
258 LOVELY PROFESSIONAL UNIVERSITY