codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ class methods

C++ class methods

Functions inside a class.

Define inside the class, or outside using ClassName::method.

Example code

#include <iostream>
using namespace std;
class Car {
public:
  int speed = 0;
  void fast() { speed += 10; }
  int get();
};
int Car::get() { return speed; }
int main() {
  Car c; c.fast(); c.fast();
  cout << c.get() << endl;
  return 0;
}

Run this code in the online editor

← C++ OOP, classes and objectsC++ constructors →