CSE 10 SUMMER 1997 - FINAL EXAM: DISCUSSION EXAMPLES
I) CLASSES and CONSTRUCTORS
a) Which are inline functions? What gets printed?
---------------
#include
class Score
{
int num;
public:
Score(int n = 7){ num = n; cout << "A\n";}
Score(Score &rs){ num = rs.num + 1 ; cout << "B\n";}
inline void diff(Score, Score);
void max (Score s) { if ( num < s.num ) num++; }
};
inline void Score::diff(Score m, Score y)
{ int num = 3; cout << num << " " << m.num - y.num << endl; }
int main ( void )
{
int i;
Score you, me(8), us[2];
Score mine = you;
you.diff(mine, me);
for ( i = 0 ; i < 2; ++i )
us[i].max( me );
return 0;
}
______________________________________________________________________
II. OVERLOADED OPERATORS/this POINTER
a) Write in the overloaded operator>> function definition
b) Why do you have to WRITE an overloaded operator>> function
c) What if the overloaded operator<< function was not a friend?
d) What does "if (this == &rd) return *this; ", this and *this refer to?
---------------
#include
class Age
{
int years;
public:
Age( ) { years = 20; }
Age& operator= ( const Age& ra );
friend ostream& operator<< (ostream &os, const Age& ra)
{os << ra.years; return os;}
operator>> ( ) { }
};
Age& Age::operator= (const Age& ra)
{
if (this == &ra) return *this;
else years = ra.years; return *this;
}
int main ( void )
{
Age young, old;
cout << "Enter your age: ";
cin >> young;
old = young;
cout << old << endl;
return 0;
}
----------------------------------------------------------------------------
2) Describe these function declarations.
float fn ( int & ri );
e.g. fn is a function returning a float and being passed a reference to an int
int & fn ( float );
inline double fn ( void );
void fn ( int * );
3) Describe these member/friend function declarations.
class Age
{
int years;
public:
Age(void);
Age(int i = 0);
Age(int i);
Age(Age &ra);
Age(const Age &ra);
Age count ( void ) { years--; }
Age operator- ( const Age & );
Age & operator-= ( const Age );
friend Age minus ( int, Age & );
friend Age operator- ( int &, const Age & );
};
______________________________________________________________________
III) CHARACTER STRINGS
a) What gets printed?
----------------
extern "C"
{
#include
#include
#include
}
#include
int main ( void )
{
int i = 0, j = 0;
char name[] = "BobDole", phone[] = "456", tmp[8];
cout << name[3] << endl;
cout << strcpy( tmp, name ) << " " << atoi(phone) << endl;
cout << strcmp(tmp, name) << " " << strlen(name) << endl;
name[2] = name[2] + 2;
while ( name[i] )
{
if ( isupper( name[i] ) )
tmp[j++] = name[i];
++i;
}
tmp[j] = '\0';
cout << name << " " << tmp << endl;
return 0;
}
___________________________________________________________________
IV) POINTERS and DYNAMIC ARRAYS / STORAGE CLASSES
a) Are there compiler errors if you didn't write the Copy constructor?
b) If you didn't write the Copy constructor, where is the problem?
c) If it was "char s[20];" vs "char *s;" do we need to write a Copy constructor?
d) Which part of memory (STACK HEAP DATA TEXT) is the:
variable s1 stored?
variable s stored?
value *s stored?
----------------------------------------------------------------------
extern "C"
{
#include
}
#include
class Strg
{
private:
char *s;
int len;
public:
Strg(void){ len = 5; s = new char[len + 1]; strcpy(s, "Hello"); }
Strg(Strg &r){ len = strlen(r.s); s = new char[len + 1]; strcpy(s, r.s); }
~Strg() { delete [] s; }
friend ostream& operator<<(ostream &os, const Strg& r){os<
int fn ( int n )
{
if ( n < 2 )
return n;
else
return ( n + fn ( n - 1 ) );
}
int main ( void )
{
int i;
for ( i = 0 ; i < 5 ; ++i )
cout << fn(i) << endl;
return 0;
}
_____________________________________________________________________
2) What is printed if you type in the input: 12345ABC
-------------------------------------------------------
extern "C"
{
#include
}
#include
void fn ( void )
{
char ch;
if ( isdigit(ch = cin.get()) )
fn();
cout << ch;
}
int main ( void )
{
fn();
return 0;
}
______________________________________________________________________
VI. POINTERS and LINKED LISTS
a) What if loop was like this: for( p = &head; p ; p = q ){..}
b) What if void insert_node() was like this:
void insert_node ( Node *prev, Node *add )
{
prev->next = add;
add->next = prev->next;
}
c)What gets printed?
------------------------------------------------------------------
#include
struct Node
{
int value;
Node *next;
};
void insert_node ( Node *prev, Node *add )
{
add->next = prev->next;
prev->next = add;
}
int main ( void )
{
int i;
Node head, *p = &head, *q;
head.next = 0;
for ( i = 0 ; i < 4 ; ++i )
{
q = new Node;
q->value = i ;
insert_node ( p, q );
p = q;
}
for( p = head.next; p ; p = q )
{
cout << p->value << endl;
q = p->next;
delete p;
}
return 0;
}
___________________________________________________________________
VII) C++ I/O STREAMS
a) If "dear_john" looks like this:
---------------------------------------------------------
DEAR JOHN
I love C++ more than YOU !!
Farewell
---------------------------------------------------------
What does "dear_mom" look like after program is run?
---------------------------------------------------------
b) What gets printed on the terminal screen?
c) Why is the " extern "C" " inserted in the program?
extern "C"
{
#include
}
#include
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;
}
Home ||
Course Outline ||
Course Description ||
lab Hours
||FAQ
||Midterm Review
|| Final Review||