GIVEN: a program point(in c++) with three files(point.h,point.cpp,main.cpp) a) p
ID: 3683666 • Letter: G
Question
GIVEN: a program point(in c++) with three files(point.h,point.cpp,main.cpp)
a) point.h
/*filename:point.h*/
#include <iostream>
using namespace std;
class point {
private:
double xcoord, ycoord;//class attributes
public:
//default constructor
point();
// parameterized constructor
point(double x, double y);
//overloaded operators
double operator -(const point & rhs) const;
bool operator ==(const point & rhs) const;
// Accessor methods
double getX() const { return xcoord; }
double getY() const { return ycoord; }
//mutators methods
void setX(double newX);
void setY(double newY);
void setXY(double newX, double newY);
// print method to cout
void point::print(ostream & out)const;
};
b) point.cpp
/*filename:cpp*/
#include "point.h"
#include <iostream>
#include <cmath>
using namespace std;
//default constructor
point::point()
{
cout << " constructing point object, default: ";
cout << "initializing to zero" << endl;
xcoord = 0.0;
ycoord = 0.0;
}
//parameterize constructor
point::point(double x, double y)
{
//input parameters x,y
cout << " constructing point object, parameterized: ";
cout << "input parameters:" << x << "," << y << endl;
xcoord = x;
ycoord = y;
}
//overload the operator
double point ::operator-(const point & rhs)const{
double t1, t2, d;
t1 = rhs.xcoord - xcoord;//(x2-x1)
t2 = rhs.ycoord - ycoord;//(y2-y1)
d = sqrt(pow(t1, 2) + pow(t2, 2));
return d;
}
//overload the operator==
bool point::operator ==(const point & rhs) const{
if (rhs.xcoord == xcoord){
return true;
}
else{
return false;
}
}
//mutator method to set the x coordinate
void point::setX(double xval){
xcoord = xval;
}
//mutator method to set the y coordinate
void point::setY(double yval){
ycoord = yval;
}
//mutator method to set the x and y coordinates
void point::setXY(double xval, double yval){
xcoord = xval;
ycoord = yval;
}
void point::print(ostream & out)const{
out << "point(" << xcoord << ", " << ycoord << ")" ;
}
c) main.cpp
/*file name.cpp*/
#include "point.h"
#include <iostream>
#include <cmath>
using namespace std;
//function prototype.
void compare_points(point p1, point p2);
int main(){
point p1;
p1.setY(-1);
cout << endl;
cout << " p1 x coordinate is " << p1.getX() << endl;
cout << "p1 y coordinate is " << p1.getY() << endl << endl;
point p2(0, 5);
cout << "p2 x coordinate is " << p2.getX() << endl;
cout << "p2 y coordinate is " << p2.getY() << endl << endl;
compare_points (p1, p2);
point p3;
p3.setXY(-2, 2);
point p4(1, 2);
compare_points(p3, p4);
p4.setX(-2);
p4.setX(-2);
compare_points(p3, p4);
}
void compare_points(point p1, point p2){
cout << " Now comparing";
p1.print(cout);
cout << "and";
p2.print(cout);
cout << ":" << endl;
double distance;
if (p1 == p2){
cout << "the two points are equal" << endl;
}
else{
distance = p1 - p2;
cout << "the distance between the two points is "
<< distance << endl << endl;
}
}
QUESTIONS:
1.list all of the objects in the main() method:
2.list all of the objects in the compare_points() method:
3.list the signatures from all mutator methods for class Point:
4.list the signatures from all accessor methods for class Point:
5.in general, if a method has the keyword const at the end of its signature, what does that mean?
6.list the signatures from all methods for class Point that are const.Why did we choose those methods to be const?
7.by creating the following method for Point:
void Point::print(ostream& out) const {
. . . this enabled us to write WHAT lines of code in main.cpp?
8.by creating the following method for Point:
double Point::operator -(const Point& rhs) const {
. . . this enabled us to write WHAT line of code in main.cpp?
9.by creating the following method for Point:
bool Point::operator ==(const Point& rhs) const {
. . . this enabled us to write WHAT line of code in main.cpp?
Explanation / Answer
1)point p1
point p2
point p3
point p4(1,2)
2)point p1
point p2
3)
void point::setX(double xval);
void point::setY(double yval);
void point::setXY(double xval, double yval);
void setX(double newX);
void setY(double newY);
void setXY(double newX, double newY);
4)
double getX() const
double getY() const
5) The const on the end of the function is not the same as the const at the beginning. This const prevents this function from changing the member variables of the class to which it belongs
6)
double operator -(const point & rhs) const;
bool operator ==(const point & rhs) const;
double getX() const;
double getY() const;
they const because...this const prevents the function from changing the member variables of the class to which it belongs..
7)void Point::print(ostream& out) const
void :- it is not returning anything..
Point:-belongs to Point class
print:- methd name
ostream :- used print some output...
const:-prevents the function from changing the member variables of the class to which it belongs..
8)double Point::operator -(const Point& rhs) const {
double:- the return type is double...i
Point:- class name
operator : - method name
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.