有没有办法使用重载运算符作为比较的一部分
Is there a way to use an overload operator as part of a comparison
Here is my class
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;
class Point {
protected:
int x, y;
Here is the overload operater I want to use it compares the difrence between two points.
double operator-(const Point &def){
return sqrt(pow((x-def.x),2.0)+
pow((y-def.y),2.0));
}
};
class Circle: public Point {
private:
int radius;
public:
Circle(){ //Point default const called implicitly
this->x=x;
this->y=y;
this->radius=radius;
}
void printCircleInfo() {
cout << x << " " << y << " " << radius << " " ;
}
bool operator=(const Circle &def){
return (x==def.x) & (y==def.y) & (radius==def.radius);
}
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
here I want to use the overloaded operater to compare the distance between the two points to the combined radius of two circles.
if (anotherCircle.radius + radius >= operator-( Point def) )
return true;
return false;
}
};
int main(){
const int SIZE = 13;
Circle myCircleArry[SIZE] = { 5,3,9};
;
cout << myCircleArry[0] <<":";
ifstream Lab6DataFileHandle;
Lab6DataFileHandle.open("Lab6Data.txt");
while (!Lab6DataFileHandle.eof( )) {
for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>myCircleArry[i];
Lab6DataFileHandle>>myCircleArry[i];
Lab6DataFileHandle>>myCircleArry[i];
cout << endl;
if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
myCircleArry[i].printCircleInfo(); cout << " ; ";
If double operator=(const Point &def)}
{cout <<"*"
}
}
}
Lab6DataFileHandle.close();
}
}
How do I use my previously created overload operator as part of my bool function doIBumpIntoAnotherCircle? Please leave an example in your answer it would be much appreciated. Thank you for your time.
是的,您可以直接使用继承自 Point
的 operator-
,如下所示:
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
if (anotherCircle.radius + radius >= *this - anotherCircle)
return true;
return false;
}
或者更简单地说:
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
return anotherCircle.radius + radius >= *this - anotherCircle;
}
此外,这个函数应该被标记为const
,并且应该以const&
作为参数,像这样:
bool doIBumpIntoAnotherCircle(Circle const &anotherCircle) const {
Here is my class
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;
class Point {
protected:
int x, y;
Here is the overload operater I want to use it compares the difrence between two points.
double operator-(const Point &def){
return sqrt(pow((x-def.x),2.0)+
pow((y-def.y),2.0));
}
};
class Circle: public Point {
private:
int radius;
public:
Circle(){ //Point default const called implicitly
this->x=x;
this->y=y;
this->radius=radius;
}
void printCircleInfo() {
cout << x << " " << y << " " << radius << " " ;
}
bool operator=(const Circle &def){
return (x==def.x) & (y==def.y) & (radius==def.radius);
}
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
here I want to use the overloaded operater to compare the distance between the two points to the combined radius of two circles.
if (anotherCircle.radius + radius >= operator-( Point def) )
return true;
return false;
}
};
int main(){
const int SIZE = 13;
Circle myCircleArry[SIZE] = { 5,3,9};
;
cout << myCircleArry[0] <<":";
ifstream Lab6DataFileHandle;
Lab6DataFileHandle.open("Lab6Data.txt");
while (!Lab6DataFileHandle.eof( )) {
for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>myCircleArry[i];
Lab6DataFileHandle>>myCircleArry[i];
Lab6DataFileHandle>>myCircleArry[i];
cout << endl;
if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
myCircleArry[i].printCircleInfo(); cout << " ; ";
If double operator=(const Point &def)}
{cout <<"*"
}
}
}
Lab6DataFileHandle.close();
}
}
How do I use my previously created overload operator as part of my bool function doIBumpIntoAnotherCircle? Please leave an example in your answer it would be much appreciated. Thank you for your time.
是的,您可以直接使用继承自 Point
的 operator-
,如下所示:
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
if (anotherCircle.radius + radius >= *this - anotherCircle)
return true;
return false;
}
或者更简单地说:
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
return anotherCircle.radius + radius >= *this - anotherCircle;
}
此外,这个函数应该被标记为const
,并且应该以const&
作为参数,像这样:
bool doIBumpIntoAnotherCircle(Circle const &anotherCircle) const {