fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class HH{
  4. float d,r,h;
  5. public:
  6. friend istream&operator>>(istream&x,HH&y);
  7. friend ostream&operator<<(ostream&x,HH y);
  8. HH(){
  9. d=h=r=0;
  10. }
  11. HH(float a,float b,float c){
  12. d=a;
  13. r=b;
  14. h=c;
  15. }
  16. bool operator==(HH y);
  17. HH operator+(HH y);
  18. HH operator-(HH y);
  19. };
  20. istream&operator>>(istream&x,HH &y){
  21. cout<<"nhap chieu dai :"; x>>y.d;
  22. cout<<"nhap chieu rong: "; x>>y.r;
  23. cout<<"nhap chieu cao: "; x>>y.h;
  24. return x;
  25. }
  26. ostream&operator<<(ostream&x,HH y){
  27. x<<"Chieu dai: "<<y.d<<endl;
  28. x<<"Chieu rong: "<<y.r<<endl;
  29. x<<"Chieu cao: "<<y.h<<endl;
  30. return x;
  31. }
  32. bool HH::operator==(HH y){
  33. if(y.d==d&&y.r==r)
  34. return true;
  35. else return false;
  36. }
  37. HH HH::operator+(HH y){
  38. HH c;
  39. c.d=d+y.d;
  40. c.r=r+y.r;
  41. c.h=h+y.h;
  42. return c;
  43. }
  44. HH HH::operator-(HH y){
  45. HH c;
  46. c.d=d-y.d;
  47. c.r=r-y.r;
  48. c.h=h-y.h;
  49. return c;
  50. }
  51. int main(){
  52. HH h1(2,3,5);
  53. cout<<"Thong tin hh1: "<<endl;
  54. cout<<h1<<endl;
  55. HH h2;
  56. cout<<"nhap thong tin hh2: "<<endl;
  57. cin>>h2;
  58. HH T=h1+h2;
  59. HH H=h1-h2;
  60. ofstream f("hh.txt");
  61. cout<<"Tong 2 hh : "<<T<<endl;
  62. f<<"Tong 2 hh : "<<T<<endl;
  63. cout<<"Hieu 2 hh:"<<H<<endl;
  64. f<<"Hieu 2 hh:"<<H<<endl;
  65. if(h1==h2==true){
  66. cout<<"2 hh co cung chieu dai va chieu rong!"<<endl;
  67. f<<"2 hh co cung chieu dai va chieu rong!"<<endl;
  68. }
  69. else{
  70. cout<<"2 k cung chieu dai va chieu rong!"<<endl;
  71. f<<"2 k cung chieu dai va chieu rong!"<<endl;
  72. }
  73. f.close();
  74. return 0;
  75. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Thong tin hh1: 
Chieu dai: 2
Chieu rong: 3
Chieu cao: 5

nhap thong tin hh2: 
nhap chieu dai :nhap chieu rong: nhap chieu cao: Tong 2 hh : Chieu dai: 2
Chieu rong: 3
Chieu cao: 5

Hieu 2 hh:Chieu dai: 2
Chieu rong: 3
Chieu cao: 5

2 k cung chieu dai va chieu rong!