C++.Создать класс типа круг. Поля-данные: радиус, координаты центра. Функции-члены
вычисляют площадь, длину круга, устанавливают поля и возвращают
значение. Функции-члены установки полей класса должны проверять
корректность параметров, которые задаются (не равны нулю и не отрицательны). Функция
печати.
Answers & Comments
Ответ:
#define M_PI 3.14159265358979323846
#include <iostream>
#include <format>
class Circle {
float Radius;
std::pair<float, float> Coordinates;
public:
Circle(float radius, float x, float y) {
if (radius <= 0) {
std::cout << "Incorrect variables. Exitting...\n";
exit(1);
}
this->Radius = radius;
this->Coordinates.first = x;
this->Coordinates.second = y;
}
void SetRaduis(float rad) {
if(rad <= 0)
return;
this->Radius = rad;
}
float GetRadius() {return this->Radius;}
void SetX(float X) {this->Coordinates.first = X;}
float GetX() {return this->Coordinates.first;}
void SetY(float Y) {this->Coordinates.second = Y;}
float GetY() {return this->Coordinates.second;}
float Square() {return M_PI * this->Radius * this->Radius;}
float Length() {return 2*M_PI*this->Radius;}
void Print() {
std::cout << std::format("Radius: {}; X: {}, Y: {}; Square: {}; Length: {};\n", this->Radius,
this->Coordinates.first,
this->Coordinates.second,
this->Square(),
this->Length());
}
};
Объяснение:
Будьте осторожны, данный код написан на С++ последней версии от Microsoft(флаг компилятора /std:c++latest). Достаточно переделать функцию печатания, чтобы запускалось на С++14.