M
M
Matvey Penkov2017-06-25 17:34:44
OOP
Matvey Penkov, 2017-06-25 17:34:44

C++ Classes, I need to compare two objects of the same class and display the result on the screen, how to do it?

a method that compares two circles by the length or by the area of ​​the circle at the user's choice (an object of the "Circle" class is taken as a parameter).

My code:
#include "stdafx.h"
#include <iostream>
using namespace std;

class Circles // имя класса
{
private: // спецификатор доступа private
  int circuit; // координаты
  int X, Y; // установка координат
  float rad; // радиус
  float leng; // длина
  float place; // площадь
  
public: // спецификатор доступа public
  float plosh, lnght;
  float pi = 3.14;
  int x,y;
  int radx, rady;

  void message() // функция (метод класса) выводящая сообщение на экран
  {
    cout << "\n Инициализация данных... \n";
  }
  void set_circuit (int coordX, int coordY) // установка координат по Х и по У
  {
    X = coordX; // инициализация X
    Y = coordY; // инициализация Y
  }
  void get_circuit() // отобразить текущую дату
  {
    cout << "х: " << X << "   у:" << Y << "\n" << endl;
  }
  void set_rad(float circ_rad) {
    rad = circ_rad; // установка радиуса
  }
  void get_rad() {
    cout << "Радиус : "<< rad << "\n" << endl;
  }
  void get_leng() {
     lnght = leng;
  }
  void get_place() {
     plosh = place;
  }
  void calc_leng() {
    lnght = rad * 2 * pi;
    cout << "Длина окружности: " << lnght << "\n"<< endl;
  }
  void calc_place() {
    plosh = rad*pi*rad;
    cout << "Площадь окружности: " << plosh << "\n" << endl;
  }
  Circles():leng(lnght){}
  int getLeng() {
    return leng;
  }
}; // конец объявления класса CppStudio

int main(int argc, char* argv[])
{
  setlocale(LC_ALL, "rus"); // установка локали
  float X, Y, X2, Y2;
  int rad1, rad2;
  
  cout << "Введите координаты первой окружности \n";
  cout << "По Х: ";     cin >> X;
  cout << "По У: ";    cin >> Y;
  cout << "\nВведите радиус первой окружности: "; cin >> rad1;
  
  cout << "\nВведите координаты второй окружности \n";
  cout << "По Х: "; cin >> X2;
  cout << "По У: "; cin >> Y2;
  cout << "Введите радиус второй окружности: "; cin >> rad2;
  Circles circOne; // объявление объекта
  circOne.message(); // вызов функции класса message
  cout << "Координаты первой окружности:\n ";
  circOne.set_circuit (X, Y); // инициализация координат
  circOne.get_circuit(); // отобразить координаты
  circOne.set_rad(rad1);
  circOne.get_rad();
  
  cout << "Координаты второй окружности:\n ";
  Circles circTwo;
  circTwo.set_circuit(X2, Y2);
  circTwo.get_circuit();
  circTwo.set_rad(rad2);
  circTwo.get_rad();
  system("pause");
  return 0;
}

little things - comparison

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arseniy Efremov, 2017-06-25
@smoky__mo

Eh. In general, you do not need to know the circumference or area for comparison. It is enough to know that the circles have the same radii.
And therefore, there are a couple of tips (and let this not be true C ++ yet)
1. Making get_XXXX methods of type void is a very strange approach. Not only does a class with such specifics hardly need to know about the logic of output to the console, it also violates the get semantics in principle. Therefore, from such methods it is better to return simply the value of the corresponding field.
2. To add some comparison logic, you can add this or a similar method:

public: bool equals_to(const Circles& right){
    return rad == right.rad; // обычно сравнивать float так не очень хорошо, но пусть будет
}

T
tomatho, 2017-06-25
@tomatho

How? Decisively, swiftly!
If you wrote this code, then it will not be difficult.
If this task is in the course of lectures, then I do not intend to help portray a false understanding.

D
devalone, 2017-06-25
@devalone

Define a comparison operator en.cppreference.com/w/cpp/language/operators and compare, and few people want to read your sheet of unformatted code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question