Member Variables and Methods
Declare member variables, write member methods, and reference them through instances.
Member Variables and Methods is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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:
// ...
};Naming Conventions
Common conventions for private members:
- Trailing underscore:
name_ - Leading
m_:m_name - Plain names with implicit
this->
Pick one and stay consistent.
Defining Methods Inside the Class
Short methods can be defined inline in the class body — and are implicitly inline.
class Counter {
int n_ = 0;
public:
void increment() { ++n_; }
int value() const { return n_; }
};Defining Methods Outside the Class
Longer methods are usually defined outside, often in a .cpp file. Use the scope resolution operator ::.
// header
class Logger {
public:
void log(const std::string& msg);
};
// implementation
void Logger::log(const std::string& msg) {
std::cout << "[LOG] " << msg << "\n";
}Calling Methods
Use dot syntax on objects and arrow syntax on pointers.
Counter c;
c.increment();
Counter* p = &c;
p->increment();Const Methods
Methods that do not modify the object should be marked const. They can be called on const instances.
class Counter {
int n_ = 0;
public:
int value() const { return n_; } // does not modify *this
};Static Members
Static members belong to the class, not to instances. Declare with static; define in a source file.
class Logger {
public:
static int messageCount;
static void log(const std::string& msg);
};
int Logger::messageCount = 0;Access Specifiers
Three access levels:
public— accessible from anywhereprivate— accessible only inside the classprotected— accessible inside the class and derived classes
Encapsulation in Practice
Make data members private. Expose them through methods that enforce invariants.
class Temperature {
double celsius_ = 0.0;
public:
void setCelsius(double c); // validates range
double celsius() const { return celsius_; }
double fahrenheit() const { return celsius_ * 9.0/5.0 + 32; }
};Methods and Free Functions
Not everything must be a method. If a function uses only the public interface, prefer a free function — it reduces coupling.
Quick Check
What keyword marks a member function as not modifying the object?
Recap
Member variables hold per-instance state; methods operate on that state. Mark non-modifying methods const. Make data private and expose behavior through a small, intentional public interface.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Member Variables and Methods” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Member Variables and Methods”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Member Variables and Methods”?
Declare member variables, write member methods, and reference them through instances. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Member Variables and Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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