Class 12 Computer Science (C++) – Chapter 3 Implementation of OOP Concepts in C++
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
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; } };
(i) Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code:
void main() { Traveller T; T.Book(1234567, \"Ravi\"); // Line 1 T.Print(); // Line 2 }
(ii) Which function will be executed at }
(Stops here)? What is this function referred as?
Answer:
- Function executed: Function 4 →
~Traveller()
- It is referred to as: Destructor
Question 2:
Write the definition of a class PIC in C++ with the following description: [Delhi, 2015]
Category | Location |
---|---|
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]
Answer:
- Private members: Can only be accessed by functions within the same class.
- Protected members: Can be accessed within the same class and by 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() // Member Function 1 { cout << \"Bought\" << endl; Code = 10; strcpy(Title, \"NoTitle\"); Price = 100; } BOOK(int C, char T[], float P) // Member Function 2 { Code = C; strcpy(Title, T); Price = P; } void Update(float P) // Member Function 3 { Price += P; } void Display() // Member Function 4 { cout << Code << \":\" << Title << \":\" << Price << endl; } ~BOOK() // Member Function 5 { cout << \"Book Discarded!\" << endl; } }; void main() { // Line 1 BOOK B, C(101, \"Truth\", 350); // Line 2 for (int I = 0; I < 4; I++) { // Line 3 B.Update(50); // Line 4 C.Update(20); // Line 5 B.Display(); // Line 6 C.Display(); // Line 7 } // Line 8 } // Line 9
(i) Which specific concept of object-oriented programming is illustrated by Member Function 1 and Member Function 2 combined together?
Answer: Polymorphism (Constructor Overloading)
(ii) How many times will the message “Book Discarded!” be displayed after executing the above C++ code? Which line is responsible for displaying this message?
- Answer: 2 times
- Responsible line: Line 9
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) Multiple Inheritance
- (ii) Name, Qty, Manager
- (iii) Input(), View(), GetIn(), Show()
- (iv) 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: Fixpay Function
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 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 class METRO { int Mno, TripNo, PassengerCount; public: METRO(int Tmno=l) { 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(); }
Answer:
5:1:20 1:1:50 5:2:50
Question 12:
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\"); } if (strcmp(FType, \"ContainsEgg\") == 0) { strcpy(Sticker, \"YELLOW\"); } 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: \"; cout << FoodCode; puts(Food); puts(FType); puts(Sticker); } };
Question 13:
Find the output of the following program:
#include class TRAIN { int Tno, TripNo, PersonCount, T, N; 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(); }
Answer:
10:0:0 1:2:140 10:1:70
Question 14:
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; puts(Name); puts(Remarks); } };
Question 15:
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\'; if (Agg < 80 && Agg >= 65) Grade = \'B\'; if (Agg < 65 && Agg >= 50) Grade = \'C\'; if (Agg < 50) 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; puts(Name); puts(Grade); } };
Question 16:
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; } };
Question 17:
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; cin >> 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; class Hospital 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 of the functions out of Function 1, 2, 3, 4 or 5 will get executed when the Statement 1 is executed in the above code?
Answer: Function 1 will get executed.
Explanation: When Hospital H(20);
is executed, it invokes the constructor Hospital(int PN)
which is Function 1. This constructor initializes the object H
with the given value of 20 for the Pno
.
(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 statement creates a new object G
by copying the state of the existing 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 of the functions out of Function 1, 2, 3, 4 or 5 will get executed when the Statement 1 is executed in the above code?
Answer: Function 1 will get executed.
Explanation: When Health H(20);
is executed, it invokes the constructor Health(int PPID)
which is Function 1. This constructor initializes the object H
with the given value of 20 for the PId
.
(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 creates a new object G
by copying the state of the existing object H
using the copy constructor Health(H)
which is Function 3.
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 for creating objects. It defines the properties (data members) and behaviors (member functions) that the objects of that class will have. An object is an instance of a class and represents a specific entity with its own data.
For Example:
class Box { int length, breadth, height; public: void Display() { cout << \"Length: \" << length << \", Breadth: \" << breadth << \", Height: \" << height << endl; } void SetDimensions(int l, int b, int h) { length = l; breadth = b; height = h; } }; int main() { Box a; // \'a\' is an object of class Box a.SetDimensions(10, 20, 30); // Set dimensions for \'a\' a.Display(); // Display dimensions of \'a\' }