Member Variables and Methods
Declare member variables, write member methods, and reference them through instances.
Members of a Class
A class can contain:
- Member variables (data, sometimes called fields)
- Member functions (methods)
- Nested types (typedefs, enums, classes)
- Static members (shared across all instances)
Declaring Member Variables
Declare them in the class body. Use C++11 in-class default initializers for cleanliness.
class Person {
std::string name_ = "unknown";
int age_ = 0;
public:
// ...
};All lessons in this course
- struct vs class When to Use Each
- Member Variables and Methods
- Constructors and Member Initializer Lists
- The this Pointer and Encapsulation