fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class DateOfBirth
  5. {
  6. public:
  7. DateOfBirth(int d, int m, int y) : day(d), month(m), year(y) {}
  8.  
  9. void displayDOB()
  10. {
  11. std::cout << day << "/" << month << "/" << year;
  12. }
  13. private:
  14. int day;
  15. int month;
  16. int year;
  17. };
  18.  
  19. class Person
  20. {
  21. public:
  22. Person(std::string n, DateOfBirth d) : DOB(d), name(n) {} //1
  23. Person(std::string n, int d, int m, int y) : DOB(d, m, y), name(n) {} //2
  24.  
  25. const std::string& getName() { return name; }
  26. DateOfBirth DOB;
  27. private:
  28. std::string name;
  29. };
  30.  
  31. int main()
  32. {
  33. Person person1("Person1", 1, 1, 1); //Calling (2)
  34. Person person2("Person2", DateOfBirth(2, 2, 2)); //Calling (1) using temporary
  35. Person person3("Person3", {3, 3, 3}); //Calling (1) using temporary
  36. }
  37.  
Success #stdin #stdout 0s 3268KB
stdin
Standard input is empty
stdout
Standard output is empty