Specify and design a class 3D_Point that can be used to keep track of the positi
ID: 3686133 • Letter: S
Question
Specify and design a class 3D_Point that can be used to keep track of the position of a point in 3-dimensional space. Include member functions to: set a point to specified location, to shift a point a given amount along the axes. Also, include non-member functions in the specifications to input 3-diemnsional points, output 3-diemnsional points as (X, Y, Z), and overload operators for “==” and “!=”.
Note that for the above class you are only responsible for the specification and design. You do not need to worry about its implementation. You will use these specifications to specify, design, and implement the class described below.
In 3-dimensional space, a line segment is defined by its two endpoints. Specify, design and implement a class for a line segment. The class should have two private member variables that are points from class 3D_Point. The member and non-member functions should be: (i) return the length of a line segment; and (ii) overload operator == to check if two line segments are equal.
Explanation / Answer
//#_3DPoint .h
//Note: class names must be start with a chracter
#ifndef POINT
#define POINT
class _3DPoint
{
private :
double x;
double y;
double z;
public:
//member functions
_3DPoint();
_3DPoint(double,double,double);
void setX(double x);
void setY(double y);
void setZ(double z);
double GetX() ;
double GetY() ;
double GetZ() ;
//non-member functions
friend bool operator== (_3DPoint &cP1, _3DPoint &cP2);
friend bool operator!= (_3DPoint &cP1, _3DPoint &cP2);
};
#endif POINT
------------------------------------------------------------------------------
//Implemenetation file _3DPoint
//_3DPoint.cpp
#include<iostream>
#include "_3DPoint.h"
using namespace std;
//defualt constructor
_3DPoint::_3DPoint()
{
x=0;
y=0;
z=0;
}
//parameterized constructor
_3DPoint::_3DPoint(double x,double y ,double z)
{
setX(x);
setY(y);
setZ(z);
}
void _3DPoint::setX(double x)
{
this->x=x;
}
void _3DPoint::setY(double y)
{
this->y=y;
}
void _3DPoint::setZ(double z)
{
this->z=z;
}
double _3DPoint::GetX()
{
return x;
}
double _3DPoint::GetY()
{
return y;
}
double _3DPoint::GetZ()
{
return z;
}
//Returns true if the two _3Dpoint objects are equal
//otherwise returns false
bool operator== (_3DPoint &p1, _3DPoint &p2)
{
return p1.GetX()==p2.GetX() &&
p1.GetY()==p2.GetY() &&
p1.GetZ()==p2.GetZ() ;
}
//Returns true if the two _3Dpoint objects are not equal
//otherwise returns false
bool operator!= (_3DPoint &p1, _3DPoint &p2)
{
return p1.GetX()!=p2.GetX() ||
p1.GetY()!=p2.GetY() ||
p1.GetZ()!=p2.GetZ() ;
}
------------------------------------------------------------------------------
//LineSegment.h
#ifndef LINE_SEGMENT
#define LINE_SEGMENT
//include header file _3DPoint
#include "_3DPoint.h"
class LineSegment
{
private :
_3DPoint p1;
_3DPoint p2;
public:
//member functions
LineSegment(_3DPoint p1, _3DPoint p2);
double lenght();
//non-member functions
friend bool operator== (LineSegment &l1, LineSegment &l2);
};
#endif LINE_SEGMENT
------------------------------------------------------------------------------
//LineSegment.cpp
//include LineSegment.h header file
#include "LineSegment.h"
//include math.h header file
#include<math.h>
//parameterized constructor
LineSegment::LineSegment(_3DPoint p1, _3DPoint p2)
{
this->p1=p1;
this->p2=p2;
}
//Returns the lengyt
double LineSegment::lenght()
{
return sqrt((abs(p1.GetX()-p2.GetX())*(abs(p1.GetX()-p2.GetX())+
abs(p1.GetY()-p2.GetY())*abs(p1.GetY()-p2.GetY())+
abs(p1.GetZ()-p2.GetZ())*abs(p1.GetZ()-p2.GetZ()))));
}
//Returns true if the two LineSegment objects are equal
//otherwise returns false
bool operator== (LineSegment &l1, LineSegment &l2)
{
return l1.p1.GetX()==l2.p1.GetX() &&
l1.p1.GetY()==l2.p1.GetY() &&
l1.p1.GetZ()==l2.p1.GetZ() ;
}
------------------------------------------------------------------------------
//Tester program to test the _3DPoint and LineSegment class
#include <iostream>
#include "LineSegment.h"
using namespace std;
int main()
{
//Create two _3DPoint objects
_3DPoint point1(1,2,3);
_3DPoint point2(5,7,8);
//Check == operator
if(point1==point2)
cout<<"two _3DPoints are equal"<<endl;
else
cout<<"two _3DPoints are not equal"<<endl;
LineSegment lineSeg1(point1, point2);
LineSegment lineSeg2(point1, point2);
//Check == operator
if(lineSeg1==lineSeg2)
cout<<"Two LineSegments are equal"<<endl;
else
cout<<"Two LineSegments are not equal"<<endl;
//print lenght of linesegment
cout<<"lenght of the line semenet : "<<lineSeg1.lenght()<<endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------
Sample output:
two _3DPoints are not equal
Two LineSegments are equal
lenght of the line semenet : 14.6969
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.