Classes & Objects in C++: Fundamentals & First OOP Program
Creating two Account objects from one class gives each account its own balance. Calling deposit on one changes that object’s state. The class definition describes the operation once for all accounts.
1. What is a Class and What is an Object? (In Simple Words)
In traditional C programming, data and functions were kept separate. Anyone could change global variables by mistake, which caused bugs.
Object-Oriented Programming (OOP) fixes this by wrapping data (variables) and the functions that work on that data together into one secure package.
A class describes fields and operations for a type. Constructing objects creates separate instances with their own state.
- Class (The Blueprint) — A user-defined data type that acts like an architectural map or drawing. It defines what variables and functions the object will have. Takes 0 bytes of RAM on its own.
- Object (The Real Entity) — A real, physical instance of the class created at runtime. When you create an object, the system allocates actual RAM memory for it.
Real-Life Examples to Remember for Exams:
- Architect Blueprint vs. House: An architect draws a 3BHK house plan on paper — that is a Class (takes 0 land). The actual house built with bricks and cement in your city is an Object (takes real land!). You can build 50 identical houses from 1 blueprint.
- Car Blueprint vs. Car: The engineering design of a Maruti Swift is the Class. The actual car standing in your college parking lot is an Object.
View diagram · The Class vs. Object Relationship
2. Syntax of a Class in C++
To create a class, use the class keyword followed by the class name. The body is wrapped inside curly braces {} and must end with a semicolon ;.
class Student {
private:
// Variables (Data Members)
// Hidden from outside world (Data Hiding)
int rollNo;
public:
// Functions (Member Functions / Methods)
// Can be called from main() to use the object
void input();
void display();
}; // Note the mandatory semicolon!
3. Access Specifiers: private vs. public vs. protected
Access specifiers decide who is allowed to view or use the members of a class:
| Access Specifier | Accessible Inside Same Class? | Accessible from Outside (main())? |
Accessible in Child / Derived Class? |
|---|---|---|---|
private (Default) |
Yes | No (Compiler error!) | No |
public |
Yes | Yes (Using dot operator .) |
Yes |
protected |
Yes | No | Yes (Inherited by child class) |
Exam Tip: In C++, if you don’t write anything, all members are
privateby default. (In a C++struct, members arepublicby default).
4. Your First Complete OOP Program in C++
Here is a complete, working student management program:
#include <iostream>
#include <string>
using namespace std;
// 1. Defining the Class Blueprint
class Student {
private:
// Private Data: Protected from outside tampering
int rollNumber;
string name;
float marks;
public:
// Public Member Function 1: Take input from user
void getDetails() {
cout << "Enter Roll Number: ";
cin >> rollNumber;
cin.ignore(); // Clears enter key from input buffer
cout << "Enter Student Name: ";
getline(cin, name);
cout << "Enter Marks: ";
cin >> marks;
}
// Public Member Function 2: Display student report
void displayDetails() {
cout << "\n--- Student Report Card ---" << endl;
cout << "Roll No : " << rollNumber << endl;
cout << "Name : " << name << endl;
cout << "Marks : " << marks << endl;
cout << "Result : " << (marks >= 40 ? "PASSED" : "FAILED") << endl;
}
}; // End of Class with semicolon
int main() {
// 2. Creating two Objects (Instances) of class Student
Student s1;
Student s2;
cout << "=== Entering Data for Student 1 ===" << endl;
s1.getDetails(); // Calling public function using dot (.) operator
cout << "\n=== Entering Data for Student 2 ===" << endl;
s2.getDetails();
// Displaying data for both students
s1.displayDetails();
s2.displayDetails();
// The line below gives a COMPILER ERROR because rollNumber is private!
// s1.rollNumber = 101;
return 0;
}
5. The Dot Operator (.)
To call a public function or access a public variable of an object inside main(), we use the Dot Operator (.):
objectName.functionName();
For example: s1.getDetails(); calls getDetails() specifically for object s1.
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- Differentiate between a Class and an Object with an example.
Model Answer:- Class: A user-defined data type and blueprint that defines properties and behaviors. Takes 0 bytes of RAM.
- Object: A physical instance of a class that occupies memory in RAM.
- Give the Blueprint vs. House or Car design vs. Real car example.
- What are access specifiers in C++? What is the default access specifier?
Model Answer:- Explain
private,public, andprotected. - Mention that in C++ classes, members are
privateby default.
- Explain
Frequently Asked 10-Mark Questions:
- Explain the fundamental concepts of OOP: Encapsulation, Data Hiding, and Abstraction. Write a complete C++ program to create a
BankAccountclass with account number, customer name, and balance, with deposit, withdraw, and check-balance methods.
Model Answer:- Define Encapsulation (wrapping data and methods into a single unit).
- Define Data Hiding (making balance
privateso outside code cannot tamper with money directly). - Write out the complete C++ class
BankAccountwithdeposit(),withdraw()(check if amount > balance), andcheckBalance(). - Create objects in
main()and demonstrate successful transactions.