!!! Final Review !!!
A) C++ ARRAYS
1)What gets printed?
#include <iostream.h>
void fn( int q[], int c[] );
int main ( void )
{
int i, qa[]= {1, 1, 2, 2}, count[3] ={4, 4, 4};
fn ( qa, count );
for ( i = 0 ; i < 3; ++i )
cout << count[i] << endl ;
return 0;
}
void fn( int q[], int c[] )
{
int i, j;
for ( i = 0 ; i < 4 ; ++i )
for ( j = 0; j < 3 ; ++j )
if ( q[i] != j )
c[j] -= 1;
}
2) What gets printed?
#include <iomanip.h>
#include <iostream.h>
void fn(int a1[]);
void swap(int & r1, int & r2);
int main ( void )
{
int qa[] = {7, 9, 6, 8};
fn(qa);
cin >> qa[0];
return 0;
}
void fn( int a1[] )
{
int i, j, k;
for ( i = 0 ; i < 3 ; ++i )
for ( j = i + 1; j < 4 ; ++j )
if ( a1[i] > a1[j] )
{
swap ( a1[i], a1[j] );
for ( k = 0 ; k < 4 ; ++ k )
cout << setw(3) << a1[k];
cout << endl;
for ( k = 0 ; k < 4 ; ++ k )
if ( k == j || k == i )
cout << "---";
else
cout << " ";
cout << endl;
}
}
void swap(int &r1, int &r2)
{ int tmp; tmp = r1; r1 = r2; r2 = tmp; }
B) C++ CLASS
1) What gets printed?
#include <iostream.h>
class Time
{
int hr;
public:
void get_time(int h){ hr = h; }
void diff(Time d){ cout << hr - d.hr << endl; }
};
int main ( void )
{
Time dawn, now;
dawn.get_time(6);
now.get_time(13);
now.diff(dawn);
return 0;
}
C) C++ CONSTRUCTORS
1) Use the program above to answer the following questions.
a) Which is/are the: no argument constructor(s)? ("default constructor")
copy constructor(s)?
b) What gets printed?
c) What happens in main(), if 2nd ctor, is changed to a default
initializer? (e.g. Book(int p = 0); )
#include <iostream.h>
class Book
{
int pg;
public:
Book(void){pg = 100;} // A
Book(int p){pg = p;} // B
Book(Book &b){pg = b.pg; cout << "hi\n";} // C
void diff(Book bb){ cout << bb.pg - pg << endl; }
};
int main ( void )
{
Book mobyDick, hamlet(300);
Book seaWolf(mobyDick);
seaWolf.diff(hamlet);
return 0;
}
D) C++ I/O STREAMS
a) If "dear_john" looks like this:
---------------------------------------------------------
DEAR JOHN
I love C++ more than YOU !!
Farewell
---------------------------------------------------------
b) What does "dear_mom" look like after program is run?
c) What gets printed on the monitor screen?
#include <fstream.h>
int main ( void )
{
char ch;
ifstream ifs("dear_john");
ofstream ofs("dear_mom");
while ( ifs.get(ch) )
{
if ( ch == 'J' || ch == 'H' )
ofs.put('M');
else
ofs.put(ch);
}
ofs << "The Real End...\n";
cout << "The End...\n";
return 0;
}
Good Luck !!