Advertisement
ksendzi

MojBroj

Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. main :
  2.  
  3. #include <iostream>
  4. #include "broj.h"
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. MojBroj b1(), b2(2), b3(3);
  10.  
  11. cout << b1 << endl;
  12. cout << b2 << endl;
  13. cout << b3 << endl;
  14.  
  15. }
  16.  
  17. header:
  18.  
  19. #ifndef BROJ_H_INCLUDED
  20. #define BROJ_H_INCLUDED
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. class MojBroj
  25. {
  26. private:
  27. double vrednost;
  28. public:
  29. MojBroj();
  30. MojBroj(double );
  31. MojBroj(const MojBroj &);
  32. void setVrednost(double );
  33. //double getVrednost() const {}
  34.  
  35. friend MojBroj operator + (const MojBroj &, const MojBroj &);
  36. friend MojBroj operator - (const MojBroj &, const MojBroj &);
  37. friend MojBroj operator * (const MojBroj &, const MojBroj &);
  38. friend MojBroj operator / (const MojBroj &, const MojBroj &);
  39.  
  40. friend MojBroj operator += (const MojBroj &, const MojBroj &);
  41. friend MojBroj operator -= (const MojBroj &, const MojBroj &);
  42. friend MojBroj operator *= (const MojBroj &, const MojBroj &);
  43. friend MojBroj operator /= (const MojBroj &, const MojBroj &);
  44.  
  45. friend ostream& operator<<(ostream& , const MojBroj &);
  46. };
  47.  
  48. #endif // BROJ_H_INCLUDED
  49.  
  50. #include "broj.h"
  51.  
  52. MojBroj::MojBroj()
  53. {
  54. vrednost = 1;
  55. }
  56. MojBroj::MojBroj(double v)
  57. {
  58. vrednost = v;
  59. }
  60. MojBroj::MojBroj(const MojBroj & v)
  61. {
  62. vrednost=v.vrednost;
  63. }
  64.  
  65. MojBroj operator +(const MojBroj &b1, const MojBroj &b2)
  66. {
  67. MojBroj rez(b1.vrednost+b2.vrednost);
  68. return rez;
  69. }
  70. MojBroj operator -(const MojBroj &b1, const MojBroj &b2)
  71. {
  72. MojBroj rez(b1.vrednost-b2.vrednost);
  73. return rez;
  74. }
  75. MojBroj operator *(const MojBroj &b1, const MojBroj &b2)
  76. {
  77. MojBroj rez(b1.vrednost*b2.vrednost);
  78. return rez;
  79. }
  80. MojBroj operator /(const MojBroj &b1, const MojBroj &b2)
  81. {
  82. MojBroj rez(b1.vrednost/b2.vrednost);
  83. return rez;
  84. }
  85. MojBroj operator +=(const MojBroj &b1, const MojBroj &b2)
  86. {
  87.  
  88. return b1+=b2;
  89. }
  90. MojBroj operator -=(const MojBroj &b1, const MojBroj &b2)
  91. {
  92.  
  93. return b1-=b2;
  94. }
  95. MojBroj operator *=(const MojBroj &b1, const MojBroj &b2)
  96. {
  97.  
  98. return b1*=b2;
  99. }
  100. MojBroj operator /=(const MojBroj &b1, const MojBroj &b2)
  101. {
  102.  
  103. return b1/=b2;
  104. }
  105.  
  106. ostream& operator << (ostream &out, const MojBroj &br)
  107. {
  108. out << br.vrednost ;
  109. return out;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement