Theory
Why this matters
You already know how to store one mark in a float and one name in a character array. But a student is not one value. A student is a roll number, a name, three subject marks, and the ability to print its own result. Keeping those in separate variables works for 10 students and falls apart for 10,000. The class is C++'s answer, and almost every topic in the next two units is a rule about classes.
Theory
One template, thousands of ID cards
Your college designed one ID card layout: a photo box, a name line, a roll number line, a barcode. That design is not an ID card. You cannot swipe it at the library gate. The office prints thousands of real cards from it, each with a different photo and roll number, but every card has the same fields in the same places.
The design is the class. Each printed card is an object.
Theory
The two definitions
A class is a user-defined data type that binds data (member variables) and the functions that act on that data (member functions) into a single unit. It is a blueprint, not a thing.
An object is an instance of a class: one actual entity built from that blueprint, carrying its own values.
Say it this way in the exam: Student is the class, s1 and s2 are objects of that class.
Theory
The line examiners listen for
Writing a class allocates no memory. The line class Student { int roll; float marks; }; reserves nothing at all. It only tells the compiler what a Student will look like.
Memory appears when you create an object. Student s1; gives s1 its own roll and its own marks. Two objects mean two separate copies of the data, while the member functions are stored once and shared by every object.
At a glance
Class vs Object, the exam answer
| Aspect | Class | Object |
|---|---|---|
| What it is | A blueprint or template | An instance built from it |
| Memory | None when declared | Allocated for each object |
| How many | Written once | As many as you create |
| Created by | class Student { ... }; | Student s1, s2; |
| Holds | The design of the data | The actual values |
Practical
One class, two objects
#include <iostream>
using namespace std;
class Student { // the blueprint: nothing exists yet
public:
int roll; // member variable
float marks; // member variable
void show() { // member function
cout << roll << " scored " << marks << endl;
}
};
int main() {
Student s1, s2; // two objects, NOW memory is allocated
s1.roll = 101; s1.marks = 78.5;
s2.roll = 102; s2.marks = 64.0;
s1.show(); // 101 scored 78.5
s2.show(); // 102 scored 64
return 0;
}
Think first
Trace it in your head
s1 and s2 are two objects of the same class Student. You set s1.roll = 101, and later you run s2.roll = 202. Take five seconds: what is s1.roll now, and why?
Show the answer
s1.roll is still 101. Every object gets its own copy of every member variable, so writing through s2 cannot touch s1.
Only the member functions are shared: there is one copy of show() in memory serving both objects. If you expected 202, you were picturing the class as a single shared box, which is the most common beginner mistake in this topic.
Watch out
The trap: using the class name as if it were an object
Student.roll = 101; does not compile. Student is a type, not a thing, exactly like writing int = 5; instead of x = 5;.
You must create an object first and then use the dot operator on that object: Student s1; followed by s1.roll = 101;. In a viva, spotting this is the quickest way to show you understand which of the two words means what.
Quiz
The line `class Student { int roll; float marks; };` has just been compiled. How much memory has been allocated for `roll` and `marks` at this point?
- None, because no object exists yet
- 4 bytes for `roll` and 4 bytes for `marks`
- Enough for one Student, reserved by the compiler
- It depends on how many objects you create later
Show the answer
None, because no object exists yet
A class declaration only describes a type, so nothing is allocated. Both "4 bytes each" and "enough for one Student" assume the class itself stores values, which is the single most common error in this topic. The last option is closer in spirit but still wrong about where allocation happens: it is not the class that grows, it is each Student s1; that allocates that object's own copy at the moment the object is created.
Formula
Where this goes next
Everything ahead is a rule about these two words. Unit 2 decides who may touch an object's data (public, private, protected), then gives each object a birth and a death through constructors and destructors. Unit 3 lets objects of related classes answer the same call differently.
In the exam, "define class and object with an example" is a standard 3 to 4 mark question. Give both definitions, one small code skeleton, and the memory line.
Summary
Key takeaways
- A class binds data members and member functions into one user-defined type. It is a blueprint.
- An object is an instance of a class, and it is what actually occupies memory.
- Declaring a class allocates nothing;
Student s1;allocates fors1. - Each object holds its own copy of the data members, while member functions are stored once and shared.
- Reach members through an object with the dot operator, never through the class name.
- Memory hook: one template, many ID cards.