How unit 2 is examined
This unit covers how data and functions are packed into classes and objects, how access is controlled, how objects talk, and how they are created and destroyed; encapsulation, classes, access modifiers and constructors carry the marks.
Encapsulation and Data Abstraction
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>Encapsulation is the binding of data and the functions that operate on it into a single unit (the class) and restricting direct access to the data (data hiding); data abstraction is showing only the essential features to the user and hiding the background implementation details.</mark>
Diagram.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u2-01" viewBox="0 0 424 80" width="424" height="80" role="img" aria-label="User calls public functions (P) of the class; private data (D) is reachable only through them. Outside code cannot touch D directly."><style>#dsfig-u2-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u2-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u2-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u2-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u2-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u2-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u2-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u2-01 .t{fill:#16181D;font-weight:500}#dsfig-u2-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u2-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u2-01 .dot{fill:#16181D}#dsfig-u2-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u2-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u2-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u2-01 .ah{fill:#454C5A}#dsfig-u2-01 .ah.hi{fill:#2340B8}#dsfig-u2-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u2-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u2-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u2-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u2-01 .e{stroke:#B1B7C3}html.dark #dsfig-u2-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u2-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u2-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u2-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u2-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u2-01 .t{fill:#E6E8ED}html.dark #dsfig-u2-01 .t.inv{fill:#0F1115}html.dark #dsfig-u2-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u2-01 .dot{fill:#E6E8ED}html.dark #dsfig-u2-01 .ann{fill:#8FA3FF}html.dark #dsfig-u2-01 .lbl{fill:#858D9C}html.dark #dsfig-u2-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u2-01 .ah{fill:#B1B7C3}html.dark #dsfig-u2-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u2-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u2-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u2-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,40 L191,40" marker-end="url(#ah4)"/><path class="e" d="M231,40 L363,40" marker-end="url(#ah4)"/><g class="wl"><rect x="105.6" y="31" width="40.8" height="18" rx="9"/><text class="t" x="126" y="40" dy=".35em" text-anchor="middle">call</text></g><g class="wl"><rect x="270.9" y="31" width="54.3" height="18" rx="9"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">access</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">U</text><circle class="n" cx="212" cy="40" r="18"/><text class="t" x="212" y="40" dy=".35em" text-anchor="middle">P</text><circle class="n" cx="384" cy="40" r="18"/><text class="t" x="384" y="40" dy=".35em" text-anchor="middle">D</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">User calls public functions (P) of the class; private data (D) is reachable only through them. Outside code cannot touch D directly.</figcaption></figure>
Key points.
- Encapsulation is the implementation mechanism: the class wraps private data members and public member functions together, so data and functions are organised as one object.
- Abstraction is the design goal: the user sees what an object does (its interface) and not how it does it.
- Data hiding is achieved by making data members private, so they can be changed only through public member functions that can validate the value.
- Encapsulation solves the problem at the implementation level, while abstraction solves it at the design level; abstraction is achieved through encapsulation and access control.
- Modularity improves because each class is a self-contained unit that can be built and tested separately.
- Maintainability improves because the internal implementation can change without changing the code that uses the public interface.
- Security and reliability improve because invalid or accidental changes to data are blocked, and complexity is reduced because the user handles only a few operations.
- Data and functions in an OO program are organised as classes (data plus methods), objects are instances of classes, access is controlled by access modifiers, and objects interact by message passing.
Scope and life of a variable. Scope is the region of the program where a name is visible: local (inside a function), block (inside braces), global (whole file) and class scope (inside a class). Lifetime is how long the variable exists in memory: automatic variables live until their block ends, static and global variables live for the whole program, and dynamic (new) variables live until delete.
Example.
#include <iostream>
#include <string>
using namespace std;
class Student {
int roll; string name; float marks; // hidden data
public:
void input(int r, string n, float m) { roll = r; name = n; marks = m; }
void display() { cout << roll << " " << name << " " << marks << "\n"; }
};
int main() { Student s; s.input(1, "Asha", 82.5); s.display(); }
// 1 Asha 82.5
Answer frame. Open with the two definitions; draw the User-Public functions-Private data figure; then develop points 1-4 (mechanism, goal, data hiding, difference) and benefits 5-7; add the Student program and the scope and life lines for the Jun 2020 / Jun 2023 form; close with "Thus encapsulation hides data and abstraction hides complexity, making software modular and secure".
Pitfall: Do not define both terms as the same thing; state that abstraction is design-level (what) and encapsulation is implementation-level (how).
Asked: [14 marks] (Dec 2023) Short notes: a) Access modifiers b) Abstraction c) Interfaces d) String handling in OOP (Interface: a class of only pure virtual functions giving loose coupling and multiple inheritance; string class such as
std::stringgives dynamic size, member functions and operators+,==) Asked: [7 marks] (Dec 2020) How are data and functions organized in an object oriented program? Asked: [7 marks] (Jun 2020, Jun 2023) Explain encapsulation and data abstraction in detail. Write a C++ program to show these concepts. Define scope and life of any variable. Asked: [7 marks] (Dec 2024, Dec 2025) Explain encapsulation and data abstraction in OOP. How do these concepts enhance software development? Define encapsulation and data abstraction. Asked: [7 marks] (Dec 2025) Write a program to demonstrate encapsulation by creating a class Student with roll number, name and marks and member functions.
Concept of Objects: State, Behavior & Identity
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. An object is a run-time instance of a class that bundles state, behaviour and identity.
Key points.
- State is the set of current values of the object's data members (attributes), for example a car's colour and speed.
- Behaviour is what the object can do, defined by its member functions, and it may change the state.
- Identity is what makes an object distinct from every other object even when their states are equal; in C++ it is the unique memory address or name.
- An object is declared as
Student s;(orStudent *p = new Student;), memory is allocated at instantiation, and it is used through the dot operator (s.display()) or the arrow operator for pointers. - Objects can be classified by lifetime and storage: automatic (local), static, global and dynamic (created by
new); by role they are also called entity, control and boundary objects.
Answer frame. Open with the definition; list state, behaviour and identity with a car example; show declaration and use with the dot operator; close with types of objects (automatic, static, global, dynamic).
Asked: [7 marks] (Nov 2018) What is an Object? How can we use objects in a program? Asked: [7 marks] (Nov 2018) Explain different types of objects.
Classes: identifying classes, attributes and services
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>A class is a user-defined data type and blueprint that groups attributes (data members) and services (member functions); an object is an instance of a class.</mark>
Identifying classes. Read the problem statement: nouns become candidate classes, adjectives and simple nouns become attributes, and verbs become services (methods); CRC cards (Class, Responsibility, Collaborator) then confirm each candidate. Reject nouns that are only synonyms, attributes, or outside the system.
Key points.
- A class is a logical template that occupies no memory by itself, while each object created from it occupies real memory.
- Many objects can be created from one class, and each has its own copy of the non-static data members.
- Concrete classes can be instantiated directly, and abstract classes contain at least one pure virtual function and cannot be instantiated.
- A singleton class allows only one object, which is done with a private constructor and a static
getInstance()function. - A nested (inner) class is declared inside another class, an empty class has no members, and a final class (C++11
final) cannot be inherited. - A member function is declared inside a class and is called through an object, so it can access private data directly.
| Basis | Class | Object |
|---|---|---|
| Nature | Blueprint, logical entity | Instance, physical entity |
| Memory | None until objects exist | Allocated per object |
| Creation | Declared once | Created many times |
| Example | class Car |
Car c1; |
| Basis | Member function | Normal function |
| --- | --- | --- |
| Declared | Inside a class | Outside any class |
| Called | With an object, obj.f() |
Directly, f() |
| Private data | Direct access | Not allowed (unless friend) |
this pointer |
Available | Not available |
Example.
#include <iostream>
using namespace std;
class Rectangle {
int length, width;
public:
Rectangle(int l, int w) : length(l), width(w) {}
int calculateArea() { return length * width; }
int calculatePerimeter() { return 2 * (length + width); }
};
int main() {
Rectangle r(5, 3);
cout << r.calculateArea() << " " << r.calculatePerimeter() << "\n";
}
// 15 16
For Student, keep string name; int roll; int marks[3]; as private data, a constructor, float calculateAverage() returning (marks[0]+marks[1]+marks[2])/3.0f, and displayInfo(); create the object in main() and call both.
Answer frame. Open with the class definition and the class-versus-object table; give the syntax class Name { private: ...; public: ...; }; with an object; then add the kinds of classes (concrete, abstract, singleton, nested, final); for programs, identify classes by noun-verb analysis first, then write the class and a main(); close by saying a class is the blueprint from which objects are built.
Pitfall: In the Rectangle and Student programs do not forget the driver
main(); the examiner marks it separately.
Asked: [7 marks] (Nov 2019) List out the different kinds of classes. Explain with an example. Asked: [7 marks] (May 2019) Differentiate between a member function and a normal function. Asked: [7 marks] (Jun 2020) What do you mean by class and object? Relation and difference? Write a C++ class syntax. Explain abstract class and singleton class in brief. Asked: [7 marks] (Nov 2022) What do you mean by class and object? Relation and difference? Write a C++ class syntax. Explain state, behavior and identity of an object in brief. Asked: [7 marks] (Dec 2024) Given a class rectangle with length and width, write methods for area and perimeter and demonstrate them in a program. Asked: [7 marks] (Dec 2024) What are classes in OOP? Discuss how to identify classes. Create a class student (name, roll_number, marks) with methods for average and display; demonstrate in a program. Asked: [7 marks] (Dec 2025) Explain classes and objects.
Access modifiers
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>Access modifiers (access specifiers) are keywords that set the visibility of class members; C++ has three, public, private and protected.</mark>
Key points.
privatemembers are accessible only inside the same class (and friends), and this is the default for a class.publicmembers are accessible from anywhere, so they form the interface of the class; this is the default for a struct.protectedmembers are accessible inside the class and in its derived classes but not from outside code.- Data members are normally made private and access is given through public getter and setter functions, which is how data hiding is implemented.
- Because outside code cannot touch private data, the class can validate every change, giving encapsulation and control.
- Access modifiers give the class a stable public interface, so the private part can change freely.
| Location | private | protected | public |
|---|---|---|---|
| Same class | Yes | Yes | Yes |
| Derived class | No | Yes | Yes |
| Outside world | No | No | Yes |
| Basis | Public | Private | |
| --- | --- | --- | |
| Visibility | Anywhere | Only inside the class | |
| Inheritance | Inherited and usable | Inherited but not accessible in derived class | |
| Default | Struct | Class | |
| Use | Interface functions | Data members |
class Base {
private: int a; // only Base
protected: int b; // Base and derived
public: int c; // everyone
};
class Derived : public Base {
void f() { b = 1; c = 2; /* a = 3; error */ }
};
// Base x; x.c = 5; OK x.a = 5; error
Answer frame. Open with the definition and list the three specifiers; draw the accessibility table; then explain each specifier with the code above; close with the role of private in data hiding. For the Dec 2025 combined question add one line each on static members and the object life cycle (creation by constructor, use, destruction by destructor).
Pitfall: Writing "protected is same as private" loses marks; protected is visible to derived classes.
Asked: [7 marks] (Jun 2020, Nov 2022, Dec 2024) What are the access modifiers? How many types are available in C++? Explain each in detail and their role in data hiding. Compare public, private and protected with examples. Asked: [7 marks] (Dec 2025) Discuss access modifiers, static members and object life cycle. Asked: [7 marks] (May 2019) Differentiate with example: i) Constructor and destructor ii) Public and private access specifiers (see the tables here and under Construction)
Static members of a Class
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. A static member belongs to the class as a whole and not to any single object.
Key points.
- A static data member has one copy shared by all objects, is declared inside the class as
static int count;, and is defined outside asint Box::count = 0;. - A static member function can access only static members, has no
thispointer, and is called asBox::show(). - We declare a member static when the data must be shared by all objects, such as an object counter, or when a function needs class-level access without an object.
- Static data is created before any object and lives until the program ends.
Asked: [7 marks] (Dec 2020) What is a class? When do we declare a member of a class static?
Instances
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. An instance is a concrete object created from a class; an empty class is a class with no data members or functions.
Key points.
- An empty class such as
class Empty {};can be instantiated, since the compiler only needs the class name to create an object. - Its size is 1 byte, because every object must have a distinct address, and a zero-size object would make two objects share one address.
- The reason it is allowed is that an instance needs identity, and identity needs storage;
sizeof(Empty)prints 1. - Each instance has its own copy of non-static data, so instances of one class hold independent state.
Asked: [7 marks] (May 2019) What are empty classes? Can an instance of an empty class be created? Give reason.
Message passing
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. <mark>Message passing is the way objects communicate: a sender object requests a receiver object to perform an action by calling one of its member functions with arguments.</mark>
Key points.
- A message has three parts: the receiver object name, the method name and the arguments, written in C++ as
object.method(arguments). - The receiver decides how to respond, since only it knows the implementation, which preserves encapsulation.
- Objects interact by one calling the public functions of another, for example
account.deposit(500)sent from a customer object. - Message passing gives loose coupling, because senders depend only on the interface, and it supports polymorphism, since the same message can give different behaviour in different classes.
- The result of a message may be a return value or a change in the receiver's state.
#include <iostream>
using namespace std;
class Account {
int bal = 0;
public:
void deposit(int x) { bal += x; }
int balance() { return bal; }
};
int main() { Account a; a.deposit(500); cout << a.balance() << "\n"; }
// 500
Answer frame. Open with the definition; draw two boxes Sender to Receiver labelled with the message; develop points 1-5; show the code; close with its benefit of loose coupling.
Asked: [7 marks] (Nov 2018) What is Message passing? How objects are interacted with one another? Asked: [7 marks] (Jun 2023, Dec 2024) What is Message Passing? How can it be achieved in C++? How does it facilitate communication between objects?
Construction and destruction of Objects
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">High weight</span>
Definition. <mark>A constructor is a special member function with the same name as the class and no return type, called automatically when an object is created to initialise it; a destructor, named ~ClassName, is called automatically when the object is destroyed to release its resources.</mark>
Key points.
- A constructor is invoked automatically at creation, has no return type, and can be overloaded; a destructor takes no arguments, cannot be overloaded, and there is one per class.
- A constructor is not mandatory: if none is written, the compiler supplies a default constructor, though it does not initialise built-in members.
- A default constructor takes no arguments, and a parameterized constructor takes arguments to give each object its own initial values:
Box(int i);. - A copy constructor initialises an object from an existing one:
ClassName(const ClassName &obj);, and it must take a reference to avoid infinite recursion. - A dynamic constructor allocates memory with
newinside the constructor, and the destructor then releases it withdelete. - Objects are destroyed in the reverse order of construction, so the last object created is destroyed first.
- Life cycle: creation (constructor), use (member functions), destruction (destructor at end of scope or
delete).
#include <iostream>
using namespace std;
class Box {
int id;
public:
Box(int i = 0) : id(i) { cout << "ctor " << id << "\n"; }
Box(const Box &o) : id(o.id + 100) { cout << "copy " << id << "\n"; }
~Box() { cout << "dtor " << id << "\n"; }
};
int main() { Box a(1); Box b(a); Box c; }
// ctor 1, copy 101, ctor 0, dtor 0, dtor 101, dtor 1
| Basis | Constructor | Destructor |
|---|---|---|
| Purpose | Initialises the object | Releases resources |
| Name | ClassName() |
~ClassName() |
| Called | When object is created | When object goes out of scope or is deleted |
| Arguments | Allowed, can be overloaded | None, only one |
| Order | Creation order | Reverse order |
Answer frame. Open with both definitions; develop points 1-5 with the code for each type; show the output and state that destruction is in reverse order; for "is it mandatory" answer no, the compiler gives a default one; close with the life-cycle line.
Pitfall: A copy constructor takes
const ClassName &; passing by value is an error.
Asked: [7 marks] (Nov 2018) What is Constructor? Is it mandatory to use Constructor in a class? Asked: [7 marks] (Nov 2019) Explain the various types of constructor with a programming example. Asked: [7 marks] (Dec 2020) What is constructor and destructor function? Explain with example. Asked: [7 marks] (Jun 2020, Nov 2022) Explain the role of constructor and destructor. Types of constructors in C++? Write syntax for copy and parameterized constructors. Asked: [7 marks] (Nov 2022, Dec 2023) Discuss construction and destruction of an object. Write a program to explain it. Asked: [7 marks] (May 2019) Differentiate: i) Constructor and destructor ii) Public and private access specifiers (tables here and under Access modifiers)
Last-minute revision
- Encapsulation binds data and functions into a class; abstraction shows only the essentials.
- Abstraction is the design goal, encapsulation the mechanism; data hiding uses private members.
- Object = state + behaviour + identity.
- Class is a logical blueprint; object is a physical instance with memory.
- Find classes by noun-verb analysis and CRC cards: nouns to classes, verbs to services.
- Class default access is private; struct default is public; protected is visible to derived classes.
- Static data member: one shared copy, defined outside the class as
int C::x;. - Empty class has size 1 byte and can be instantiated.
- Message = receiver + method + arguments:
obj.method(args). - Constructor has the class name and no return type; destructor is
~Class(); destruction is in reverse order. - Copy constructor signature:
Class(const Class &obj).
Memory hooks
- ESA: Encapsulation = Storage in a capsule, Abstraction = Appearance only.
- SBI: State, Behaviour, Identity.
- P-P-P: Private (class), Protected (class + children), Public (all).
- Constructor builds first, destructor demolishes last, in reverse (last in, first out).
- Empty class still gets 1 byte, because no two objects share an address.
Coverage checklist
- Encapsulation and Data Abstraction: Dec 2023 short notes, Dec 2020, Jun 2020, Jun 2023, Dec 2024, Dec 2025 (two).
- Concept of Objects: State, Behavior & Identity of an object: Nov 2018 (two).
- Classes: identifying classes and candidates for Classes Attributes and Services: Nov 2019, May 2019, Jun 2020, Nov 2022, Dec 2024 (two), Dec 2025.
- Access modifiers: Jun 2020, Nov 2022, Dec 2024, Dec 2025, May 2019 (public and private).
- Static members of a Class: Dec 2020.
- Instances: May 2019.
- Message passing: Nov 2018, Jun 2023, Dec 2024.
- Construction and destruction of Objects: Nov 2018, Nov 2019, Dec 2020, Jun 2020, Nov 2022, Dec 2023, May 2019 (constructor and destructor).