0Pricing
C++ Academy · Lesson

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 — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 anywhere
  • private — accessible only inside the class
  • protected — 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.

Frequently asked questions

Is the “Member Variables and Methods” lesson free?

Yes — the full text of “Member Variables and Methods” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. struct vs class When to Use Each
  2. Member Variables and Methods
  3. Constructors and Member Initializer Lists
  4. The this Pointer and Encapsulation
← Back to C++ Academy