Object Oriented Programming

BS – Spring 2003

Second Mid-Term

April 4, 2003                                                                                                     Time: 1:30 Hrs

 

Q1.             

     

a.       Write the exact type of this pointer in a const member function of a class.    02

b.      Write two distinct situations in which copy constructor of a class is called.      02

c.       What are the main uses of member initialization list?                                               02

d.      What is the need for this pointer?                                                                 02

e.      Why can’t function overloading be done by making its return type different?   02

 

Q2.                  class Complex

            {

   private:

                        double x,y;

                        static int z;

               public:

                        Complex(double = 0.0);

                        Complex operator+(const Complex&);

                        Complex& operator=(const Complex&);

                        Complex& operator--( );

                        static int doSomething() { z = 2 * y; return z; }

            };

 

Given that z1, z2 and z3 are objects of this class, answer the following:

 

a.       Write the exact function calls for the following three expressions                    06

                                                                          i.      z1 + z2 + z3

                                                                        ii.      z1 = z2 = z3

                                                                      iii.      z1 = z2 + (--z3)

b.      What is wrong in the definition of member function doSomething().              02

c.       What will be the effect of making the constructor in the above class private?  02

d.      Write the functions called in appropriate sequence during the execution of     

the following statements. Describe if there are any errors.

                                                                          i.      z1 =  z2 + 3.5;                                                                      03

                                                                        ii.      z1 =  3.5 + z2;                                                                      02

           

Q3.            Define and Implement the String class that makes the following code work.    10     

       The class should store the string in a dynamically allocated memory.

 

void main()

{

                                    String X, Y = “World!”;

                                    X = “Hello “ + Y;

                                    cout << !X << endl;

                                    X.Print( );

}   

 

where !X is true if string is empty.

Q4.            Consider the following class:                                                                        10

 

class Circle {

private:

                        int center_x;

                        int center_y;

                        float radius;

                        const float Pi;

                        static int count;

};

 

Complete the above class so that the following program can produce the output next to it:

 

void main( )

{

            Circle first, second(-3, 5, 1.2), third(3.5);

            cout << first.GetArea( ) << endl;

            cout << second.GetCount( ) << endl;

            {

                        Circle first(5, 3);

                        first.Print( );

                        cout << third.GetCount( ) << endl;

}

cout << second.GetCount( ) << endl;

first.SetArea( 90.8 );

cout << first.GetRadius( );

}

 

Output:

 

22.7

3

5, 3, 1, 22.7

4

3

2