Group_Coder

How to implement Member functions in cpp.

Aug 8th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Person {
  5. private:
  6. std::string name;
  7. int age;
  8.  
  9. public:
  10. Person(const std::string& n, int a) : name(n), age(a) {}
  11.  
  12. void introduce() {
  13. std::cout << "Hello, I'm " << name << " and I'm " << age << " years old." << std::endl;
  14. }
  15.  
  16. void celebrateBirthday() {
  17. ++age;
  18. std::cout << "Happy Birthday! Now I'm " << age << " years old." << std::endl;
  19. }
  20. };
  21.  
  22. int main() {
  23. Person person1("Alice", 25);
  24. Person person2("Bob", 30);
  25.  
  26. person1.introduce();
  27. person2.introduce();
  28.  
  29. person1.celebrateBirthday();
  30. person2.celebrateBirthday();
  31.  
  32. person1.introduce();
  33. person2.introduce();
  34.  
  35. return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment