Advertisement
sheredega

C++ / Operator overloading

Nov 6th, 2022 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. class Spatiu_Vectorial {
  7.     float a;
  8.     float b;
  9.     float c;
  10. public:
  11.     Spatiu_Vectorial()  {
  12.         a=b=c=0;
  13.     }
  14.     Spatiu_Vectorial(float x, float y, float z) {
  15.         a=x; b=y; c=z;
  16.     }
  17.     void Afisare() {
  18.         cout << "(" << a << ", " << b << ", " << c << ")" << endl;
  19.     }
  20.     float Lungimea() {  
  21.         return sqrt(a*a+b*b+c*c);
  22.     }
  23.     friend Spatiu_Vectorial operator + (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2);
  24.     friend Spatiu_Vectorial operator - (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2);
  25.     friend Spatiu_Vectorial operator * (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2);
  26.     friend Spatiu_Vectorial operator / (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2);
  27.     friend float skalar(Spatiu_Vectorial &d1, Spatiu_Vectorial &d2);
  28.     friend float cosinus(Spatiu_Vectorial &d1, Spatiu_Vectorial &d2);
  29. };
  30.  
  31.  
  32. Spatiu_Vectorial operator + (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2) {
  33.     return Spatiu_Vectorial(d1.a + d2.a, d1.b + d2.b, d1.c + d2.c);
  34. }
  35. Spatiu_Vectorial operator - (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2) {
  36.     return Spatiu_Vectorial(d1.a - d2.a, d1.b - d2.b, d1.c - d2.c);
  37. }
  38. Spatiu_Vectorial operator * (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2) {
  39.     return Spatiu_Vectorial(d1.a * d2.a, d1.b * d2.b, d1.c * d2.c);
  40. }
  41. float skalar (Spatiu_Vectorial &d1, Spatiu_Vectorial &d2) {
  42.     return ((d1.a * d2.a) + (d1.b * d2.b) + (d1.c * d2.c));
  43. }
  44. float cosinus(Spatiu_Vectorial &d1, Spatiu_Vectorial &d2) {
  45.     return (skalar(d1, d2)) / (d1.Lungimea() * d2.Lungimea());
  46. }
  47.    
  48.    
  49. int main() {
  50.     Spatiu_Vectorial a(2, -3, 1), b(1, 4, 2), c, d;
  51.     c=a+b;
  52.     d=a-c;
  53.     a.Afisare();
  54.     b.Afisare();
  55.     c.Afisare();
  56.     d.Afisare();
  57.     cout << cosinus(a, b) << endl;
  58.     cout << cosinus(c, d) << endl;
  59.     return 0;
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement