Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. class myInt{
  11. protected:
  12. int num;
  13. public:
  14. myInt(){}
  15. myInt(int num) : num(num) {}
  16. int get(){
  17. return num;
  18. }
  19. virtual void set(int num){
  20. this->num = num;
  21. }
  22. myInt operator+(myInt& rhs){
  23. return myInt(this->get() + rhs.get());
  24. }
  25. myInt operator+(myDouble& rhs){
  26. return myInt(this->get + rhs.getWh());
  27. }
  28.  
  29. };
  30.  
  31. class myInt99 : public myInt{
  32. public:
  33. myInt99() {}
  34. myInt99(int num){
  35. this->set(num);
  36. }
  37. virtual void set(int num){
  38. if (num > 99 || num < -99) throw("NOOOOOOOOOOO!");
  39. this->num = num;
  40. }
  41. };
  42.  
  43. class myDouble : public myInt, public myInt99{
  44. private:
  45. myInt whole;
  46. myInt99 frac;
  47. void cast(stringstream& ss){
  48. int a, b;
  49. char c;
  50. ss >> a >> c >> b;
  51. while (abs(b) > 99) b /= 10;
  52. whole.set(a);
  53. frac.set(b);
  54. }
  55.  
  56. public:
  57. myDouble() {}
  58. myDouble(string s){
  59. stringstream ss(s);
  60. cast(ss);
  61. }
  62. myDouble(double d){
  63. stringstream ss(d);
  64. cast(ss);
  65. }
  66. myDouble(myInt mI, myInt99 mI99){
  67. whole = mI;
  68. frac = mI99;
  69. }
  70. myDouble(int mI, int mI99){
  71. whole.set(mI);
  72. frac.set(mI99);
  73. }
  74.  
  75. myDouble operator+(myDouble& rhs){
  76. return myDouble(this->whole.get() + rhs.getWh(), this->frac.get() + rhs.getFr());
  77. }
  78.  
  79. myDouble operator+(myInt& rhs){
  80. return myDouble(this->whole.get() + rhs.get(), this->frac.get());
  81. }
  82.  
  83. virtual void set(double d){
  84. stringstream ss(d);
  85. cast(ss);
  86. }
  87. int getWh(){
  88. return this->whole.get();
  89. }
  90. int getFr(){
  91. return this->frac.get();
  92. }
  93. };
  94.  
  95. int main(){
  96. myInt a(2), b(3);
  97. myInt c(a + b);
  98. cout << c.get() << endl;
  99. system("pause");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement