If you’re new to programming, learning C++ can seem intimidating. But don’t worry you’re not alone! You might notice that C++ has two types of data structures (structs) and classes (classes).At first glance, they might seem similar. After all, both can contain variables and functions. It’s important to understand how they behave under the hood so that you can write clean, professional C++ code.
What is a Structure in C++?
A structure (declared using struct) is a simple way to group related data together. Think of it like a file folder it keeps different but related papers (data) in one place. In a struct, all members are public by default meaning they can be accessed from anywhere.
In C++, a structure can hold variables (called data members) and even simple functions.
Real-life example:
Imagine a student record. Each student has a:
- Roll number
- Name
- Marks
All these pieces of information can be grouped using a struct.
#include <iostream> // This allows us to use input/output functions like cout using namespace std; // So we don’t have to write std:: before every C++ keyword // Define a structure named 'Student' to group related data struct Student { int roll; // Variable to store the student's roll number string name; // Variable to store the student's name float marks; // Variable to store the student's marks // A function inside the struct to display the student's details void display() { // Print the roll number cout << "Roll: " << roll << endl; // Print the name cout << "Name: " << name << endl; // Print the marks cout << "Marks: " << marks << endl; } }; int main() { Student s1; // Create an object 's1' of the structure 'Student' // Assign values to the s1 object’s variables s1.roll = 1; // Assign roll number 1 s1.name = "John"; // Assign name "John" s1.marks = 95.5; // Assign marks 95.5 s1.display(); // Call the function to print the student’s details return 0; // Indicate that the program ended successfully }
Output:
Roll: 1 Name: John Marks: 95.5
What is a Class in C++?
A class is like a more secure structure. It also groups data and functions, but gives you control over who can access the data. Classes follow the principles of Object-Oriented Programming (OOP) like encapsulation, abstraction, inheritance, and polymorphism.
In a class, all members are private by default, meaning they can’t be accessed directly from outside the class. You have to use functions to get or set data.
Real-life example:
Think of a bank locker:
- You can store valuable items (data).
- Only authorized people (functions) can access it.
- Outsiders can’t directly open the locker — they need permission.
Simple Syntax and Example:
#include <iostream> // Allows us to use input/output like cout using namespace std; // So we don't need to write std:: before every C++ keyword // Define a class named 'Student' class Student { // These are private variables by default (only accessible inside the class) int roll; // To store roll number string name; // To store name float marks; // To store marks public: // This function lets us assign values to the private variables void setDetails(int r, string n, float m) { roll = r; // Set roll number name = n; // Set name marks = m; // Set marks } // This function prints the student’s details void display() { cout << "Roll: " << roll << endl; // Print roll number cout << "Name: " << name << endl; // Print name cout << "Marks: " << marks << endl; // Print marks } }; int main() { Student s1; // Create a student object named 's1' // Set details for student using the function s1.setDetails(2, "Alice", 89.5); // Show the stored details on screen s1.display(); return 0; // Program ends successfully }
Output:
Roll: 2 Name: Alice Marks: 89.5
Key Differences
Struct in C++
- Default Access: Public
- Inheritance Type: Public by default
- Usage: Ideal for simple data containers (e.g., coordinates, RGB values, dates).
- Encapsulation: Weak or no data hiding members can be accessed directly.
- Flexibility: Less secure, no strict OOP rules.
- Syntax: Very easy and beginner-friendly.
- Best Use Case: When you just want to group variables without logic or security.
Class in C++
- Default Access: Private
- Inheritance Type: Private by default.
- Usage: Ideal for secure and complex object-oriented models.
- Encapsulation: Strong members are hidden unless explicitly made public.
- Flexibility: More secure, supports full object-oriented features like inheritance, polymorphism, abstraction.
- Syntax: Slightly more complex due to access specifiers and functions.
- Best Use Case: When you need to hide internal data and control how it’s accessed.
When to Use What?
Use struct when:
- You are working with simple data models.
- You want all variables to be public.
- Example: Point, Date, etc.
Use class when:
- You want to hide details from outside.
- You are designing software with user interaction, business logic, etc.
- Example: Bank Account, Employee, Vehicle, etc.