C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
C++ Classes & Objects
Discover how to encapsulate data and behavior in C++ using classes and objects, with clear examples and best practices.
Why Use Classes?
In earlier lessons we explored functions and variables. As projects grow, grouping related data and operations becomes essential for maintainability and clarity. A class lets you bundle data members and member functions into a single logical unit, enabling object‑oriented programming (OOP)—the foundation of modern C++ design.
Illustration: A Room
Consider a rectangular room. You need to store its length, breadth, and height while also calculating its area and volume. With a class, you can keep these tightly coupled:
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() { return length * breadth; }
double calculateVolume() { return length * breadth * height; }
};
Defining a Class
A class is defined with the class keyword, followed by its name and a body enclosed in curly braces, ending with a semicolon:
class ClassName {
// data members
// member functions
};
In the example above, Room is a blueprint. The variables inside are called data members; the functions are member functions.
Creating Objects
Defining a class only specifies the layout; it doesn’t allocate memory. Objects are concrete instances created from that blueprint:
Room room1; // single object
Room room2, room3; // multiple objects in one declaration
Objects can be instantiated anywhere—inside functions, other classes, or globally—so long as their scope is respected.
Accessing Members
Use the dot (.) operator to call member functions or access data members:
room1.length = 5.5;
double area = room1.calculateArea();
Example 1: Calculating Room Metrics
#include <iostream>
using namespace std;
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() { return length * breadth; }
double calculateVolume() { return length * breadth * height; }
};
int main() {
Room room1;
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output
Area of Room = 1309 Volume of Room = 25132.8
Encapsulation: Public vs Private
Encapsulation protects data by controlling access. Members declared as private are hidden from the outside world and can only be accessed via public member functions.
class Test {
private:
int a;
void function1() {}
public:
int b;
void function2() {}
};
In the following example, data members are private and are initialized through a public method:
#include <iostream>
using namespace std;
class Room {
private:
double length;
double breadth;
double height;
public:
void initData(double len, double brth, double hgt) {
length = len; breadth = brth; height = hgt;
}
double calculateArea() { return length * breadth; }
double calculateVolume() { return length * breadth * height; }
};
int main() {
Room room1;
room1.initData(42.5, 30.8, 19.2);
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output
Area of Room = 1309 Volume of Room = 25132.8
Further Reading
For authoritative guidelines, consult the ISO/IEC 14882:2023 C++ Standard or trusted resources such as cplusplus.com and cppreference.com.
C Language
- Master C++ Inheritance: Build Powerful Classes with Reusable Code
- C++ Inheritance Models: Multiple, Multilevel, Hierarchical
- C++ Friend Functions and Friend Classes: Mastering Access Control
- Mastering Python Objects & Classes: A Practical Guide
- Java Classes and Objects: A Practical Guide
- C++ Structures vs Classes: A Practical Guide for Embedded Developers
- C++ Classes & Objects: A Practical Guide with Code Examples
- Master Java: Understanding Objects, Classes, and Core OOP Concepts
- Understanding Storage Classes in C++: Scope, Lifetime, and Usage
- C++ Interfaces: Mastering Abstract Classes & Pure Virtual Functions