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.
| Class | Object | |
|---|---|---|
| What it is | A type you define | A value of that type |
| When it exists | In the source, as a blueprint | When the program creates it |
| How many | One definition | As many as you create |
| Example | class Lamp | Lamp desk; |
| Analogy | The cookie cutter | A 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
publicmembers. - 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 specifiers —
public,private, andprotected.
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;
}