Chapter 2: Object Oriented Programming in C++ Class 12 Computer Science NCERT Solutions
Chapter 2 introduces students to the core principles of Object-Oriented Programming (OOP) using C++. It marks a paradigm shift from procedural programming by focusing on data encapsulation, reusability, and abstraction through classes and objects. This chapter forms the conceptual and practical foundation for building modular, scalable, and maintainable software.
Understanding OOP concepts like classes, objects, encapsulation, and inheritance is crucial for developing real-world applications and aligns with modern software development practices used in industry today.
What You Will Learn in Chapter 2
This chapter enables students to move beyond procedural logic and start thinking in terms of objects and their interactions. It teaches how to define and implement user-defined data types using classes, and how to design programs based on reusable and modular structures.
Key Topics Covered
Introduction to OOP
Evolution from procedure-oriented to object-oriented programming
Benefits of OOP: modularity, reusability, security, and abstraction
Key OOP concepts: Class, Object, Encapsulation, Abstraction, Inheritance, Polymorphism
Classes and Objects in C++
Defining a class: syntax and structure
Creating objects of a class
Accessing data members and member functions
Visibility modes: public, private, protected
Defining Member Functions
Inside the class definition (inline functions)
Outside the class using scope resolution operator
::
Passing arguments to member functions
Encapsulation and Data Hiding
Use of access specifiers: public, private, protected
Bundling of data and functions that operate on that data
Constructor and Destructor
Constructor: special function that initializes objects
Types: default, parameterized, copy constructors
Destructor: function to clean up resources
Arrays of Objects
Creating and managing multiple objects using arrays
Accessing members through loops
Dynamic Memory Allocation with Objects
Using
new
anddelete
for object creation and destructionManaging memory during runtime
Object as Function Arguments
Passing objects by value and by reference
Returning objects from functions
Download Chapter 2 Solutions PDF – Object Oriented Programming in C++
Our PDF includes:
All NCERT textbook questions with complete, well-explained answers
Class diagrams and sample code for each OOP concept
Syntax and logic examples for constructors, destructors, and access specifiers
Output-based questions and solved programs
Visuals of memory allocation with object pointers
Highlights of Our NCERT Solutions
Clearly illustrated object-oriented concepts using real-world examples
Simplified explanation of class and object declarations
Conceptual clarity with code-driven learning
Debugging assistance and best coding practices
Comparison between procedural and object-oriented approaches
Recommended Preparation Tips
Understand the difference between class and object through diagrams
Practice writing basic class programs with constructors and destructors
Focus on how access specifiers impact encapsulation
Write short programs using arrays of objects and constructors
Revise syntax and behavior of functions defined inside and outside the class
Additional Study Resources
Flashcards: OOP terminology and concepts
Worksheets: Fill-in-the-code and logic-based activities on classes
PYQs and CBSE sample questions on OOP
Constructor/destructor usage charts
Object interaction diagrams and memory tracing activities
Mastering Chapter 2 – Object-Oriented Programming in C++
Mastery of this chapter is crucial for advancing in software development and understanding modern programming design. Object-Oriented Programming empowers students to think modularly, solve problems effectively, and build complex applications with clarity.
A strong foundation in classes, objects, and constructors enables students to move confidently into topics like inheritance, polymorphism, file handling, and data structures—key for success in both board exams and computer science careers.
Class 12 Computer Science (C++) – Chapter 2: Object Oriented Programming in C++
Short Answer Type Questions
Question 1: Differentiate between data abstraction and data hiding. [Delhi, 2015]
Answer:- Data Hiding: Restricting access to internal data by declaring members as
private
to prevent unauthorized access or changes. - Data Abstraction: Showing only essential features and hiding implementation details. Achieved using
public
member functions to interact with private data.
Question 2: How encapsulation and abstraction are implemented in C++ language? Explain with an example. [CBSE SQP 2015]
Answer:- Encapsulation: Wrapping data and associated functions together into a single unit (class).
- Abstraction: Exposing only necessary functions while hiding internal implementation.
class Rectangle {
int L, B; // Encapsulated data
public:
void Input() {
cin >> L >> B;
}
void Output() {
cout << L << B;
}
};
Here, Input()
and Output()
provide abstraction, while L
and B
are hidden.
Question 3: Explain data hiding with an example. [CBSE Comptt., 2014]
Answer: Data hiding ensures that internal object data is protected and not directly accessible from outside the class.class Square {
private:
int Num; // Data hidden
public:
void Get() {
cout << "Enter Number:"; cin >> Num;
}
void Display() {
cout << "Square Is: " << Num * Num;
}
};
void main() {
Square Obj;
Obj.Get();
Obj.Display();
}
Question 4: What do you understand by data encapsulation and data hiding? Also, give a suitable C++ code to illustrate both. [CBSE SQP 2013]
Answer:- Data Encapsulation: Bundling of data and methods in a class.
- Data Hiding: Preventing access to data members using private visibility.
#include <iostream.h>
class Adder { // Encapsulation
public:
Adder(int i = 0) {
total = i;
}
void addNum(int Number) {
total += Number;
}
int getTotal() {
return total;
}
private:
int total; // Data Hiding
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total: " << a.getTotal() << endl;
return 0;
}
Question 5: Write the output of the following C++ program code: [Delhi, 2015]
class Eval {
char Level;
int Point;
public:
Eval() {
Level = 'E'; Point = 0;
}
void Sink(int L) {
Level -= L;
}
void Float(int L) {
Level += L;
Point++;
}
void Show() {
cout << Level << "#" << Point << endl;
}
};
void main() {
Eval E;
E.Sink(3);
E.Show();
E.Float(7);
E.Show();
E.Sink(2);
E.Show();
}
Output:
B#0 I#1 G#1
Question 6: Observe the following program carefully and answer the questions: [CBSE SQP 2016]
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main() {
clrscr();
randomize();
char courses[4][10] = {"M.Tech", "MCA", "MBA", "B.Tech"};
int ch;
for(int i = 1; i <= 3; i++) {
ch = random(i) + 1;
cout << courses[ch] << "\t";
}
getch();
}
(i) M.Tech will never be displayed because index 0 is never accessed in the loop.
(ii) MCA will not always be displayed first due to the use of random(i) + 1.
Minimum value of ch = 1
Maximum value of ch = 3
Question 7: Study the following program and select the possible outputs and value range of VAL: [O.D, 2015]
void main() {
randomize();
int VAL;
VAL = random(3) + 2;
char GUESS[] = "ABCDEFGHIJK";
for (int I = 1; I <= VAL; I++) {
for (int J = VAL; J <= 7; J++)
cout << GUESS[J];
cout << endl;
}
}
Correct output options are (ii) and (iii).
Minimum value of VAL: 2
Maximum value of VAL: 4
Question 8: Rewrite the following program after removing syntax errors: [O.D, 2012]
#include <iostream.h>
#include <iomanip.h>
class Item {
long IId, Qty;
public:
void purchase() {
cin >> IId >> Qty;
}
void sale() {
cout << setw(5) << IId << " Old:" << Qty << endl;
cout << "New: " << Qty << endl;
}
};
void main() {
Item I;
I.purchase();
I.sale();
I.sale();
}
The syntax has been corrected by ensuring the use of proper headers and correctly structured methods.
There are no logical errors; the output depends on input.
Question 9: Rewrite the following program after removing syntax errors: [Delhi, 2012]
#include <iostream.h>
#include <iomanip.h>
class Book {
long Bid, Qty;
public:
void purchase() {
cin >> Bid >> Qty;
}
void sale() {
cout << setw(5) << Bid << " Old: " << Qty << endl;
cout << "New: " << --Qty << endl;
}
};
void main() {
Book B;
B.purchase();
B.sale();
}
All syntax issues are corrected. It includes setw
from iomanip.h
and uses proper class and method syntax.
The --Qty
decreases the quantity by 1 before display.
Question 10: Answer the questions after going through the class: [O.D, 2012]
class Travel {
int PlaceCode;
char Place[20];
float Charges;
public:
Travel() {
PlaceCode = 1;
strcpy(Place, "DELHI");
Charges = 1000;
}
void TravelPlan(float C) {
cout << PlaceCode << ":" << Place << ":" << Charges << endl;
}
~Travel() {
cout << "Travel Plan Cancelled" << endl;
}
Travel(int PC, char P[], float C) {
PlaceCode = PC;
strcpy(Place, P);
Charges = C;
}
};
(i) Function 1 and Function 4 are overloaded constructors (default and parameterized).
(ii) Function 3 is a destructor and is automatically called when the object goes out of scope.
Short Answer Type Questions – II (3 Marks Each)
Question 1: Write any four important characteristics of Object Oriented Programming. Give example of any one of the characteristics using C++. [O.D, 2016]
1. Encapsulation: Wrapping data and related functions into a single unit called class.
2. Abstraction: Hiding the internal details and showing only the essential features.
3. Inheritance: Acquiring properties and behaviors of one class into another.
4. Polymorphism: The ability to use a single interface to represent different types of objects or methods.
Example of Encapsulation in C++:
class Student {
private:
int rollNo;
string name;
public:
void setData(int r, string n) {
rollNo = r;
name = n;
}
void display() {
cout << rollNo << " " << name << endl;
}
};
Question 2: Find the output of the following C++ program [CBSE SQP 2015]
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
class Class {
int Cno, total;
char section;
public:
Class(int no = 1) {
Cno = no;
section = 'A';
total = 30;
}
void admission(int c = 20) {
section++;
total += c;
}
void ClassShow() {
cout << Cno << ":" << section << ":" << total << endl;
}
};
void main() {
Class C1(5), C2;
C1.admission(25);
C1.ClassShow();
C2.admission();
C1.admission(30);
C2.ClassShow();
C1.ClassShow();
}
Output:
5 : B : 55
1 : B : 50
5 : C : 85
Question 3: Obtain the output of the following C++ program, which will appear on the screen after its execution. [O.D, 2014]
class Game {
int Level, Score;
char Type;
public:
Game(char GType = 'P') {
Level = 1;
Score = 0;
Type = GType;
}
void Play(int GS);
void Change();
void Show() {
cout << Type << "@" << Level << endl;
cout << Score << endl;
}
};
void Game::Change() {
Type = (Type == 'P') ? 'G' : 'P';
}
void Game::Play(int GS) {
Score += GS;
if (Score >= 30)
Level = 3;
else if (Score >= 20)
Level = 2;
else
Level = 1;
}
void main() {
Game A('G'), B;
B.Show();
A.Play(11);
A.Change();
B.Play(25);
A.Show();
B.Show();
}
Output:
P@1
0
P@1
11
P@2
25
Question 4: Obtain the output of the following C++ program, which will appear on the screen after its execution. [O.D, 2014]
class Player {
int Score, Level;
char Game;
public:
Player(char GGame = 'A') {
Score = 0;
Level = 1;
Game = GGame;
}
void Start(int SC);
void Next();
void Disp() {
cout << Game << "@" << Level << endl;
cout << Score << endl;
}
};
void Player::Next() {
Game = (Game == 'A') ? 'B' : 'A';
}
void Player::Start(int SC) {
Score += SC;
if (Score >= 100)
Level = 3;
else if (Score >= 50)
Level = 2;
else
Level = 1;
}
void main() {
Player P, Q('B');
P.Disp();
Q.Start(75);
Q.Next();
P.Start(120);
Q.Disp();
P.Disp();
}
Output:
A@1
0
A@2
75
A@3
120
Question 5: Observe the following C++ code carefully and obtain the output. [O.D, 2013]
#include <iostream.h>
class Mausam {
int City, Temp, Humidity;
public:
Mausam(int C = 1) {
City = C;
Temp = 10;
Humidity = 63;
}
void Sun(int T) {
Temp += T;
}
void Rain(int H) {
Humidity += H;
}
void Checkout() {
cout << City << ":" << Temp << "&" << Humidity << "%" << endl;
}
};
void main() {
Mausam M, N(2);
M.Sun(5);
M.Checkout();
N.Rain(10);
N.Sun(2);
N.Checkout();
M.Rain(15);
M.Checkout();
}
Output:
1:15&63%
2:12&73%
1:15&78%
Question 6: Observe the following C++ code carefully and obtain the output. [Delhi, 2013]
#include <iostream.h>
class Aroundus {
int Place, Humidity, Temp;
public:
Aroundus(int P = 2) {
Place = P;
Humidity = 60;
Temp = 20;
}
void Hot(int T) {
Temp += T;
}
void Humid(int H) {
Humidity += H;
}
void JustSee() {
cout << Place << ":" << Temp << "&" << Humidity << "%" << endl;
}
};
int main() {
Aroundus A, B(5);
A.Hot(10);
A.JustSee();
B.Humid(15);
B.Hot(2);
B.JustSee();
A.Humid(5);
A.JustSee();
}
Output:
2:30&60%
5:22&75%
2:30&65%
Question 7: Find the output of the following program: [O.D., 2012]
#include <iostream.h>
class METRO {
int Mno, TripNo, PassengerCount;
public:
METRO(int Tmno = 1) {
Mno = Tmno;
TripNo = 0;
PassengerCount = 0;
}
void Trip(int PC = 20) {
TripNo++;
PassengerCount += PC;
}
void StatusShow() {
cout << Mno << " : " << TripNo << ":" << PassengerCount << endl;
}
};
int main() {
METRO M(5), T;
M.Trip();
T.Trip(50);
M.StatusShow();
M.Trip(30);
T.StatusShow();
M.StatusShow();
}
Output:
5 : 1 : 20
1 : 1 : 50
5 : 2 : 50
Question 8: Find the output of the following program: [Delhi, 2012]
#include <iostream.h>
class TRAIN {
int Tno, TripNo, PersonCount;
public:
TRAIN(int Tmno = 1) {
Tno = Tmno;
TripNo = 0;
PersonCount = 0;
}
void Trip(int TC = 100) {
TripNo++;
PersonCount += TC;
}
void Show() {
cout << Tno << " : " << TripNo << " : " << PersonCount << endl;
}
};
void main() {
TRAIN T(10), N;
N.Trip();
T.Show();
T.Trip(70);
N.Trip(40);
N.Show();
T.Show();
}
Output:
10 : 0 : 0
10 : 1 : 70
1 : 2 : 140
10 : 1 : 70
Question 9:
Write the output of the following C++ program code:
Note: Assume all required header files are already being included in the program.
class seminar
{
char topic[30];
int charges;
public:
seminar()
{
strcpy(topic, "Registration");
charges = 5000;
}
seminar(char t[])
{
strcpy(topic, t);
charges = 5000;
}
seminar(int c)
{
strcpy(topic, "Registration with Discount");
charges = 5000 - c;
}
void regis(char t[], int c)
{
strcpy(topic, t);
charges = charges + c;
}
void regis(int c = 2000)
{
charges = charges + c;
}
void subject(char t[], int c)
{
strcpy(topic, t);
charges = charges + c;
}
void show()
{
cout << topic << "@" << charges << endl;
}
};
void main()
{
seminar s1, s2(1000), s3("Genetic Mutation"), s4;
s1.show();
s2.show();
s1.subject("ICT", 2000);
s1.show();
s2.regis("Cyber Crime", 2500);
s2.show();
s3.regis();
s3.show();
s4 = s2;
s4.show();
}
Output:
Registration@5000
Registration with Discount@4000
ICT@7000
Cyber Crime@6500
Genetic Mutation@7000
Cyber Crime@6500
Question 10:
Find and write the output of the following C++ program code:
Note: Assume all required header files are already being included in the program.
class Product
{
int PID;
float Price;
int Qty;
public:
Product()
{
PID = 100;
Price = 20;
Qty = 100;
}
void Add(int I, float P)
{
PID = I;
Price = P;
}
void Alter(int Change, int TQ)
{
Price += Change;
Qty += TQ;
}
void Display()
{
cout << "PID: " << PID << endl;
cout << Qty << "@" << Price << endl;
}
};
void main()
{
Product P, Q, R;
P.Add(104, 50);
Q.Add(201, 30);
R.Alter(-10, 25);
P.Display();
Q.Display();
R.Display();
}
Output:
PID: 104
100@50
PID: 201
100@30
PID: 100
125@10
Topic-2: Function Overloading
Short Answer Type Questions – I (2 Marks Each)
Question 1: Answer the questions (i) and (ii) after going through the following class:
class Motor {
int MotorNo, Track;
public:
Motor(); // Function 1
Motor(int MN); // Function 2
Motor(Motor &M); // Function 3
void Allocate(); // Function 4
void Move(); // Function 5
};
void main() {
Motor M;
}
(i) Option 2 – Motor P(10);
is correct for calling Function 2.
(ii) Feature: Function Overloading, i.e., Polymorphism.
Question 2: Answer the questions (i) and (ii) after going through the following class:
class Race {
int CarNo, Track;
public:
Race(); // Function 1
Race(int CN); // Function 2
Race(Race &R); // Function 3
void Register(); // Function 4
void Drive(); // Function 5
};
void main() {
Race R;
}
(i) Option 1 – Race T(30);
is correct since the parameter is an integer.
(ii) Feature: Constructor Overloading, i.e., Polymorphism.
Question 3: What is function overloading? Write an example using C++ to illustrate the concept of function overloading.
Function overloading means having multiple functions with the same name but different parameter lists in the same scope. It implements polymorphism.
#include <stdlib.h>
#include <conio.h>
#define pi 3.14
class fn {
public:
void area(int); // Circle
void area(int, int); // Rectangle
};
void fn::area(int a) {
cout << "Area of Circle: " << pi * a * a;
}
void fn::area(int a, int b) {
cout << "Area of rectangle: " << a * b;
}
void main() {
int ch;
int a, b, r;
fn obj;
cout << "1. Area of Circle\n2. Area of Rectangle\n3. Exit\n:";
cout << "Enter your Choice:"; cin >> ch;
switch(ch) {
case 1:
cin >> r;
obj.area(r);
break;
case 2:
cin >> a >> b;
obj.area(a, b);
break;
case 3:
exit(0);
}
getch();
}
Question 4: Write the output of the following C++ code. Also, write the name of the feature of Object Oriented Programming used in the following program jointly illustrated by the functions [I] to [IV].
void Line() { // Function [I]
for(int L = 1; L <= 80; L++)
cout << "-";
cout << endl;
}
void Line(int N) { // Function [II]
for(int L = 1; L <= N; L++)
cout << "*";
cout << endl;
}
void Line(char C, int N) { // Function [III]
for(int L = 1; L <= N; L++)
cout << C;
cout << endl;
}
void Line(int M, int N) { // Function [IV]
for(int L = 1; L <= N; L++)
cout << M * L;
cout << endl;
}
void main() {
int A = 9, B = 4, C = 3;
char K = '#';
Line(K, B);
Line(A, C);
}
Output:
####
91827
Feature: Function Overloading
Question 5: Answer the questions (i) and (ii) after going through the following class:
class Test {
int Regno, Max, Min, Score;
public:
Test() { // Function 1
Regno = 101; Max = 100; Min = 40;
Score = 75;
}
Test(int Pregno, int Pscore) { // Function 2
Regno = Pregno; Max = 100; Min = 40;
Score = Pscore;
}
~Test() { // Function 3
cout << "Test Over" << endl;
}
void Display() { // Function 4
cout << Regno << ":" << Max << ":" << Min << endl;
cout << "[Score]" << Score << endl;
}
};
(i) The concept illustrated by Function 1 and Function 2 together is: Polymorphism or Constructor Overloading.
(ii) Function 3 is called a Destructor. It is invoked automatically when the object goes out of scope.
Question 6: Answer the questions (i) and (ii) after going through the following class:
class Exam {
int Rno, Maxmarks, MinMarks, Marks;
public:
Exam() { // Module 1
Rno = 101; MaxMarks = 100; MinMarks = 40; Marks = 75;
}
Exam(int Prno, int Pmarks) { // Module 2
Rno = Prno; MaxMarks = 100; MinMarks = 40;
Marks = Pmarks;
}
~Exam() { // Module 3
cout << "Exam Over" << endl;
}
void show() { // Module 4
cout << Rno << " : " << MaxMarks << ":" << MinMarks << endl;
cout << "[Marks Got]" << Marks << endl;
}
};
(i) Concept illustrated by Module 1 and Module 2: Polymorphism or Constructor Overloading.
(ii) Module 3 is referred to as a Destructor. It is invoked automatically when the object’s scope ends.
Question 7: What do you understand by Function overloading or functional polymorphism? Explain with a suitable example.
Function overloading is a feature in C++ where two or more functions can have the same name but differ in the number or type of parameters. It is an example of polymorphism as the same function behaves differently based on arguments passed.
void show() {
cout << "\nHello World!";
}
void show(char na[]) {
cout << "\nHello World! Its " << na;
}
Here, the show()
function is overloaded to work with and without a parameter, demonstrating functional polymorphism.