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:
class Screen{ int height; int widht; char* cursor; };
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; }
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 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.
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.
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).
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.
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.
#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. }
Turn in a hard copy of your final source code along with a printout of your output.