Question:
please reply only 30 minutes using concept of oop c++
Answer:
#include <iostream>
#include <math.h>
using namespace std;
class Point {
private:
double x,y,z;
public:
// required constructors
Point() {
x=y=z=0;
}
Point(double a,double b,double c) {
x=a;y=b;z=c;
}
// overloaded < operator
bool operator <(Point& d) {
double dist1=sqrt(x*x+y*y+z*z);
double dist2=sqrt(d.x*d.x+d.y*d.y+d.z*d.z);
if(dist1<dist2)
return true;
return false;
}
// overloaded > operator
bool operator >(Point& d) {
double dist1=sqrt(x*x+y*y+z*z);
double dist2=sqrt(d.x*d.x+d.y*d.y+d.z*d.z);
if(dist1>dist2)
return true;
return false;
}
// overloaded == operator
bool operator ==(Point& d) {
double dist1=sqrt(x*x+y*y+z*z);
double dist2=sqrt(d.x*d.x+d.y*d.y+d.z*d.z);
if(dist1==dist2)
return true;
return false;
}
};
//test them
int main() {
Point p1(1,1,1),p2(2,2,2),p3(1,1,1);
if( p1 < p2 ) {
cout << "p1 is less than p2 " << endl;
} else if(p1>p2) {
cout << "p1 is greater than p2 " << endl;
} else if(p1==p2)
{cout << "p1 is equal to p2 " << endl;}
if( p1 < p3 ) {
cout << "p1 is less than p3 " << endl;
} else if(p1>p3) {
cout << "p1 is greater than p3 " << endl;
} else if(p1==p3)
{cout << "p1 is equal to p3 " << endl;}
return 0;
}