Help solve, please.

Create a class of angles on a plane with data members angle value in degrees and minutes. Overload the input and output operators of the object, addition and subtraction of angles. Create member functions for converting an angle from degrees to radians and vice versa, calculating trigonometric functions. Create an example to demonstrate the capabilities of the class.

UPD: That's what I have: http://pastie.org/9328088

#include <iostream> #include <cmath> using namespace std; class Angle { static const double Pi; public: Angle(double deg = 0.0, double amin = 0.0): _angle(deg + amin / 60.0) {} double getAngle() const { return _angle; } double transInRad() const { return _angle * Pi / 180; } double transInDeg() const { return transInRad() * 180 / Pi; } double sin() const { return std::sin(transInRad()); } double cos() const { return std::cos(transInRad()); } double tan() const { return std::tan(transInRad()); } private: double _angle; }; const double Angle::Pi = 3.14159265358979323; int main() { Angle a1(25.0, 18.0); std::cout << "Угол:\n" << a1.getAngle() << "\n"; std::cout << "Переводим его в радианы:\n" << a1.transInRad() << "\n"; std::cout << "Затем обратно в градусы:\n" << a1.transInDeg() << "\n"; std::cout << "Вычисляем sin угла:\n" << a1.sin() << "\ncos:\n" << a1.cos() << "\ntg:\n" << a1.tan() << "\nctg:\n" << 1/a1.tan(); return 0; } 

I also need to "Overload the input and output operators of the object, addition and subtraction of angles."

  • @unitek, According to the rules of the forum, questions should not be reduced to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - BogolyubskiyAlexey
  • @BogolyubskiyAlexey and @Flammable, added. Look here please. - unitek

1 answer 1

You can read here, it seems accessible and detailed. .

Here are a couple of lines for your class:

 class Angle { ... friend ostream& operator<<(ostream& os, const Angle& dt); friend const Angle operator+(const Angle& left, const Angle& right); } ostream& operator<<(ostream& os, const Angle& dt) { os << "deg = " << dt.deg << ", dt.amin = " << amin; return os; } const Angle operator+(const Angle& left, const Angle& right) { return Angle(left.deg + right.deg, left.amin + right.amin); } // main() std::cout << "Angle: " << a1 << "\n"; // будет типа Angle: deg = 25, amin = 18 
  • @Gimka: Nah. You cannot define an ostream& operator<< using the member function (where did deg and amin come from?). - VladD
  • Yes, you are right, with the argument nakosyachil and did not write, and indeed the type left from copy-paste remained :) Also it is not clear that they are friendly. Corrected. - vinnie
  • @Gimka: Oh, another thing, +1. - VladD