example1_basic_class_instantiation.cpp

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

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

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

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

int main() {
    Person alice("Alice", 30);
    alice.introduce();

    return 0;
}
Back to class_instantiation