Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Person {
- private:
- string name;
- int age;
- public:
- // Constructor
- Person(string n, int a) : name(n), age(a) {}
- // Setter methods
- void setName(string n) {
- name = n;
- }
- void setAge(int a) {
- age = a;
- }
- // Getter methods
- string getName() {
- return name;
- }
- int getAge() {
- return age;
- }
- // Display method
- void displayInfo() {
- cout << "Name: " << name << ", Age: " << age << " years old" << endl;
- }
- };
- int main() {
- Person person1("Alice", 25);
- Person person2("Bob", 30);
- // Display initial information
- cout << "Initial Information:" << endl;
- person1.displayInfo();
- person2.displayInfo();
- // Modify and display updated information
- person1.setName("Carol");
- person1.setAge(28);
- person2.setAge(35);
- cout << "\nUpdated Information:" << endl;
- person1.displayInfo();
- person2.displayInfo();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement