1)    POINTERS

 

a)    (3 pts) Declare an integer pointer.

 

 

b)    (3 pts) Use the integer pointer to allocate a dynamic array of 30 integers.

 

 

c)    Given the following:

 

           double *dp, *dq;

          

 

           double x = 78.9;

           double y = 23.4;

 

           dp = &x;

           dq = &y;

 

 

i)      (3 pts) What is the value of *dp?

 

 

 

ii)     (6 pts) Write the C++ code that will swap the values of x and y, WITHOUT USING x and y in your code.

 

 

 

 

 

 

 

 

iii)    (5 pts) Given the following code, write an expression that uses b to copy the third element of the array to the fourth element.  DO NOT use a in your answer.

 

 

 

float a[5] = {0.0, 1.1, 2.2, 3.3, 4.4};

float *b = &a[1];

 

 

 

    

 

 

 

 

2)    Here is a class definition for a Point class.  Objects in the class represent points on the Cartesian Graph.

 

   1     class Point {

 

   2     public:

   3          // Uses default arguments to allow calling with

   4          // zero, one, or two values.

   5          Point(double x = 0.0, double y = 0.0);

 

   6          // Computes distance to another point using Pythagorean            //theorem.

   7          double dist(Point other);

 

   8          // Move the existing point.

   9          void move(double a, double b);

 

  10          //print the point nicely

  11          void print();

 

  12     private:

  13          double xval;

  14          double yval;

 

 

  15     };

 

Point::Point(double x, double y) {

                xval = x;

                yval = y;

        }

void Point::move(double a, double b)

        {

                xval += a;

                yval += b;

        }

double Point::dist(Point other)

       {

                double xd = xval - other.xval;

                double yd = yval - other.yval;

                return sqrt(xd*xd + yd*yd);

        }

 

void Point::print()

        {

                cout<< "(" << xval << "," << yval << ")";

        }

       

 

a)    (3 pts) Which line in the class definition contains the constructor?

 

 

 

b)    (10 pts) Give the prototype and defintion for the overloaded + operator, that will add two points.  You can define the addition of two points as follows: (x1,y1) + (x2,y2) = (x1+y1,x2+y2)

 

 

prototype:_____________________________________________

 

definition:

 

 

 

 

 

 

 

 

 

 

 

c)    (10 pts) Assume the class defintion above, plus the added overloaded addition from question b).  What is printed by the following code?

 

int main()

{

        // Some points.

           Point a(5.2, -4.8);

           Point b(1.0, 9.0);

        Point d;  //look at default values

           a.print();

        b.print();

        d.print();

 

        cout << endl;

 

        b.move(2,-5);

 

        b.print();

       

        (a + b).print();

        cout << endl << endl;

       

        b.print();

        cout << " is " << b.dist(d) << " from ";

        d.print();

        cout << endl;

 

      

        }

d)    (10 pts) Suppose that we want users to input a point in the following format (note that the numbers here are just examples; the user can input any doubles):   (2,3.5)  Give the prototype and definition for the overloaded >> operator.

 

prototype:____________________________________________________________

 

definition:

 

 

 

 

 

 

 

3)      TWO DIMENSIONAL ARRAYS

 

 

a)      (4 pts) The following code was written to declare a dynamic two-dimensional array with 10 rows and 4 columns, but it is incorrect!  Correct it below.

 

 

      int **matrix = new int matrix[10];

 

      int i;

 

      for (i = 0; i < 4; i++)

            matrix[i] = new int[10];

 

 

 

 

b)      (5 pts) Write the code to free (deallocate) the two-dimensional array from part a) above.

 

 

 

 

 

 

 

 

4)      Here is a class that you know!!

 

class movieInfo

{

private:

     int numActors; //number of Actors on the list

     string * actorList; //holds all the actor names

     string director;

public:

     void printActors() const; //prints all the actors

     void readActors(); //gets actor names from cin

     void setDirector(string); //sets the director name

     string getDirector() const; //returns the director name

     movieInfo(); //sets empty actorList

     movieInfo(int); //sets numActors to int parameter

};

 

 

a)    (10 pts) Write the prototype and defintion for a member function called sharesDirector.  This function returns true if two movies have the same director and returns false otherwise.

 

prototype:____________________________________________________________

 

definition:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b)    (10 pts) Write the prototype and defintion for a member function calles sharesActors.  This function returns the number of shared actors (i.e. the count of actors that are in both movies.)  If there are no actors that are in both movies, then the function returns a zero.

 

prototype:____________________________________________________________

 

definition:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5)    Questions:

 

a)    (5 pts) What does this refer to in C++?  Give one example of when you would use this.

 

 

 

 

 

 

b)    (5 pts) When must you overload the assignment operator?  Why?

 

 

 

 

 

 

 

c)    (5 pts) Why are pointers so useful?