fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class PhanSo{
  6. private:
  7. int tu;
  8. int mau;
  9. public:
  10. //loi
  11. //chuyen kieu = constructor va dung template
  12.  
  13. template <typename T1>
  14. PhanSo(T1 t=0,T1 m=1);
  15. PhanSo(T1 t,T1 m){
  16. cout<<"Phan so da duoc tao\n";
  17. this->tu=(t);
  18. this->mau=(m);
  19. }
  20.  
  21.  
  22. //chuyen kieu = constructor ko dung template
  23. // PhanSo(int t=0,int m=1){
  24. // cout<<"Phan so da duoc tao\n";
  25. // this->tu=(t);
  26. // this->mau=(m);
  27. // }
  28. void set(); //ham nhap phan so
  29. void get(); // ham xuat phan so
  30.  
  31. // overloading operator
  32. friend istream &operator>>(istream &,PhanSo &);
  33. friend PhanSo operator+(const PhanSo &,const PhanSo &);
  34. friend PhanSo operator-(const PhanSo &,const PhanSo &);
  35. friend PhanSo operator*(const PhanSo &,const PhanSo &);
  36. friend PhanSo operator/(const PhanSo &,const PhanSo &);
  37. };
  38.  
  39. void PhanSo::set(){
  40. cin>>*this; // toan tu cin da duoc nap chong !!
  41.  
  42. }
  43. void PhanSo::get(){
  44. if(this->tu==0)
  45. cout<<"0\n";
  46. else if(this->mau==1 )
  47. cout<<this->tu<<endl;
  48. else if(this->mau==0)
  49. cout<<"Phan So khong hop le vi mau so bang khong!\n";
  50. else
  51. cout<<this->tu<<"/"<<this->mau<<endl;
  52. }
  53.  
  54. istream &operator>>(istream &in,PhanSo &ps){
  55. cout<<"Nhap tu so: ";
  56. in>>ps.tu;
  57. cout<<"Nhap mau so: ";
  58. in>>ps.mau;
  59. return in;
  60. }
  61.  
  62. PhanSo operator+(const PhanSo &c,const PhanSo &d){
  63. PhanSo kq;
  64. kq.tu=(c.tu*d.mau) + (d.tu*c.mau);
  65. kq.mau=c.mau*d.mau;
  66. return kq;
  67. }
  68.  
  69. PhanSo operator-(const PhanSo &a,const PhanSo &b){
  70. PhanSo kq;
  71. kq.tu=(a.tu*b.mau) - (b.tu*a.mau);
  72. kq.mau=a.mau*b.mau;
  73. return kq;
  74. }
  75.  
  76. PhanSo operator*(const PhanSo &a,const PhanSo &b){
  77. PhanSo kq;
  78. kq.tu=a.tu*b.tu;
  79. kq.mau=a.mau*b.mau;
  80. return kq;
  81. }
  82.  
  83. PhanSo operator/(const PhanSo &a,const PhanSo &b){
  84. PhanSo kq;
  85. kq.tu=(a.tu*b.mau) ;
  86. kq.mau=a.mau*b.tu;
  87. return kq;
  88. }
  89.  
  90. int main(){
  91. PhanSo x;
  92. PhanSo y;
  93. PhanSo z(4,5);
  94. cout<<"Khoi tao x!\n";
  95. x.set();
  96. x.get();
  97. cout<<"Khoi tao y!\n";
  98. y.set() ;
  99. y.get();
  100. cout<<"Khoi tao z: ";
  101. z.get();
  102. char ope; //bien luu phep toan muon tinh
  103. cout<<"Nhap phep toan muon tinh voi x va y(+,-,*,/): ";
  104. cin>>ope;
  105. switch (ope){
  106. case '+':{
  107. z=x+y;
  108. z.get();
  109. break;
  110. }
  111. case '-':{
  112. z=x-y;
  113. z.get();
  114. break;
  115. }
  116. case '*':{
  117. z=x*y;
  118. z.get();
  119. break;
  120. }
  121. case '/':{
  122. z=x/y;
  123. z.get();
  124. break;
  125. }
  126. }
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:15:11: error: expected ‘)’ before ‘t’
  PhanSo(T1 t,T1 m){
        ~  ^~
           )
prog.cpp: In function ‘PhanSo operator+(const PhanSo&, const PhanSo&)’:
prog.cpp:63:9: error: no matching function for call to ‘PhanSo::PhanSo()’
  PhanSo kq;
         ^~
prog.cpp:14:2: note: candidate: ‘template<class T1> PhanSo::PhanSo(T1, T1)’
  PhanSo(T1 t=0,T1 m=1);
  ^~~~~~
prog.cpp:14:2: note:   template argument deduction/substitution failed:
prog.cpp:63:9: note:   couldn't deduce template parameter ‘T1’
  PhanSo kq;
         ^~
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(const PhanSo&)’
 class PhanSo{
       ^~~~~~
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(PhanSo&&)’
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp: In function ‘PhanSo operator-(const PhanSo&, const PhanSo&)’:
prog.cpp:70:9: error: no matching function for call to ‘PhanSo::PhanSo()’
  PhanSo kq;
         ^~
prog.cpp:14:2: note: candidate: ‘template<class T1> PhanSo::PhanSo(T1, T1)’
  PhanSo(T1 t=0,T1 m=1);
  ^~~~~~
prog.cpp:14:2: note:   template argument deduction/substitution failed:
prog.cpp:70:9: note:   couldn't deduce template parameter ‘T1’
  PhanSo kq;
         ^~
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(const PhanSo&)’
 class PhanSo{
       ^~~~~~
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(PhanSo&&)’
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp: In function ‘PhanSo operator*(const PhanSo&, const PhanSo&)’:
prog.cpp:77:9: error: no matching function for call to ‘PhanSo::PhanSo()’
  PhanSo kq;
         ^~
prog.cpp:14:2: note: candidate: ‘template<class T1> PhanSo::PhanSo(T1, T1)’
  PhanSo(T1 t=0,T1 m=1);
  ^~~~~~
prog.cpp:14:2: note:   template argument deduction/substitution failed:
prog.cpp:77:9: note:   couldn't deduce template parameter ‘T1’
  PhanSo kq;
         ^~
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(const PhanSo&)’
 class PhanSo{
       ^~~~~~
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(PhanSo&&)’
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp: In function ‘PhanSo operator/(const PhanSo&, const PhanSo&)’:
prog.cpp:84:9: error: no matching function for call to ‘PhanSo::PhanSo()’
  PhanSo kq;
         ^~
prog.cpp:14:2: note: candidate: ‘template<class T1> PhanSo::PhanSo(T1, T1)’
  PhanSo(T1 t=0,T1 m=1);
  ^~~~~~
prog.cpp:14:2: note:   template argument deduction/substitution failed:
prog.cpp:84:9: note:   couldn't deduce template parameter ‘T1’
  PhanSo kq;
         ^~
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(const PhanSo&)’
 class PhanSo{
       ^~~~~~
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(PhanSo&&)’
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp: In function ‘int main()’:
prog.cpp:91:9: error: no matching function for call to ‘PhanSo::PhanSo()’
  PhanSo x;
         ^
prog.cpp:14:2: note: candidate: ‘template<class T1> PhanSo::PhanSo(T1, T1)’
  PhanSo(T1 t=0,T1 m=1);
  ^~~~~~
prog.cpp:14:2: note:   template argument deduction/substitution failed:
prog.cpp:91:9: note:   couldn't deduce template parameter ‘T1’
  PhanSo x;
         ^
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(const PhanSo&)’
 class PhanSo{
       ^~~~~~
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(PhanSo&&)’
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:92:9: error: no matching function for call to ‘PhanSo::PhanSo()’
  PhanSo y;
         ^
prog.cpp:14:2: note: candidate: ‘template<class T1> PhanSo::PhanSo(T1, T1)’
  PhanSo(T1 t=0,T1 m=1);
  ^~~~~~
prog.cpp:14:2: note:   template argument deduction/substitution failed:
prog.cpp:92:9: note:   couldn't deduce template parameter ‘T1’
  PhanSo y;
         ^
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(const PhanSo&)’
 class PhanSo{
       ^~~~~~
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:5:7: note: candidate: ‘constexpr PhanSo::PhanSo(PhanSo&&)’
prog.cpp:5:7: note:   candidate expects 1 argument, 0 provided
stdout
Standard output is empty