Chapter 3: Implementation of OOP Concepts in C++ Class 12 Computer Science NCERT Solutions
Chapter 3 takes the theoretical foundations of Object-Oriented Programming (OOP) and shows how they are practically implemented in C++. It focuses on how different OOP features like inheritance, function overloading, and polymorphism are used in real C++ programs to promote code reusability, modularity, and flexibility.
This chapter bridges the gap between basic class structures and more advanced programming patterns. By learning how to extend existing classes and redefine behavior, students are prepared to write scalable and robust programs.
What You Will Learn in Chapter 3
This chapter helps students develop a deeper understanding of OOP principles by applying them in C++. Students will explore how inheritance allows class extension, how function overloading enables multiple functions with the same name, and how polymorphism allows the same interface to behave differently.
Key Topics Covered
Inheritance in C++
Concept of inheritance: reusability of code
Types of inheritance: single, multiple, multilevel, hierarchical, hybrid
Syntax for derived classes and use of access specifiers
Visibility modes (public, private, protected) and their impact on inheritance
Use of
base_class::member
syntax
Function Overloading
Defining multiple functions with the same name but different parameter types or numbers
Compile-time polymorphism using function overloading
Rules for valid overloading (difference in type, number, or order of parameters)
Constructor and Destructor in Inheritance
Calling base class constructor using derived class constructor
Order of constructor and destructor invocation in inheritance hierarchy
Pointers to Objects
Creating object pointers
Accessing members using arrow operator
->
Using
new
anddelete
with object pointers
Dynamic Binding and Virtual Functions
Introduction to runtime polymorphism
Role of virtual functions in achieving dynamic dispatch
Base class pointer calling derived class method
Concept of late binding and its benefits
Abstract Classes and Pure Virtual Functions
Declaring a class as abstract using pure virtual functions
Use of abstract classes as interfaces
Implementing polymorphic behavior using virtual functions
Object Composition and Containment
Including objects of one class in another (HAS-A relationship)
Difference between inheritance (IS-A) and containment (HAS-A)
Download Chapter 3 Solutions PDF – Implementation of OOP Concepts in C++
Our PDF includes:
Complete NCERT textbook question solutions with step-by-step explanations
Real C++ code examples for inheritance, overloading, and polymorphism
Diagrams illustrating inheritance hierarchies and virtual function flow
Output-based questions and concept-clarifying programs
Syntax tables for inheritance and function declarations
Highlights of Our NCERT Solutions
Conceptual clarity with clean, working code snippets
Easy-to-understand charts for inheritance types and access control
Example programs that demonstrate how virtual functions and object pointers work
Debugging strategies for common errors in inheritance and polymorphism
Clear comparison between compile-time and runtime polymorphism
Recommended Preparation Tips
Practice writing programs using all types of inheritance
Revise the role and usage of virtual functions and abstract classes
Memorize constructor calling order in inheritance
Practice function o
verloading with varied parameter patternsUse object pointers to call class member functions
Additional Study Resources
Visual maps of inheritance hierarchies and class relationships
Worksheets for identifying valid function overloading cases
Flowcharts for virtual function resolution at runtime
Flashcards: types of inheritance, polymorphism types, keywords
Practice questions from CBSE sample papers and PYQs
Mastering Chapter 3 – Implementation of OOP Concepts in C++
Mastery of this chapter helps students move from writing simple class-based programs to designing complex applications using inheritance, polymorphism, and object relationships. These concepts are foundational for understanding how large-scale software systems are built and maintained.
By practicing these advanced OOP features, students not only prepare for board exams but also strengthen their programming skills for competitive exams and future projects in software development.
Class 12 Computer Science (C++) – Chapter 3: Implementation of OOP Concepts in C++
Short Answer Type Questions
Question 1: Observe the following C++ code and answer the question (i) and (ii): [Delhi, 2015]
class Traveller {
long PNR;
char TName[20];
public:
Traveller() // Function 1
{
cout << "Ready" << endl;
}
void Book(long P, char N[]) // Function 2
{
PNR = P;
strcpy(TName, N);
}
void Print() // Function 3
{
cout << PNR << TName << endl;
}
~Traveller() // Function 4
{
cout << "Booking cancelled!" << endl;
}
};
void main() {
Traveller T;
T.Book(1234567, "Ravi"); // Line 1
T.Print(); // Line 2
}
(i) Function 2 and Function 3 are executed using:
Line 1: T.Book(1234567, "Ravi");
Line 2: T.Print();
(ii) The function executed at the end of }
is Function 4: ~Traveller()
.
It is referred to as a Destructor.
Question 2: Write the definition of a class PIC in C++ with the following description: [Delhi, 2015]
Category & Location Mapping:
- Classic → Amina
- Modern → Jim Plaq
- Antique → Ustad Khan
class PIC {
int Pno;
char Category[20];
char Location[20];
void FixLocation();
public:
void Enter();
void SeeAll();
};
void PIC::FixLocation() {
if (strcmpi(Category, "Classic") == 0)
strcpy(Location, "Amina");
else if (strcmpi(Category, "Modern") == 0)
strcpy(Location, "Jim Plaq");
else if (strcmpi(Category, "Antique") == 0)
strcpy(Location, "Ustad Khan");
}
void PIC::Enter() {
cin >> Pno;
gets(Category);
FixLocation();
}
void PIC::SeeAll() {
cout << Pno << " " << Category << " " << Location << endl;
}
Question 3: What is the difference between the members in private visibility mode and the members in protected visibility mode inside a class? Also, give a suitable C++ code to illustrate both. [O.D, 2012]
Private members: Accessible only within the same class.
Protected members: Accessible within the same class and in derived classes.
class Me {
public:
int a, b;
protected:
int c, d; // Protected members
private:
int e, f; // Private members
};
Short Answer Type Questions – II (3 Marks Each)
Question 1: Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included. [O.D, 2016]
class BOOK {
long Code;
char Title[20];
float Price;
public:
BOOK() {
cout << "Bought" << endl;
Code = 10;
strcpy(Title, "NoTitle");
Price = 100;
}
BOOK(int C, char T[], float P) {
Code = C;
strcpy(Title, T);
Price = P;
}
void Update(float P) {
Price += P;
}
void Display() {
cout << Code << ":" << Title << ":" << Price << endl;
}
~BOOK() {
cout << "Book Discarded!" << endl;
}
};
void main() {
BOOK B, C(101, "Truth", 350);
for (int I = 0; I < 4; I++) {
B.Update(50);
C.Update(20);
B.Display();
C.Display();
}
}
(i) The concept illustrated by Function 1 and 2: Constructor Overloading / Polymorphism
(ii) Message “Book Discarded!” will be displayed: 2 times
Responsible line: Line 9 (end of main()
)
Question 2: Write the definition of a class CITY in C++ with the following description: [O.D, 2016]
class CITY {
int CCode;
char CName[20];
long int Pop;
float KM;
float Density;
void DenCal(); // Calculate Density = Pop / KM
public:
void Record(); // Input values and call DenCal
void View(); // Display values and check for population density
};
void CITY::DenCal() {
Density = Pop / KM;
}
void CITY::Record() {
cin >> CCode;
gets(CName);
cin >> Pop;
cin >> KM;
DenCal();
}
void CITY::View() {
cout << CCode << " " << CName << " " << Pop << " " << KM << " " << Density << endl;
if (Density > 10000)
cout << "Highly Populated City" << endl;
}
Question 3: Write the definition of a class METROPOLIS in C++ with following description: [Delhi, 2016]
class METROPOLIS {
int MCode;
char MName[20];
long int MPop;
float Area;
float PopDens;
void CalDen() {
PopDens = MPop / Area;
}
public:
void Enter() {
cin >> MCode;
cin.ignore();
cin.getline(MName, 20);
cin >> MPop;
cin >> Area;
CalDen();
}
void viewALL() {
cout << MCode << " " << MName << " " << Area << " " << PopDens;
if (PopDens > 12000)
cout << " Highly Populated Area";
}
};
Question 4: Answer the questions (i) to (iv) based on the following: [Delhi, 2016]
class PRODUCT {
int Code;
char Item[20];
protected:
float Qty;
public:
PRODUCT();
void GetIn();
void Show();
};
class WHOLESALER {
int WCode;
protected:
char Manager[20];
public:
WHOLESALER();
void Enter();
void Display();
};
class SHOWROOM : public PRODUCT, private WHOLESALER {
char Name[20];
public:
SHOWROOM();
void Input();
void View();
};
(i) The inheritance used: Multiple Inheritance
(ii) Data members accessible by SHOWROOM objects: Name, Qty, Manager
(iii) Functions accessible through SHOWROOM objects: Input(), View(), GetIn(), Show()
(iv) Constructors invoked while creating SHOWROOM object: PRODUCT(), WHOLESALER(), SHOWROOM()
Question 5:
Write the definition of a function Fixpay(float Pay[], int N)
in C++: [O.D., 2016]
void Fixpay(float pay[], int N) {
for (int i = 0; i < N; i++) {
if (pay[i] < 100000)
pay[i] += pay[i] * 0.35;
else if (pay[i] < 200000)
pay[i] += pay[i] * 0.30;
else
pay[i] += pay[i] * 0.20;
}
}
Long Answer Type Questions
Question 1: Define a class Candidate in C++: [Delhi, 2015]
class Candidate {
long Rno;
char Cname[20];
float Agg_marks;
char Grade;
void setGrade() {
if (Agg_marks >= 80)
Grade = 'A';
else if (Agg_marks >= 65)
Grade = 'B';
else if (Agg_marks >= 50)
Grade = 'C';
else
Grade = 'D';
}
public:
Candidate() {
Rno = 0;
strcpy(Cname, "N.A.");
Agg_marks = 0.0;
}
void Getdata() {
cout << "Registration No: "; cin >> Rno;
cout << "Name: "; cin >> Cname;
cout << "Aggregate Marks: "; cin >> Agg_marks;
setGrade();
}
void dispResult() {
cout << "Registration No: " << Rno << endl;
cout << "Name: " << Cname << endl;
cout << "Aggregate Marks: " << Agg_marks << endl;
cout << "Grade: " << Grade << endl;
}
};
Question 2: Define a class Customer with the following specifications: [CBSE SQP 2015]
class Customer {
int Customer_no;
char Customer_name[20];
int Qty;
float Price, TotalPrice, Discount, Netprice;
public:
Customer() {
Customer_no = 111;
strcpy(Customer_name, "Leena");
Qty = 0;
Price = Discount = Netprice = 0;
}
void Input() {
cin >> Customer_no;
cin.ignore();
cin.getline(Customer_name, 20);
cin >> Qty >> Price;
Caldiscount();
}
void Caldiscount() {
TotalPrice = Price * Qty;
if (TotalPrice >= 50000)
Discount = 0.25 * TotalPrice;
else if (TotalPrice >= 25000)
Discount = 0.15 * TotalPrice;
else
Discount = 0.10 * TotalPrice;
Netprice = TotalPrice - Discount;
}
void Show() {
cout << "Customer No: " << Customer_no << endl;
cout << "Name: " << Customer_name << endl;
cout << "Qty: " << Qty << endl;
cout << "Total Price: " << TotalPrice << endl;
cout << "Net Price: " << Netprice << endl;
}
};
Question 3: Define a class CONTEST:
class CONTEST {
int Eventno;
char Description[30];
int Score;
char Qualified;
public:
CONTEST() {
Eventno = 11;
strcpy(Description, "School level");
Score = 100;
Qualified = 'N';
}
void Input() {
cin >> Eventno;
cin.ignore();
cin.getline(Description, 30);
cin >> Score;
}
void Award(int cutoffscore) {
Qualified = (Score > cutoffscore) ? 'Y' : 'N';
}
void Displaydata() {
cout << "Event No: " << Eventno << endl;
cout << "Description: " << Description << endl;
cout << "Score: " << Score << endl;
cout << "Qualified: " << Qualified << endl;
}
};
Question 4: Derive class 'District' from 'State':
class State {
protected:
int tp;
public:
State() {
tp = 0;
}
void inctp() {
tp++;
}
int gettp() {
return tp;
}
};
class District : public State {
private:
char distname[50];
long population;
public:
void dinput() {
cin.ignore();
cin.getline(distname, 50);
cin >> population;
}
void doutput() {
cout << "District Name: " << distname << endl;
cout << "Population: " << population << endl;
}
};
Question 5:
Define a function Fixpay(float Pay[], int N)
in C++ to modify the salary array according to the given rules.
void Fixpay(float Pay[], int N) {
for (int i = 0; i < N; i++) {
if (Pay[i] < 100000)
Pay[i] += Pay[i] * 0.35;
else if (Pay[i] >= 100000 && Pay[i] < 200000)
Pay[i] += Pay[i] * 0.30;
else
Pay[i] += Pay[i] * 0.20;
}
}
Question 6: Candidate Class Define a class Candidate in C++ with the following specifications:
class Candidate {
long Rno;
char Cname[20];
float Agg_marks;
char Grade;
void setGrade() {
if (Agg_marks >= 80)
Grade = 'A';
else if (Agg_marks >= 65)
Grade = 'B';
else if (Agg_marks >= 50)
Grade = 'C';
else
Grade = 'D';
}
public:
Candidate() {
Rno = 0;
strcpy(Cname, "N.A");
Agg_marks = 0.0;
}
void Getdata() {
cout << "Enter Registration Number: ";
cin >> Rno;
cout << "Enter Name: ";
cin >> Cname;
cout << "Enter Aggregate Marks: ";
cin >> Agg_marks;
setGrade();
}
void dispResult() {
cout << "Registration No: " << Rno << endl;
cout << "Name: " << Cname << endl;
cout << "Aggregate Marks: " << Agg_marks << endl;
cout << "Grade: " << Grade << endl;
}
};
Question 7: Customer Class Define a class Customer with the following specifications:
class Customer {
int Customer_no;
char Customer_name[20];
int Qty;
float Price, TotalPrice, Discount, Netprice;
public:
Customer() {
Customer_no = 111;
strcpy(Customer_name, "Leena");
Qty = 0;
Price = 0;
Discount = 0;
Netprice = 0;
}
void Input() {
cout << "Enter Customer No: ";
cin >> Customer_no;
cout << "Enter Customer Name: ";
gets(Customer_name);
cout << "Enter Quantity: ";
cin >> Qty;
cout << "Enter Price: ";
cin >> Price;
Caldiscount();
}
void Caldiscount() {
TotalPrice = Price * Qty;
if (TotalPrice >= 50000)
Discount = 0.25 * TotalPrice;
else if (TotalPrice >= 25000)
Discount = 0.15 * TotalPrice;
else
Discount = 0.10 * TotalPrice;
Netprice = TotalPrice - Discount;
}
void Show() {
cout << "Customer No: " << Customer_no << endl;
cout << "Customer Name: " << Customer_name << endl;
cout << "Quantity: " << Qty << endl;
cout << "Total Price: " << TotalPrice << endl;
cout << "Net Price: " << Netprice << endl;
}
};
Question 8: Contest Class Define a class CONTEST with the following description:
class CONTEST {
private:
int Eventno;
char Description[30];
int Score;
char Qualified;
public:
CONTEST() {
Eventno = 11;
strcpy(Description, "School level");
Score = 100;
Qualified = 'N';
}
void Input() {
cout << "Enter Event No: ";
cin >> Eventno;
cout << "Enter Description: ";
gets(Description);
cout << "Enter Score: ";
cin >> Score;
}
void Award(int cutoffscore) {
if (Score > cutoffscore)
Qualified = 'Y';
else
Qualified = 'N';
}
void Displaydata() {
cout << "Event No: " << Eventno << endl;
cout << "Description: " << Description << endl;
cout << "Score: " << Score << endl;
cout << "Qualified: " << Qualified << endl;
}
};
Question 9: District Class Consider the following State class. Write a derived class District to inherit the state data and include additional members.
class State {
protected:
int tp;
public:
State() {
tp = 0;
}
void inctp() {
tp++;
}
int gettp() {
return tp;
}
};
class District : public State {
private:
char distname[50];
long population;
public:
void dinput() {
cout << "Enter District Name: ";
gets(distname);
cout << "Enter Population: ";
cin >> population;
}
void doutput() {
cout << "District Name: " << distname << endl;
cout << "Population: " << population << endl;
}
};
Question 10: CABS Class Define a class CABS to manage a cab system with the following specifications:
class CABS {
int CNo;
char Type;
int PKM;
float Dist;
public:
CABS() {
Type = 'A';
CNo = 1111;
}
void Charges() {
if (Type == 'A') PKM = 25;
else if (Type == 'B') PKM = 20;
else if (Type == 'C') PKM = 15;
}
void Register() {
cout << "Enter Cab No: ";
cin >> CNo;
cout << "Enter Type (A/B/C): ";
cin >> Type;
Charges();
}
void ShowCab() {
cout << "Enter Distance: ";
cin >> Dist;
cout << "Cab No: " << CNo << endl;
cout << "Type: " << Type << endl;
cout << "PKM: " << PKM << endl;
cout << "Amount: " << PKM * Dist << endl;
}
};
Question 11: Find the output of the following program:
#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;
}
};
void 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 12: RESTRA Class Define a class RESTRA in C++ with the following description:
class RESTRA {
char FoodCode;
char Food[20], FType[20], Sticker[20];
void GetSticker() {
if (strcmp(FType, "Vegetarian") == 0)
strcpy(Sticker, "GREEN");
else if (strcmp(FType, "ContainsEgg") == 0)
strcpy(Sticker, "YELLOW");
else if (strcmp(FType, "Non-Vegetarian") == 0)
strcpy(Sticker, "RED");
}
public:
void GetFood() {
cout << "Enter FoodCode, Food, Food Type: ";
cin >> FoodCode;
gets(Food);
gets(FType);
GetSticker();
}
void ShowFood() {
cout << "Enter Food Code, Food, FoodType, Sticker:" << endl;
cout << FoodCode << endl;
puts(Food);
puts(FType);
puts(Sticker);
}
};
Question 13: Find the output of the following program:
#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();
N.Trip(70);
N.Trip(40);
N.show();
T.show();
}
Output:
10 : 0 : 0
1 : 2 : 140
10 : 0 : 0
Question 14: Candidate Class Define a class Candidate in C++ with the following description:
class Candidate {
long RNo;
char Name[40];
float Score;
char Remarks[20];
void AssignRem() {
if (Score >= 50) {
strcpy(Remarks, "Selected");
} else {
strcpy(Remarks, "Not Selected");
}
}
public:
void Enter() {
cout << "Enter the values for RNo, Name, Score: ";
cin >> RNo;
gets(Name);
cin >> Score;
AssignRem();
}
void Display() {
cout << RNo << " " << Score << endl;
puts(Name);
puts(Remarks);
}
};
Question 15: Applicant Class Define a class Applicant in C++ with the following description:
class Applicant {
long ANo;
char Name[40];
float Agg;
char Grade;
void GradeMe() {
if (Agg >= 80) Grade = 'A';
else if (Agg >= 65) Grade = 'B';
else if (Agg >= 50) Grade = 'C';
else Grade = 'D';
}
public:
void Enter() {
cout << "Enter the values for ANo, Name, Agg: ";
cin >> ANo;
gets(Name);
cin >> Agg;
GradeMe();
}
void Result() {
cout << ANo << " " << Agg << endl;
puts(Name);
cout << Grade << endl;
}
};
Question 16: ITEM Class Define a class ITEM in C++ with the following description:
class Item {
int Code;
char Iname[20];
float Price;
int Qty;
float Offer;
void GetOffer() {
if (Qty <= 50) {
Offer = 0;
} else if (Qty <= 100) {
Offer = 5; // Or Offer = 0.05;
} else {
Offer = 10; // Or Offer = 0.1;
}
}
public:
void GetStock() {
cin >> Code;
gets(Iname);
cin >> Price >> Qty;
GetOffer();
}
void ShowItem() {
cout << Code << " " << Iname << " " << Price << " " << Qty << " " << Offer << endl;
}
};
Question 17: Stock Class Define a class Stock in C++ with the following description:
class Stock {
int ICode, Qty;
char Item[20];
float Price, Discount;
void FindDisc() {
if (Qty <= 50) {
Discount = 0;
} else if (Qty <= 100) {
Discount = 5; // Or Discount = 0.05;
} else {
Discount = 10; // Or Discount = 0.1;
}
}
public:
void Buy() {
cin >> ICode;
gets(Item);
cin >> Price >> Qty;
FindDisc();
}
void ShowAll() {
cout << ICode << '\t' << Item << '\t' << Price << '\t' << Qty << '\t' << Discount << endl;
}
};
Short Answer Type Questions – 1 (2 Marks Each)
Question 1: Answer the questions (i) and (ii) after going through the following class:
class Hospital {
int Pno, Dno;
public:
Hospital(int PN); //Function 1
Hospital(); //Function 2
Hospital(Hospital &H); //Function 3
void In(); //Function 4
void Disp(); //Function 5
};
void main() {
Hospital H(20); //Statement 1
}
(i) Which function will get executed when Statement 1 is executed?
Answer: Function 1 will get executed.
Explanation: The statement Hospital H(20);
invokes the parameterized constructor Hospital(int PN)
, which is Function 1.
(ii) Write a statement to declare a new object G
with reference to already existing object H
using Function 3.
Answer: Hospital G(H);
Explanation: This creates a new object G
by copying the state of object H
using the copy constructor Hospital(H)
, which is Function 3.
Question 2: Answer the questions (i) and (ii) after going through the following class:
class Health {
int PId, Did;
public:
Health(int PPID); //Function 1
Health(); //Function 2
Health(Health &H); //Function 3
void Entry(); //Function 4
void Display(); //Function 5
};
void main() {
Health H(20); //Statement 1
}
(i) Which function will get executed when Statement 1 is executed?
Answer: Function 1 will get executed.
Explanation: The statement Health H(20);
invokes the constructor Health(int PPID)
, which is Function 1.
(ii) Write a statement to declare a new object G
with reference to already existing object H
using Function 3.
Answer: Health G(H);
Explanation: This statement uses the copy constructor to create a new object G
using the existing object H
.
Question 3: What is the relationship between a class and an object? Illustrate with a suitable example.
Answer: A class is a blueprint or template that defines the structure and behavior of objects. An object is an instance of a class, holding actual values for the class attributes and using its methods.
Example:
class Box {
int length, breadth, height;
public:
void SetDimensions(int l, int b, int h) {
length = l;
breadth = b;
height = h;
}
void Display() {
cout << "Length: " << length
<< ", Breadth: " << breadth
<< ", Height: " << height << endl;
}
};
int main() {
Box a; // 'a' is an object of class Box
a.SetDimensions(10, 20, 30);
a.Display();
return 0;
}