EGR-126 - Lab 9 – Chapter 6 – Classes – Class Point Create a Project called Poin
ID: 3682000 • Letter: E
Question
EGR-126 - Lab 9 – Chapter 6 – Classes – Class Point
Create a Project called Point from the given file(point.h,point.cpp,main.cpp)
Add a new Item to the Project – choose C++ Header file.Name the file Point.h
Type in the code for Point.h based on the code provided.Save the file.Fix any compile errors.
Note this is the same as the code from Page 268, except for new methods have been added:
void setXY(double newX, double newY);
void Point::print(ostream& out) const;
Add a new item to the Project – choose C++ .cpp file.Name the file Point.cpp
Type in the code for Point.cpp.Save the file.Fix any compile errors.
Note this is the same as the code from Page 270, except for new methods have been added:
void setXY(double newX, double newY)
void Point::print(ostream& out) const
Add a new item to the Project – choose C++ .cpp file.Name the file main.cpp
Type in the code for main.cpp.Save the file.Fix any compile errors.
Note this is the similar to the code provided on Page 273, but with many additional changes:
Construct four objects:p1, p2, p3, and p4
Call various flavors of the constructors and methods to set the X and Y coordinates
Call the print method
Add a new method:
void compare_points(Point p1, Point p2)
Write this code and get it working.Paste the code from your Point.h file:
Paste the code from your Point.cpp file:
Paste the code from your main.cpp file:
Paste a screenshot of the output screen:
Write this code and get it working.Paste the code from your Point.h file:
Paste the code from your Point.cpp file:
Paste the code from your main.cpp file:
Paste a screenshot of the output screen:
sample output:
constructing point object,defaul:
initializing to zero
p1 x coordinate is 0
p1 y coordinate is -1
constructing point object, parameterized:
input parameters:0.5
p2 x coordinate is 0
p2 y coordinate is 5
now comparing point(0,-1) and point (0.5):
the distance between the two points is 6
constructing point object, default:
initializing to zero
constructing point object,parameterized :
input parameters :1,2
now comparing point (-2,-2) and point (1,2):
the distance between the two points is 5
now comparing point(-2,-2) and point (-2,-2):
the two points are equal
press any key to continue......................
GIVEN FILES;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 x and y coordinates
void point::setXY(double xval, double yval){
//TODO-WRITE THE CODE FOR THIS METHOD
}
void point::print(ostream & out)const{
out << "point(" << xcoord << ", " << ycoord << ")" ;
}
C) file: main.cpp
/*file name.cpp*/
#include "point.h"
#include <iostream>
#include<cmath>""
using namespace std;
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);
//TODO-WRITE THE CALL TO COMPARE POINTS p3 and p4
p4.setX(-2);
p4.setX(-2);
// TODO-WRITE THE CALL TO COMPARE POINTS p3 and 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;
}
}
Explanation / Answer
/*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 print(ostream & out)const;
};
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 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 << ")" ;
}
main.cpp
/*file name.cpp*/
#include "point.h"
#include <iostream>
#include<cmath>
void compare_points(point p1,point p2);
using namespace std;
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);
//TODO-WRITE THE CALL TO COMPARE POINTS p3 and p4
p4.setX(-2);
p4.setX(-2);
compare_points(p3,p4);
// TODO-WRITE THE CALL TO COMPARE POINTS p3 and 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;
}
}
output:
constructing point object, default:
initializing to zero
p1 x coordinate is 0
p1 y coordinate is -1
constructing point object, parameterized:
input parameters:0,5
p2 x coordinate is 0
p2 y coordinate is 5
Now comparingpoint(0, -1)andpoint(0, 5),
the two points are equal
constructing point object, default:
initializing to zero
constructing point object, parameterized:
input parameters:1,2
Now comparingpoint(-2, 2)andpoint(1, 2),
the distance between the two points is 3
Now comparingpoint(-2, 2)andpoint(-2, 2),
the two points are equal
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.