C++ Tutorial

C++ Class Methods

Methods are functions that belong to a class. Call them on an object with the dot operator.

Functions that belong to a class

A method is a function written inside a class. It can use the object's fields without you passing them as extra arguments. You call it on an object: c.bump(). The object before the dot is the one the method works on.

Free functions live outside any class. Methods live on a type. That is how a Counter canbump and show without a pile of loose functions that all take a counter as the first parameter.

Write the method in the class body

For beginners, define the method inside the class. The compiler treats that as an inline member function. You can also declare inside and define outside with Counter::bump; skip that until the class is too large to read in one block. Keep the class small.

Example

#include <iostream>
using namespace std;

class Counter {
 public:
  int value;

  void bump() {
    value = value + 1;
  }
};

int main() {
  Counter c;
  c.value = 0;
  c.bump();
  cout << c.value << endl;
  return 0;
}

Call with the dot

c.bump() runs bump on object c. Inside bump, the namevalue means c.value for that call. If you later have two counters, each method call uses that object's own value.

Example

#include <iostream>
using namespace std;

class Counter {
 public:
  int value;

  void bump() {
    value = value + 1;
  }

  void show() {
    cout << value << endl;
  }
};

int main() {
  Counter c;
  c.value = 0;
  c.bump();
  c.bump();
  c.show();
  return 0;
}

Try it in /cpp/try. Add a second object d, set d.value = 10, calld.bump(), and print both. The two counters stay independent.

Methods can take parameters

A method is still a function. It can take arguments and return a value. The object's fields are available in addition to those parameters. add below increases value by n.

Example

#include <iostream>
using namespace std;

class Counter {
 public:
  int value;

  void add(int n) {
    value = value + n;
  }

  int get() {
    return value;
  }
};

int main() {
  Counter c;
  c.value = 2;
  c.add(5);
  cout << c.get() << endl;
  return 0;
}

The object is implied

Inside a method you can write this to mean a pointer to the current object. Beginners do not need it for these examples. The field names already refer to that object. Use this later when a parameter has the same name as a field, or when you pass the object onward.

Call methods on an object, not on the class name. c.bump() is correct.Counter.bump() is not, unless you later learn static methods.

Set fields before you call

bump reads value. If you never assigned value, you are reading garbage. Either set the field in main, or use a constructor so the object starts valid. Next: constructors.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Physics

Weight from mass

W = m g. A 70 kg person “weighs” 687 N on Earth. The method uses the object’s mass so you call person.weight() instead of passing 70 into a free function and hoping it was the right person.

W = m g

Weight is a force. Bathroom scales that say “70 kg” are reporting mass, not newtons.

Example

#include <iostream>
using namespace std;

class Body {
  double mass;
public:
  Body(double m) : mass(m) {}
  double weight() { return mass * 9.81; }
};

int main() {
  Body person(70.0);
  cout << "weight = " << person.weight() << " N" << endl;
  return 0;
}

Maths

Rectangle area as a method

A = w × h. For 4 by 3 that is 12. The method lives on the rectangle, which is where the two sides already are.

A = w × h

Example

#include <iostream>
using namespace std;

class Rect {
  double w, h;
public:
  Rect(double width, double height) : w(width), h(height) {}
  double area() { return w * h; }
};

int main() {
  Rect r(4.0, 3.0);
  cout << "area = " << r.area() << endl;
  return 0;
}

FAQ: C++ Class Methods

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ class methods examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ class methods in this C++ C++ lesson (C++ Class Methods).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.