Object-Oriented Programming Lesson 12 of 17
C++ Classes and Objects
Define classes with member variables and member functions, create objects, use public and private, write const member functions, and split a class into .h and .cpp files.
A class is a blueprint that bundles data (member variables) and actions (member functions). Objects are the actual things created from it. Classes are the main idea C++ added to C.
A first class
#include <iostream>
#include <string>
using namespace std;
class Product {
public:
string name;
double price;
int stock;
void show() const {
cout << name << " - P" << price << " (" << stock << " left)" << endl;
}
void sell(int qty) {
if (qty > stock) {
cout << "Not enough " << name << "!" << endl;
return;
}
stock -= qty;
}
};
int main() {
Product kape;
kape.name = "Kape 3-in-1";
kape.price = 8;
kape.stock = 50;
kape.show();
kape.sell(12);
kape.sell(100);
kape.show();
return 0;
}
Output
Kape 3-in-1 - P8 (50 left) Not enough Kape 3-in-1! Kape 3-in-1 - P8 (38 left)
public:makes the members below it usable from outside the class.- Member functions like
show()andsell()can use the object's variables directly. constaftershow()promises that this function doesn't change the object.- Don't forget the semicolon after the closing brace of the class.
Many objects, one class
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
public:
string name;
vector<int> scores;
double average() const {
if (scores.empty()) return 0;
int sum = 0;
for (int s : scores) sum += s;
return static_cast<double>(sum) / scores.size();
}
};
int main() {
Student a;
a.name = "Ana";
a.scores = {90, 94, 88};
Student b;
b.name = "Ben";
b.scores = {78, 82};
for (const Student &s : {a, b}) {
cout << s.name << ": " << s.average() << endl;
}
return 0;
}
Output
Ana: 90.6667 Ben: 80
public and private
Making everything public lets anyone set stock = -500. Good classes keep data private and offer public functions that enforce the rules:
#include <iostream>
using namespace std;
class Counter {
private:
int count = 0; // default member value (C++11)
public:
void increment() { count++; }
void reset() { count = 0; }
int value() const { return count; }
};
int main() {
Counter visitors;
visitors.increment();
visitors.increment();
visitors.increment();
cout << "Visitors: " << visitors.value() << endl;
// visitors.count = 100; // error: 'count' is a private member
return 0;
}
Output
Visitors: 3
In a class, members are private by default. A struct is the same as a class except its members are public by default. Use struct for simple data bundles and class when there are rules to protect.
Defining functions outside the class
For bigger classes, declare functions inside the class and define them outside with ClassName:::
#include <iostream>
using namespace std;
class Rectangle {
public:
double width = 0, height = 0;
double area() const;
double perimeter() const;
};
double Rectangle::area() const { return width * height; }
double Rectangle::perimeter() const { return 2 * (width + height); }
int main() {
Rectangle room;
room.width = 9;
room.height = 7;
cout << "Area: " << room.area() << ", perimeter: " << room.perimeter() << endl;
return 0;
}
Output
Area: 63, perimeter: 32
Header and source files
In real projects, each class usually has two files:
// Rectangle.h (the declaration)
#pragma once
class Rectangle {
public:
double width = 0, height = 0;
double area() const;
};
// Rectangle.cpp (the definitions)
#include "Rectangle.h"
double Rectangle::area() const { return width * height; }
// main.cpp
#include <iostream>
#include "Rectangle.h"
int main() { Rectangle r; r.width = 3; r.height = 4; std::cout << r.area(); }
Compile them together: g++ main.cpp Rectangle.cpp -o app. #pragma once prevents the header from being included twice.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04