CSE 109 Test 1   23 February 2009
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWER<<<<<<<<<<<<<<<<<<<<<<<<<<
1. Given the following class and funtion definitions, state the output when
the code below the functions is executed.
void a(int  b, int &c, int &d){
  c+=2;
  b+=d+c;
  d+=3;
}
class A{
 public: int j;
};
void b(A &a, A b){
 a=b;
 b.j+=2;
}
void c(int *a,int *&b){
 a[2]=4;
 b=a;
 b[2]=8;
}

int main(){
int u,v,w;
 u=2; v=3; w=4;
 a(u,v,w);
 cout<<u<<" "<<v<<" "<<w<<endl;//-----------------> 2 5 7
 u=2;
 a(u,u,u);
 cout<<u<<endl;//---------------------------------> 7
A s,t;
 s.j=2;
 t.j=3;
 b(s,t);
 cout<<s.j<<" "<<t.j<<" "<<endl;//----------------> 3 3
int p[]={1,2,3},q[]={9,10,11},*qp;
 qp=q; 
 c(p,qp);
 cout<<p[2]<<" "<<qp[2]<<endl;//------------------> 8 8
}

=======================================================================
2.  I want a simple class that only stores a letter grade. Write the
declaration and definition (code) for the class Grade so that the code
below will compile and produce the indicated output when run. Before
writing the declaration, read the next question.  You may assume you have
available the function  void check(bool b, char *mess);
Grade s('A'),t(s);
t.set('C');
cout<<s<<t<<endl;//Grade(A)Grade(C);
========================================================================
class Grade{
  protected:
    char grd;
  public:
    Grade(char ch);
    Grade(const Grade &g);
    void set(char g);
    friend ostream & operator<<(ostream & out,const Grade &g);
};

 Grade::Grade(char ch):grd(ch){
  check(ch>='D' && ch<='A' || ch=='F',"(Grade::Grade()) Bad initial grade");
 }

 Grade::Grade(const Grade &g):grd(g.grd){}

 void Grade::set(char ch){
   check(ch>='D' && ch<='A' || ch=='F',"(Grade::set()) Bad grade");
   grd=ch;
 }

ostream & operator<<(ostream & out,const Grade &g){
  out<<"Grade("<<g.grd<<")";
  return out;
}
=============================================================================
3.  The class Grade in question 2 is pretty useless, so I want to create a
subclass, StudentGrade, that enables me to store a student Lehigh ID.
Write the declaration and definition (code) for the class StudentGrade so
that the code below will compile and produce the indicated output when run.
StudentGrade t('A',882345675);
 t-=2; //decrease the grade by 2 grades to C; should do similar things for
       //other grades
 cout<<t<<endl; //Grade(C)
============================================================================
class StudentGrade:public Grade{
 private:
   int id;
 public:
  StudentGrade(char c,int d);
  StudentGrade & operator-=(int j);
};

StudentGrade::StudentGrade(char c,int d):Grade(c),id(d){
  check(d>0 && d<999999999,"(StudentGrade::StudentGrade(char,int)) bad id");
}

StudentGrade & StudentGrade::operator-=(int j){
  check(j>=0,"(StudentGrade::(-=)) need decrement > 0");
  grd=grd+j;
  if(grd=='E')
    grd='F';
  check(grd>='A' && grd<='D' || grd=='F',
     "StudentGrade::(-=) bad decrement");
  return *this;
}
============================================================================
4.  Write a C++ progam that reads ints from the console and writes out to
the screen the largest and the smallest. It should stop reading the ints
when reading from cin fails.
============================================================================
#include <iostream>
using namespace std;

int main(){
 int n,max,min;
 cin>>n;
 if(!cin.good()){
   cout<<"No ints entered\n";
   return 0;
 }
 max=n;
 min=n;
 while(cin.good()){
  if(max<n)
    max=n;
  else if(min>n)
    min=n;
 cin>>n;
 }
 cout<<"Max: "<<max<<", min: "<<min<<endl;
}

