171k views
0 votes
Programming Exercise 15, Chapter 11, describes how to design the class pointType to implement a point. Redo this programming exercise so that the class pointType

Programming Exercise 15, Chapter 11, describes how to design the class pointType to implement a point. Redo this programming exercise so that the class pointType:

Overloads the stream insertion operator, <<, for easy output.

Overloads the stream extraction operator, >>, for easy input. (A point is input as (a, b).)

Overloads the assignment operator to copy a point into another point.

Overloads the binary operator +, as a member function, so that it returns the distance between two points.

Overloads the operator ==, as a member function, so that it returns true if two points are the same; falseotherwise.

Overloads the operator !=, as a member function, so that it returns true if two points are not the same; falseotherwise.

Write a program to test your class.

***************************

pointType.cpp

***************************

//Implementation File for the class integerManipulation

#include
#include
#include "pointType.h"

using namespace std;

void pointType::setPoint(double a, double b)
{
x = a;
y = b;
}

void pointType::setX(double a)
{
x = a;
}

double pointType::getX()
{
return x;
}

void pointType::setY(double b)
{
y = b;
}

double pointType::getY()
{
return y;
}

double pointType::distance(pointType & point)
{
return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2));
}

void pointType::print() const
{
cout << "(" << x << ", " << y << ")" << endl;
}

pointType::pointType(double a, double b)
{
setPoint(a, b);
}

*******************
pointType.h

*******************

#ifndef pointType_H
#define pointType_H

class pointType
{
public:
void setPoint(double a = 0, double b = 0);
//Function to set the x and y coordinates of a point
//Postcondition x = a; y = b;

void setX(double a = 0);
//Function to set x coordinate.
//Postcondition: x = a;

double getX();
//Function to return the x coordinate.

void setY(double b = 0);
//Function to set y coordinate.
//Postcondition: y = b;

double getY();
//Function to return the y coordinate.

double distance(pointType & point);
//Function to return the distance between this point
//point

void print() const;
//Function to print the x and y coordinates.

pointType(double a = 0.0, double b = 0.0);
//Constructor with a default parameter.

private:
double x;
double y;
};

#endif

User Meseret
by
7.7k points

1 Answer

3 votes

Final answer:

The student's question is about enhancing a C++ class named pointType that represents a geometric point by overloading various operators for ease of use and intuitive handling of class objects.

Step-by-step explanation:

The question pertains to the modification of a C++ class called pointType, originally designed to represent a geometric point, to include additional functionalities such as overloading various operators. This enhancement will facilitate the handling of these points in a more intuitive and efficient manner through operator overloading, which is a feature of C++ that allows us to define the behavior of operators for user-defined types.

Overloading Operators for class pointType

  • To overload the stream insertion operator <<, it allows the class to output point objects directly to output streams, such as cout.
  • The stream extraction operator >> overloading enables the class to read point objects directly from input streams, such as cin, by interpreting the input as coordinates (a,b).
  • Overloading the assignment operator provides a way to copy one point object to another.
  • The binary operator + is redefined not to perform the usual arithmetic addition, but rather to calculate the distance between two points.
  • The equality operator == is used to compare two point objects and determine if their coordinates are the same.
  • Lastly, the inequality operator != will do the opposite — check if the point objects are not the same.

Each of these operator overloads will be a member function of the class, with the appropriate return type and parameters based on what is typical for such operators within the context of C++. The modifications to the pointType class will be complemented by a driver program to test the aforementioned enhancements and ensure their correct implementation.

User Hiep Tran
by
7.3k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.