Salve a tutti, ho un problema con questo esercizio che ho svolto in C++ e richiede il calcolo dell'area, del perimetro e del centro di un rettangolo. Per l'area e il perimetro tutto ok, solo che per il centro non so se ho fatto bene a scrivere il costruttore Point Center e insomma quelle tre righe di codice che ho commentato con "?????" all'interno della class "Rectangle". Volevo sapere dove č l'errore o se ho sbagliato proprio a scrivere in quel modo...Inoltre cosa dovrei mettere nel main per stampare il risultato del centro?
codice:
class Point { private:
float x;
float y;
public:
Point(float x_,float y_) { // costruttore
x=x_;
y=y_;
}
float GetX() { return x; } // getter
float GetY() { return y; } // getter
}
Point() { // costruttore di default
x=0;
y=0;
}
};
class Rectangle {
private:
Point anchor;
float width;
float height;
public:
Rectangle(const Point& anchor_,const float width_,const float height_) {
anchor=anchor_;
width=width_;
height=height_;
}
float Area() {
return(width*height);
}
float Perimeter() {
return((width+height)*2);
}
Point Center() { // ?????
Point center(anchor.GetX()+0.5*width,anchor.GetY()+0.5*height); // ?????
return center; // ?????
}
};
int main() {
Point p(3,4);
Rectangle rect(p,10,4);
printf("%f %f%\n",rect.Area(),rect.Perimeter());
return 0;
}
Grazie in anticipo!