fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct NgaySinh
  8. {
  9. int d, m, y;
  10. };
  11.  
  12. class Nguoi
  13. {
  14. private:
  15. string ten;
  16. NgaySinh ngaySinh;
  17. string diaChi;
  18. public:
  19. Nguoi();
  20. Nguoi(string hoTen, int d, int m, int y, string diaChi);
  21. int tinhTuoi();
  22. void nhap();
  23. void xuat();
  24. ~Nguoi();
  25. };
  26.  
  27. Nguoi::Nguoi(): ten(""), ngaySinh({1,1,2000}), diaChi(""){}
  28.  
  29. Nguoi::Nguoi(string hoTen, int d, int m, int y, string diaChi)
  30. : ten(hoTen), ngaySinh({d, m, y}), diaChi(diaChi) { }
  31.  
  32. int Nguoi::tinhTuoi()
  33. {
  34. return 2025 - ngaySinh.y;
  35. }
  36.  
  37. void Nguoi::nhap()
  38. {
  39. cout << "Nhap ten: ";
  40. getline(cin, ten);
  41. cout << "Nhap ngay, thang, nam: ";
  42. cin >> ngaySinh.d >> ngaySinh.m >> ngaySinh.y;
  43. cin.ignore();
  44. //fflush(stdin);
  45. cout << "Nhap dia chi: ";
  46. getline(cin, diaChi);
  47. }
  48.  
  49. void Nguoi::xuat()
  50. {
  51. cout << "Ten: " << ten << endl;
  52. cout << "Ngay sinh: " << ngaySinh.d << "/" << ngaySinh.m << "/" << ngaySinh.y << endl;
  53. cout << "Dia chi: " << diaChi << endl;
  54. cout << "Tuoi: " << tinhTuoi() << " tuoi" << endl;
  55. }
  56.  
  57. Nguoi::~Nguoi()
  58. {
  59.  
  60. }
  61.  
  62. int main()
  63. {
  64. Nguoi a;
  65. a.nhap();
  66. a.xuat();
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Nhap ten: Nhap ngay, thang, nam: Nhap dia chi: Ten: 
Ngay sinh: 1/1/2000
Dia chi: 
Tuoi: 25 tuoi