Computer Science 1622
Computer Science II

Laboratory Assignment 10
Introduction to Classes
Due at the end of Lab

Introduction

The C++ class mechanism allows users to define their own data types.Classes are typically used to define abstractions that do not map naturally into the predefined or derived data types.

A class definition has 2 parts: the class head, composed of the keyword class followed by the class tag name, and the class body, enclosed by a pair of curly braces, which is followed by a semicolon. For example:
      class Screen{ /* ... */ }; 

A C++ class has the following associated attributes:

  1. A collection of data members of the class. Data members are fields of the class and sometimes referred to as instance variables. The declaration of class data members is the same as for variable declarations with an exception that explicit initializer is not allowed. For example:
          class Screen{
               int height; 
    	   int widht;
    	   char* cursor;
          };
    
  2. A collection of member functions which are the set of operations that may be applied to objects of that class. The members functions of a class are declared inside the class body. A declaration consists of the function prototype. For example:
          class Screen{
          public:
              void home();
    	  void move(int, int);
    	  char get();
          };
    
    The entire definition of a member function can also be placed inside the class body. For example:
          class Screen{
          public:
              void home() { height = width; }
    	  char get()  { return *cursor; }   
          }; 
    
    A member function defined outside the class body must exlpicitly specify its scope( that is the class that it belongs to). The scope resolution operator '::', resolves the scope of a member of a class. In its binary form, it takes two operands, one on the left which is always the class name and one on the right which is the class member name. For example,
          class Screen{
          public: 
              void home();
    	  char get();
          };
    
          void Screen::home() { height = width; }
          char Screen::get()  { return *cursor; }
    
  3. Levels of program access. Members of a class may be specified as private, protected, or public. These levels control access to members from within the program.
  4. An associated class tag name, serving as a type specifier for the user-defined class.

The definition of a class does not cause any memory to be allocated. Memory is allocates for a class with the definition of each class object. Thus the definition

                  Screen myScreen;
allocates a chunk of storage sufficient to contain the Screen class data members.

The members of a class can be accessed using the class object selector ".". For example, if you wanted to access the data member 'width' of the instance myScreen of the class Screen, it can be done as

                  myScreen.width

Member functions of a class provide the set of operations a user may perform on the class type. One specialized member function used as an initialization function is called a constructor. This member function is invoked implicitly each time a class object is defined or alloated. A constructor is specified by giving it the same name as that of the class that it belongs to. An example of the use of a constructor:

        class Screen {        
	      int height;
	      int width;
	      Screen(int, int);  // Constructor prototype
	};
	
	Screen::Screen(int A, int B){
	// Constructor definition
	      height = A;
	      width = B;
	}
	
	void main(){
	      Screen S1(3,3);// creates an instance S1 of class Screen and 
	                     // initializes its height and width to 3 and 3 
			     // respectively.
	}

The following in-lab assignment involves the use of all the above mentioned features of classes and will give you some practice with implementing these features.

The Program

The first thing that the following program does is to define a class Point withtwo private integer variables x_coord and y_coord which specifies the x_coordinate and the y_coordinate of a point.

The class has a public default constructor Point(), which initializes the x_coord and the y_coord of an instance(point here) to zero's.

It also has 2 other functions set_x_coord and set_y_coord which takes in integers as arguments and sets the x_coordinate and y_coordinate equal to those integers respectively.

And it has a member function Distance(Point p) which calculates the distance between the point p and the current point being referred to.

The program basically creates two instances p1 and p2 of the Point class initializing their x and y coordinates to zero's respectively. It then sets the x and y coordinates of point p2 to 3 and 3 respectively. It then calculates and prints the distance between the points p1 and p2.

Things To Do !

The following are the changes that you have to make to the code and they are listed in the code as well.
  1. The first thing that you have to do is to create a new constructor that will take in two integer arguments A and B and initialize the x_coordinate of thepoint to A and the y_coordinate of the point to B.

  2. Next create a new instance(point) P3 and initialize its x_coord and y_coord to 6 and 6 respectively using the concept of the new constructor that you have created(dont use the functions set_x_coord and set_y_coord for this purpose).

  3. The next thing that you have to do is to find the distance between this point p3 and the existing point p2 with coordinates 3 and 3.This you can do by calling the function Distance providing it with the appropriate argument.

  4. Finally write a new member function Move_Point that moves the x_coordinate of the point by some distance and the y_coordinate of the point by some distance. For this you need to input the 2 values move_x and move_y through which you want to move the x and y coordinates of the point and then pass these 2 values as arguments to that member function. The function Move_Point should then make the necessary changes to the x and y coordinates and print the updated coordinates.

The Code

#include <iostream.h>
#include <iomanip.h>
#include <math.h>

class Point{

private:
  int x_coord;
  int y_coord;

public:
  
  Point();     // This is the prototype for the default constructor for the 
               // class Point.
  
  // prototypes for the member functions are as given below.  
  void set_x_coord(int A);
  void set_y_coord(int B);
  double Distance(Point P);

  // Change #1 
  //Declare the prototype for the new constructor that takes initial
  //values for the x and y coordinates.
  
  // Change #4 
  //Include the prototype for the new function that you are going to 
  //write to move a point(that is change its x and y coordinates) here. 
  
};


Point::Point(){ //This function is the default constructor for the class Point.
  x_coord = 0;
  y_coord = 0;
}


// Change #1
// write the definition for a new constructor here that will take in two 
//integer arguments A and B and initialize the x_coordinate of the point to A 
//and the y_coordinate of the point to B



// the following are the functions to set the x and y coordinates to particular
// values.
void Point::set_x_coord(int A){
  x_coord = A;
}

void Point::set_y_coord(int A){
  y_coord = A;
}



double Point::Distance(Point P){    // The following is the function definition
                                    // for the member function Distance
  double dist;
  
  dist = sqrt(((P.x_coord-x_coord)*(P.x_coord-x_coord)) + ((P.y_coord-y_coord)*(P.y_coord-y_coord)));
  return dist;
}



// Change #4 
//Write the definition of the member function Move_Point to change the 
//x_coordinate and the y_coordinate of the point here. 
//The function should then  print the coordinates of the new point.



void main(){
  double dist;
  Point P1,P2;

// Change #2 
//create a new instance(point) P3 and initialize its x_coord and y_coord to 6 
//and 6 respectively using the concept of the new constructor that you have 
//created(dont use the functions set_x_coord and set_y_coord for this purpose)
 
  P2.set_x_coord(3);
  P2.set_y_coord(3);

  dist = P1.Distance(P2);
  cout<<endl;
  cout<<dist<<endl;

// change #3
//find the distance between the point p3 that you have just created and the 
//existing point p2 with coordinates 3 and 3. This you can do by calling the 
//function Distance providing it with the appropriate argument.
  
// Change #4 
//Input the 2 values move_x and move_y through which you want to move the x 
//and y coordinates of the point P3 and then pass these 2 values as arguments 
//to the function Move_Point. The function Move_Point should then make the 
//necessary changes to the x and y coordinates of Point P3 and print its 
//updated coordinates.

}


What to turn in

Turn in a hard copy of your final source code along with a printout of your output.