C++ Tutorial

C++ Object Oriented Programming

Objects bundle data and the functions that work on it. Classes are the blueprint.

Data and behavior together

So far a program has been variables in main and free functions that take those values. That works until related pieces scatter: a name in one variable, a score in another, a function that expects both in the right order.

Object oriented programming (OOP) groups those pieces. An object holds data and the functions that use that data. A class is the blueprint that names the type. You write the class once. Then you create as many objects of that type as you need.

Class vs object

The class is the plan. The object is a real value in memory. int is a type; int n = 3;makes a variable. Lamp is a type you define; Lamp desk; makes an object.

ClassObject
What it isA type you defineA value of that type
When it existsIn the source, as a blueprintWhen the program creates it
How manyOne definitionAs many as you create
Exampleclass LampLamp desk;
AnalogyThe cookie cutterA cookie

A tiny class

Lamp has one field, on, and one method, toggle. The method belongs to the class. You call it on an object with the dot operator. This program creates one lamp, flips it, and prints the new state.

Example

#include <iostream>
using namespace std;

class Lamp {
 public:
  bool on;

  void toggle() {
    on = !on;
  }
};

int main() {
  Lamp desk;
  desk.on = false;
  desk.toggle();
  if (desk.on) {
    cout << "on" << endl;
  } else {
    cout << "off" << endl;
  }
  return 0;
}

Compile this in /cpp/try. Call toggle twice and watch the printed state change.

The same idea without a class

You could keep a bool desk_on and a free function. The class version keeps the flag and the flip next to each other. When you add a second lamp, you do not invent a second pair of names. You create anotherLamp.

Example

#include <iostream>
using namespace std;

int main() {
  bool desk_on = false;
  desk_on = !desk_on;
  cout << desk_on << endl;
  return 0;
}

That works for one flag. It does not scale when a lamp also needs a brightness, a room name, and three operations. The class is the type that holds those together.

What the next chapters cover

  • Classes and objects — define a type, create two objects, use public members.
  • Methods — functions that live on the class and run with the dot operator.
  • Constructors — code that runs when an object is created, so fields start valid.
  • Access specifierspublic, private, and protected.

Later chapters add encapsulation, inheritance, and polymorphism. Those are still the same idea: a class names a type, objects are values of that type.

Keep classes small

For this tutorial a class has a few fields and a few methods. Do not pack a whole program into one type. Name the class after the thing it models: Lamp, Player, Point. Next: how to write a class and create objects from it.

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

A particle as an object

Mass and speed belong together. A class is the blueprint; an object is one particle. kinetic() uses the fields so you cannot pass the wrong mass to the wrong speed.

KE = ½ m v²

Example

#include <iostream>
using namespace std;

class Particle {
public:
  double mass;
  double speed;
  double kinetic() { return 0.5 * mass * speed * speed; }
};

int main() {
  Particle p;
  p.mass = 2.0;
  p.speed = 3.0;
  cout << "KE = " << p.kinetic() << " J" << endl;
  return 0;
}

Biology

A sample with a mass

A tube has an id and a mass. The object is that record. 0.250 g is a quarter of a gram — a small solid, not a beaker of water.

Example

#include <iostream>
using namespace std;

class Sample {
public:
  int id;
  double mass_g;
};

int main() {
  Sample s;
  s.id = 17;
  s.mass_g = 0.250;
  cout << "id " << s.id << ", " << s.mass_g << " g" << endl;
  return 0;
}

FAQ: C++ Object Oriented Programming

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++ oop 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++ oop in this C++ C++ lesson (C++ Object Oriented Programming).

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.