Class 12 Computer Science (C++) – Chapter 2 Object Oriented Programming in C++
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
Question 1:
Differentiate between data abstraction and data hiding. [Delhi, 2015]
Answer:
Data Hiding: It refers to restricting access to the internal data of a class to prevent accidental or intentional modification. It is achieved by declaring class members as private
.
Data Abstraction: It refers to showing only the essential features of an object to the outside world and hiding the background details. This is done using public
member functions that interact with private data.
Question 2:
How encapsulation and abstraction are implemented in C++ language? Explain with an example. [CBSE SQP 2015]
Answer:
Abstraction: Representing only essential features and hiding unnecessary details.
Encapsulation: Wrapping data and functions into a single unit called class. A class allows abstraction by exposing only necessary functions via public
access.
class Rectangle { int L, B; // Encapsulated data public: void Input() { cin >> L >> B; } void Output() { cout << L << B; } };
Here, only Input()
and Output()
are exposed, thus achieving abstraction, while data members are hidden inside the class.
Question 3:
Explain data hiding with an example. [CBSE Comptt., 2014]
Answer:
Data Hiding: The concept of keeping internal object details private to protect them from unauthorized access.
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 Hiding: Hiding internal data from external access.
Encapsulation: Combining data and functions inside a class.
#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 (index 0 not used).
MCA will always be displayed first.
(ii) 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 Options: (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(); }
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(); }
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.
(ii) Function 3 is a destructor, which is called automatically when an 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]
Answer:
- 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(); }
Answer:
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(); }
Answer:
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(); }
Answer:
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(); }
Answer:
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(); }
Answer:
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(); }
Answer:
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();
}
Answer:
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();
}
Answer:
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();
}
Answer:
PID: 104
100@50
PID: 201
100@30
PID: 100
125@10
TOPIC-2
Function Overloading
Short Answer Type Questions-1 [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) Out of the following, which of the option is correct for calling Function 2?
Option 1 – Motor N (M);
Option 2 – Motor P (10);
(ii) Name the feature of Object Oriented Programming, which is illustrated by Function 1, Function 2, and Function 3 combined together.
Answer:
(i) Option 2 is correct for calling Function 2. [1]
(ii) Function overloading, i.e., Polymorphism. [1]
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) Out of the following, which of the option is correct for calling Function 2?
Option 1 – Race T (30);
Option 2 – Race U (R);
(ii) Name the feature of Object Oriented Programming, which is illustrated by Function 1, Function 2, and Function 3 combined together.
Answer:
(i) Option 1 – Race T (30); is correct since the parameter to T is integer. [1]
(ii) When Functions – Function 1, Function 2, and Function 3 are combined together, it is referred to as constructor overloading, i.e., Polymorphism. [1]
Question 3:
What is function overloading? Write an example using C++ to illustrate the concept of function overloading.
Answer:
Function Overloading
In C++, we can declare different functions with the same name. This property is called function overloading. Function overloading implements polymorphism.
Example:
# include
#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 << \"n1. Area of Circlen2. Area of Rectanglen3. Exitn:\";
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].
#include
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);
}
Answer:
Output:
####
91827
Name of Feature: Function Overloading [1/2 Mark for each correct output and feature]
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) As per object-oriented programming, which concept is illustrated by function 1 and function 2 together?
Answer:
Polymorphism or Function Overloading or Constructor Overloading [1 Mark for naming the concept correctly]
(ii) What is Function 3 specially referred to as? When do you think Function 3 will be invoked/called?
Answer:
Function 3 is referred to as a Destructor. It will be invoked when the scope of the object gets over. [1/2 Mark for identifying Destructor, 1/2 Mark for mentioning when it is invoked]
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) As per Object Oriented Programming, which concept is illustrated by Module 1 and Module 2 together?
Answer:
Polymorphism or Constructor Overloading [1 Mark for naming the correct concept]
(ii) What is Module 3 referred to as? When do you think Module 3 will be invoked/called?
Answer:
Module 3 is referred to as a Destructor. It will be invoked as soon as the scope of the object gets over. [1/2 Mark for identifying Destructor, 1/2 Mark for mentioning when it is called/invoked]
Question 7:
What do you understand by Function overloading or functional polymorphism? Explain with a suitable example.
Answer:
Function overloading is a method of using the same function or method to work using different sets of input. It is one of the examples of polymorphism, where more than one function carrying a similar name behaves differently with a different set of parameters passed to them.
Example:
void show()
{
cout << \"nHello World!\";
}
void show(char na[])
{
cout << \"nHello World! Its \" << na;
}
[1 Mark for correct explanation of Function overloading, 1 Mark for suitable example of Function overloading]