Advertisement
muhata84

Day 4: Class vs. Instance

Jan 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3.  
  4. class Person {
  5. public:
  6. int age;
  7. Person(int initialAge);
  8. void amIOld();
  9. void yearPasses();
  10. };
  11.  
  12. Person::Person(int initialAge) {
  13. // Add some more code to run some checks on initialAge
  14. if (initialAge > 0) {
  15. age = initialAge;
  16.  
  17. } else {
  18. age = 0;
  19. cout << "Age is not valid, setting age to 0." << endl;
  20. }
  21. }
  22.  
  23. void Person::amIOld() {
  24. // Do some computations in here and print out the correct statement to the
  25. // console
  26. if (age < 13) {
  27. cout << "You are young." << endl;
  28. } else if (age >= 13 && age < 18) {
  29. cout << "You are a teenager." << endl;
  30. } else if (age >= 18) {
  31. cout << "You are old." << endl;
  32. }
  33. }
  34.  
  35. void Person::yearPasses() {
  36. // Increment the age of the person in here
  37. age += 1;
  38. }
  39.  
  40. int main(){
  41. int t;
  42. int age;
  43. cin >> t;
  44. for(int i=0; i < t; i++) {
  45. cin >> age;
  46. Person p(age);
  47. p.amIOld();
  48. for(int j=0; j < 3; j++) {
  49. p.yearPasses();
  50. }
  51. p.amIOld();
  52.  
  53. cout << '\n';
  54. }
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement