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;
}