Homework Answers: Classes II

1.   9
     11


2.  x   default constructor
    y   constructor with 1 int parameter
    z   default constructor
    d   constructor with 1 int parameter
    e   constructor with 1 int parameter


3.  int A::e(A other)
    {
       if (n == other.n)
          return 1;
       else
          return 0;
    }


4.  class B
    {
    public:
       B() { val = 1; }   // inline constructor
       void p();
       void q(float);
       void pr();
    private:
       float val;
       A myA;
    };
    void B::p()
    {
       val *= 10;
       myA.f();
    }
    void B::q(float fact)
    {
       val = val / fact;
    }
    void B::pr()
    {
       cout << “val = “ << val << endl << “myA = “;
       myA.k();
    }


5.  int main()
    {
       B bOne, bTwo;
       A aOne;
       aOne.g();
       aOne.g();
       bOne.p();
       bOne.p();
       bOne.p();
       bOne.q(100);
       aOne.k();
       bOne.pr();
       bTwo.pr();
       return 0;
    }

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2006