Activities 2Document Viewor Thu Sep 27, 14:34 Programming-Exam-1-Section-03.pdf
ID: 3755732 • Letter: A
Question
Activities 2Document Viewor Thu Sep 27, 14:34 Programming-Exam-1-Section-03.pdf The problem 1. 2. Create a class called myPoint with x and y fields Overload the >> operator for the class to read a point in the format (x,y) a. E.g., (4.35, 6.87) should read 4.35 in the field x and 6.87 in the field y 3. 4. Create a class called myLine with two fields of type myPoint Create a member function in myLine called getLength that computes the length of the line as follows: (X1-X2)2 + (y1-½)2 if the end points are (X1 , y1) and (x2+ 5. 6. Overload >,Explanation / Answer
//C++ program
#include<iostream>
#include<math.h>
#include<fstream>
using namespace std;
class myPoint{
private:double x,y;
public:
friend istream & operator >> (istream &in, myPoint &p){
cout<<"Enter x : ";
in>>p.x;
cout<<"Enter y : ";
in>>p.y;
}
double getX(){
return x;}
double getY(){
return y;}
};
class myLine{
private: myPoint p1 , p2;
public:
myLine(){
}
myLine(myPoint a ,myPoint b){
p1=a;
p2=b;
}
double getLength(){
return sqrt(pow(p1.getX()-p2.getX(),2)+pow(p1.getY()-p2.getY(),2));
}
bool operator > (myLine l){
if(this->getLength()>l.getLength())return true;
return false;
}
bool operator < (myLine l){
if(this->getLength()<l.getLength())return true;
return false;
}
bool operator >= (myLine l){
if(this->getLength()>=l.getLength())return true;
return false;
}
bool operator <= (myLine l){
if(this->getLength()<=l.getLength())return true;
return false;
}
double slope(){
if(p2.getX()!=p1.getX()){
return ((p1.getY()-p2.getY())/(p1.getX()-p2.getX()));
}
}
double intercept(){
if(p2.getX()!=p1.getX()){
return (p2.getY()-(this->slope()*p2.getX()));
}
}
};
int main(){
myPoint p1,p2;
cin>>p1;
cin>>p2;
myLine l(p1,p2);
cout<<l.slope()<<" "<<l.intercept();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.