Skip to content

Defining classes

🔷 What is a Class in C++?

A class in C++ is a user-defined data type that acts as a blueprint for creating objects. It groups data (called data members) and functions (called member functions or methods) that operate on that data into a single unit.

Classes are the foundation of Object-Oriented Programming (OOP). They help in encapsulating (hiding) data, reusing code through inheritance, and modeling real-world entities.


🔷 Syntax of a Class

class ClassName {
private:
// Private data members and functions

public:
// Public data members and functions

protected:
// Protected data members and functions (used in inheritance)
};
  • private: Members cannot be accessed outside the class directly.
  • public: Members can be accessed from outside the class using an object.
  • protected: Used mainly in inheritance (not accessible directly outside the class, but accessible to derived classes).

🔷 Example: Defining and Using a Class

#include <iostream>
using namespace std;

class Student {
private:
int rollNumber;
float marks;

public:
string name;

// Function to set private data
void setData(int r, float m) {
rollNumber = r;
marks = m;
}

// Function to display student data
void display() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Marks: " << marks << endl;
}
};

int main() {
Student s1;
s1.name = "Alice";
s1.setData(101, 89.5);
s1.display();

return 0;
}

🔷 Explanation of the Example

  • Class Name: Student
  • Data Members: rollNumber, marks, name
  • Member Functions: setData(), display()
  • Object: s1 is an object of the class Student
  • The setData() function is used to initialize private data.
  • The display() function shows student details.

🔷 Key Concepts in Classes

ConceptDescription
EncapsulationWrapping data and functions together. Keeps data safe from outside interference.
Data MembersVariables defined inside a class.
Member FunctionsFunctions defined inside a class.
Access SpecifiersControl how data/functions are accessed (public, private, protected).
ObjectAn instance of a class. Used to access class members.

🔷 Memory Allocation

  • Memory is allocated to class members only when an object is created.
  • Each object gets its own copy of data members, but member functions are shared.

🔷 Advantages of Using Classes

  1. Modularity – Code is organized and modular.
  2. Reusability – Classes can be reused using inheritance.
  3. Security – Data hiding protects sensitive information.
  4. Scalability – Easy to modify and scale programs.

🔷 Diagram: Class vs Object (Text Form)

Class: Student
------------------------
| - rollNumber : int |
| - marks : float |
| + name : string |
| + setData() |
| + display() |
------------------------

Object: s1
------------------------
| rollNumber = 101 |
| marks = 89.5 |
| name = "Alice" |
------------------------

🔷 Summary

  • A class is a user-defined type in C++ that bundles data and functions.
  • You use a class to create objects, and through these objects, you access the class members.
  • Use public members to interact with the object and keep private members hidden for security and encapsulation.