Advertisement
Guest User

Untitled

a guest
May 25th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int currentYear = 2008;
  4.  
  5. class Person {
  6. public:
  7. struct
  8. {
  9. operator int ()
  10. {
  11. return currentYear - ((Person*)this)->yearOfBirth;
  12. }
  13. int operator= (int value)
  14. {
  15. ((Person*)this)->yearOfBirth = currentYear - value;
  16. return value;
  17. }
  18. } age;
  19. int yearOfBirth;
  20. Person()
  21. {
  22. yearOfBirth = 1989;
  23. }
  24. void PrintYearOfBirth(void) {
  25. printf("Year of birth: %d\n", yearOfBirth);
  26. }
  27. };
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31. Person person;
  32.  
  33. printf("Age: %d\n", (int)person.age);
  34. person.PrintYearOfBirth();
  35.  
  36. person.age = 21;
  37.  
  38. printf("Age: %d\n", (int)person.age);
  39. person.PrintYearOfBirth();
  40.  
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement