1. How to Use This Page
1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.
2. Key Concepts (Quick Recap)
Class vs Object
| Feature | Class | Object |
|---|---|---|
| What is it? | Blueprint / template | Instance of a class |
| Memory | No memory until objects created | Occupies memory |
| Created by | class keyword | Declaration of variable |
| Example | class Student { ... }; | Student s1; |
Defining a Class and Creating Objects
class Student {
private:
int rollNo;
char name[20];
float marks;
public:
void getDetails() {
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
cout << "Enter marks: ";
cin >> marks;
}
void display() {
cout << "Roll No: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
};
// Creating objects
Student s1; // Object s1 of class Student
Student s2; // Object s2 of class Student
s1.getDetails(); // Calling member function
s1.display();
Access Specifiers
| Specifier | Accessible from | Usage |
|---|---|---|
private | Only within the class | Data members (for encapsulation) |
protected | Within the class and derived classes | Used in inheritance |
public | Anywhere (inside and outside the class) | Member functions (interface) |
By default, class members are private if no specifier is given.
Constructors
A constructor is a special member function that automatically initialises an object when it is created.
| Feature | Rule |
|---|---|
| Name | Same as class name |
| Return type | None (not even void) |
| Called | Automatically when object is created |
| Purpose | Initialise data members |
| Overloading | Yes — multiple constructors with different parameters |
Types of Constructors:
| Type | Purpose | Example |
|---|---|---|
| Default | No parameters | Student() { rollNo = 0; } |
| Parameterised | Takes parameters | Student(int r) { rollNo = r; } |
| Copy | Creates a copy of an existing object | Student(Student &s) { rollNo = s.rollNo; } |
class Sample {
int x;
public:
Sample() { x = 0; } // Default constructor
Sample(int a) { x = a; } // Parameterised constructor
Sample(Sample &s) { x = s.x; } // Copy constructor
};
Sample obj1; // Calls default constructor
Sample obj2(10); // Calls parameterised constructor
Sample obj3(obj2); // Calls copy constructor
Destructor
A destructor is a special member function called automatically when an object goes out of scope.
class Sample {
public:
~Sample() {
cout << "Object destroyed";
}
};
| Feature | Rule |
|---|---|
| Name | Same as class name prefixed with ~ |
| Return type | None |
| Parameters | None |
| Called | Automatically when object is destroyed |
| Purpose | Release resources (memory, files, etc.) |
Inline Functions
An inline function expands its code at the call site instead of performing a function call, reducing overhead.
class Calculator {
public:
inline int square(int x) { // Inline function
return x * x;
}
};
When square(5) is called, the compiler replaces it with 5 * 5 directly.
this Pointer
The this pointer is an implicit pointer available in every non-static member function. It points to the object that called the function.
class Sample {
int x;
public:
void setX(int x) {
this->x = x; // 'this->x' refers to the member, 'x' refers to the parameter
}
};
Static Data Members
A static data member is shared by all objects of the class. It exists even before any object is created.
class Counter {
public:
static int count; // Declaration
Counter() { count++; }
};
int Counter::count = 0; // Definition (mandatory)
3. I. Choose the Best Answer (MCQs)
this pointer points to:4. II. Fill in the Blanks
1. A class is a blueprint from which objects are created. 2. An object is an instance of a class. 3. The default access specifier in a C++ class is private. 4. A constructor is automatically called when an object is created. 5. A destructor is automatically called when an object is destroyed. 6. A constructor with no parameters is called a default constructor. 7. A constructor that takes parameters is called a parameterised constructor. 8. A copy constructor creates a new object as a copy of an existing object. 9. The this pointer refers to the current object that invoked the member function. 10. The inline keyword suggests the compiler to expand the function body at the call site. 11. A static data member is shared by all objects of the class. 12. The dot (.) operator is used to access public members of an object. 13. A destructor is prefixed with the ~ (tilde) symbol.
5. III. True or False (with corrections)
1. A class occupies memory for its data members even before any object is created.
False. A class is only a template or blueprint. Memory is allocated only when objects are created. Static data members are an exception — they are allocated even without objects.
2. A constructor can have a return type.
False. A constructor has no return type, not even void. It is called implicitly when an object is created and cannot return a value.
3. Private members of a class can be accessed from outside the class using the dot operator.
False. Private members can only be accessed by member functions of the same class. They are not accessible from outside, even with the dot operator.
4. A class can have multiple constructors with different parameters.
True. This is called constructor overloading. A class can have a default constructor, a parameterised constructor, and a copy constructor, all with different parameter lists.
5. A destructor can take parameters.
False. A destructor takes no parameters and has no return type. There can be only one destructor per class (no overloading).
6. The this pointer is available in static member functions.
False. The this pointer is not available in static member functions because static functions are not called through any specific object — they belong to the class.
6. IV. Match the Following
| Column A | Column B |
|---|---|
| 1. Class | a. Instance of a class |
| 2. Object | b. Blueprint/template |
| 3. Constructor | c. Called when object is created |
| 4. Destructor | d. Called when object is destroyed |
5. private | e. Accessible only within the class |
6. public | f. Accessible from anywhere |
7. this | g. Pointer to current object |
8. static | h. Shared by all objects |
7. V. Answer Briefly (2-mark questions)
8. VI. Answer in Detail (5-mark questions)
BankAccount class with deposit, withdrawal, and balance display functionality.#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNo;
char name[30];
float balance;
public:
// Constructor to initialise account
BankAccount() {
accountNo = 0;
balance = 0.0;
}
// Member function to open account
void openAccount() {
cout << "Enter account number: ";
cin >> accountNo;
cout << "Enter account holder name: ";
cin >> name;
cout << "Enter initial deposit: ";
cin >> balance;
}
// Member function to deposit money
void deposit() {
float amount;
cout << "Enter amount to deposit: ";
cin >> amount;
balance = balance + amount;
cout << "Deposit successful. New balance: " << balance << endl;
}
// Member function to withdraw money
void withdraw() {
float amount;
cout << "Enter amount to withdraw: ";
cin >> amount;
if (amount > balance) {
cout << "Insufficient balance!" << endl;
} else {
balance = balance - amount;
cout << "Withdrawal successful. New balance: " << balance << endl;
}
}
// Member function to display balance
void displayBalance() {
cout << "Account No: " << accountNo << endl;
cout << "Name: " << name << endl;
cout << "Balance: Rs. " << balance << endl;
}
};
int main() {
BankAccount account;
int choice;
account.openAccount();
do {
cout << "\n--- Bank Menu ---" << endl;
cout << "1. Deposit" << endl;
cout << "2. Withdraw" << endl;
cout << "3. Display Balance" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: account.deposit(); break;
case 2: account.withdraw(); break;
case 3: account.displayBalance(); break;
case 4: cout << "Thank you!"; break;
default: cout << "Invalid choice!";
}
} while (choice != 4);
return 0;
}
9. Common Mistakes to Avoid
1. Forgetting the semicolon after the class definition — class Student { ... }; must end with };. Missing this causes compilation errors.
2. Accessing private members from outside the class — Private members cannot be accessed using the dot operator from main() or other functions. Use public member functions (getters/setters) to access them.
3. Confusing class and object — A class is a template; an object is a specific instance. class Student is the template; Student s1 creates an object.
4. Constructor with return type — Constructors have no return type, not even void. Adding void before the constructor name makes it a regular member function, not a constructor.
5. Destructor with parameters — A destructor takes no parameters and cannot be overloaded. There is exactly one destructor per class.
6. Not defining static data members outside the class — A static data member declared inside the class must be defined outside: int Counter::count = 0;. Forgetting this causes a linker error.
7. Using this pointer in static functions — Static member functions do not have a this pointer because they are not called through an object.
8. Calling a constructor explicitly like a normal function — Constructors are called automatically when objects are created, not invoked like obj.Constructor().
10. Exam Checklist
- [ ] I can define a class with private, protected, and public sections
- [ ] I can create objects and access members using the dot operator
- [ ] I can explain the three access specifiers and their scope
- [ ] I can write default, parameterised, and copy constructors
- [ ] I can write a destructor and explain when it is called
- [ ] I can differentiate between constructor and destructor
- [ ] I can explain the
thispointer and its use - [ ] I can explain inline functions and when to use them
- [ ] I can explain static data members and their properties
- [ ] I can write complete programs using classes and objects
- [ ] I understand encapsulation — why data is private and functions are public
