A class is a user-defined data type that works as a blueprint/template for creating objects.
Class = Data + Functions in one unit
Socho tum ek "Student" naam ka class bana rahe ho. Har student ke paas kuch common cheezein hoti hain:
displayDetails()Yeh sab class ke andar define kiya jaata hai.
class ClassName {
public:
// Data Members
int x;
// Member Functions
void show() {
cout << "x = " << x << endl;
}
};
#include<iostream>
using namespace std;
class Student {
public:
string name;
int roll;
void display() {
cout << "Name: " << name << ", Roll: " << roll << endl;
}
};
int main() {
Student s1; // object of class Student
s1.name = "Nikhil";
s1.roll = 101;
s1.display(); // Output: Name: Nikhil, Roll: 101
return 0;
}
| Part | Description |
|---|---|
| Class | Blueprint for creating objects |
| Object | Real entity created from class |
| Data Members | Variables inside class (e.g., name, roll) |
| Member Functions | Functions inside class (e.g., display()) |
| Access Specifier | public, private, protected |