example1_basic_usage.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

class Person {
private:
    std::string name;
    int age;

public:
    Person(const std::string& n, int a) : name(n), age(a) {}

    void introduce() const {
        std::cout << "Hello, I'm " << name << " and I'm " << age << " years old." << std::endl;
    }

    void haveBirthday() {
        age++;
        std::cout << "Happy Birthday! " << name << " is now " << age << " years old." << std::endl;
    }
};

int main() {
    Person alice("Alice", 30);
    alice.introduce();
    alice.haveBirthday();
    return 0;
}
Back to class