Advertisement
SkidScripts

Untitled

Nov 29th, 2023
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Person {
  7. private:
  8.     string name;
  9.     int age;
  10. public:
  11.     // Constructor
  12.     Person(string n, int a) : name(n), age(a) {}
  13.  
  14.     // Setter methods
  15.     void setName(string n) {
  16.         name = n;
  17.     }
  18.  
  19.     void setAge(int a) {
  20.         age = a;
  21.     }
  22.  
  23.     // Getter methods
  24.     string getName() {
  25.         return name;
  26.     }
  27.  
  28.     int getAge() {
  29.         return age;
  30.     }
  31.  
  32.     // Display method
  33.     void displayInfo() {
  34.         cout << "Name: " << name << ", Age: " << age << " years old" << endl;
  35.     }
  36. };
  37.  
  38. int main() {
  39.     Person person1("Alice", 25);
  40.     Person person2("Bob", 30);
  41.  
  42.     // Display initial information
  43.     cout << "Initial Information:" << endl;
  44.     person1.displayInfo();
  45.     person2.displayInfo();
  46.  
  47.     // Modify and display updated information
  48.     person1.setName("Carol");
  49.     person1.setAge(28);
  50.     person2.setAge(35);
  51.  
  52.     cout << "\nUpdated Information:" << endl;
  53.     person1.displayInfo();
  54.     person2.displayInfo();
  55.  
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement