C++ Classes & Objects: A Practical Guide with Code Examples
What is a Class?
A C++ class is a user‑defined data type that bundles data and the functions that operate on that data into a single entity. Think of it as a blueprint: it defines the shape and behaviour of objects that can be created from it.
Just as a house blueprint specifies the placement of doors, windows, and rooms, a class describes the layout of its data members and the operations that can manipulate them. Multiple houses (objects) can be built from the same blueprint.

In the illustration above, the single prototype produces two distinct houses with different features.
In this tutorial you’ll learn:
- What a class is and why it matters in C++
- Concrete class examples
- The role of
privateandpublicaccess modifiers - How to create objects (instantiation)
- Accessing data members safely
- Member functions inside and outside the class definition
- Constructors and destructors in action
Class Declaration
Declaring a class is straightforward: use the class keyword followed by the class name and enclose the body within curly braces.
Syntax
class className
{
// data members
// member functions
};
- className – the identifier for your class.
- Data members are typically declared as variables.
- Member functions provide behaviour.
Access Modifiers: private vs. public
Access modifiers control visibility of class members.
- private – members are only accessible within the class or its friends.
- public – members can be accessed from anywhere where the object is visible.
Creating Objects (Instantiation)
Objects are instances of a class. Declaring an object follows the same pattern as declaring a variable: className objectName;
Syntax
className objectName;
- className – the class from which the object is created.
- objectName – the identifier for the new object.
Instantiation allocates memory for the object and may trigger its constructor.
Accessing Data Members
Public data members are accessed using the dot (.) operator.
Example
#include <iostream>
using namespace std;
class Phone {
public:
double cost;
int slots;
};
int main() {
Phone Y6;
Phone Y7;
Y6.cost = 100.0;
Y6.slots = 2;
Y7.cost = 200.0;
Y7.slots = 2;
cout << "Cost of Huawei Y6 : " << Y6.cost << endl;
cout << "Cost of Huawei Y7 : " << Y7.cost << endl;
cout << "Number of card slots for Huawei Y6 : " << Y6.slots << endl;
cout << "Number of card slots for Huawei Y7 : " << Y7.slots << endl;
return 0;
}
Output:

Why Use Getters/Setters?
Accessing or modifying data directly can break encapsulation. Using getter and setter member functions provides controlled access.
Example with Encapsulation
#include <iostream>
using namespace std;
class ClassA {
public:
void setA(int val);
int getA() const;
private:
int a;
};
int ClassA::getA() const {
return a;
}
void ClassA::setA(int val) {
a = val;
}
int main() {
ClassA obj;
obj.setA(20);
cout << "Value of a is: " << obj.getA();
return 0;
}
Output:

Protected Members and Inheritance
Protected members are accessible within the class and by derived classes, enabling controlled inheritance.
Example
#include <iostream>
using namespace std;
class ParentClass {
protected:
int value;
};
class ChildClass : public ParentClass {
public:
void setId(int x) {
value = x;
}
void displayValue() {
cout << "Value is: " << value << endl;
}
};
int main() {
ChildClass c;
c.setId(21);
c.displayValue();
return 0;
}
Output:

Member Functions Inside vs. Outside the Class
Member functions can be defined inline within the class or declared inside and defined later using the scope resolution operator ::.
Example
#include <iostream>
#include <string>
using namespace std;
class Guru99 {
public:
string tutorial_name;
int id;
void printName();
void printId() {
cout << "Tutorial id is: " << id;
}
};
void Guru99::printName() {
cout << "Tutorial name is: " << tutorial_name;
}
int main() {
Guru99 guru;
guru.tutorial_name = 'C++';
guru.id = 1001;
guru.printName();
cout << endl;
guru.printId();
return 0;
}
Output:

Constructors and Destructors
Constructors initialise objects when they are created, while destructors clean up just before an object is destroyed.
Example
#include <iostream>
using namespace std;
class ClassA {
public:
ClassA() {
cout << "Class constructor called" << endl;
}
~ClassA() {
cout << "Class destructor called" << endl;
}
};
int main() {
ClassA a; // constructor called
int p = 1;
if (p) {
ClassA b; // constructor called, destructor called at end of block
}
// destructor for 'a' called here
return 0;
}
Output:

Key Takeaways
- C++ is fundamentally object‑oriented; classes are the building blocks.
- A class groups data and related functions, creating a reusable blueprint.
- Use
public,private, andprotectedto enforce encapsulation. - Instantiate objects with the class name followed by the object name.
- Access public members via the dot operator; use getters/setters for private data.
- Member functions can be defined inline or outside the class with
::. - Constructors initialise objects; destructors handle cleanup.
C Language
- C# Classes & Objects: Foundations for Robust OOP
- C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
- C++ Operators Explained: Types, Examples, and Sample Programs
- C++ Structs Explained with a Practical Example
- Mastering std::list in C++: Syntax, Functions & Practical Examples
- C++ Struct vs Class: Key Differences, Syntax, and Best Use Cases
- Understanding C# Interfaces: Definition, Example, and Practical Use
- C# Serialization & Deserialization: A Practical Example
- Understanding Java Classes and Objects: Clear Concepts, Practical Examples
- Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained