dimanche 2 août 2020

How to use overload operator as condition in a if statment?

Here is the class

#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;

class Point {
  protected:
    int x, y;
    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(){     
    this->x=x;
    this->y=y;
    this->radius=radius;
  }

  Circle(int x, int y, int radius){
this->x=x;
this->y=y;
this->radius=radius;
}
    void printCircleInfo() {
      cout << x << " " << y << " " << radius << " " ;
    }

This is the operator I want to be the condition in my if statement.

bool operator==(const Circle &def){ 
  return (x==def.x) & (y==def.y) & (radius==def.radius);
}
    bool doIBumpIntoAnotherCircle(Circle anotherCircle){
      if (anotherCircle.radius + radius >=   *this - anotherCircle    )
    return true;
      return false;
    }

};

Here is main

int main(){
  int x,y,radius;
  const int SIZE = 13;
  Circle myCircleArry[SIZE];
  myCircleArry[0] = Circle(5,3,9);
   cout << endl;
   myCircleArry[0].printCircleInfo(); cout << " ; ";
  ifstream Lab6DataFileHandle;

  Lab6DataFileHandle.open("Lab6Data.txt");
  while (!Lab6DataFileHandle.eof( )) {
 for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>x;
Lab6DataFileHandle>>y;
Lab6DataFileHandle>>radius;
 myCircleArry[i] = Circle(x,y,radius);

 if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
      myCircleArry[i].printCircleInfo(); cout << " ; ";

Here is the If statement

      if ( operator==( Circle &def))
 {cout <<"*";
}


  }
  }
}
  Lab6DataFileHandle.close();

}

How do I use the overloaded operator as the condition of the if statement? If you need any clarification just ask other wise please leave an example in your answer. Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire