CBSE Class 12 Computer Science C++: Chapter 3 - Implementation of OOP Concepts in C++ NCERT Solutions

NCERT Solutions PDF Class 12 PDF

This section provides detailed NCERT Solutions for Chapter 3 of the CBSE Class 12 Computer Science (C++) syllabus, focusing on the Implementation of OOP Concepts in C++. It covers fundamental aspects of Object-Oriented Programming (OOP) using C++, with a specific emphasis on classes. The solutions explain how to define classes, declare data members and member functions, and manage access specifiers. Key concepts like constructors, destructors, and member function definitions are illustrated through practical examples. These solutions are designed to help students understand the practical application of OOP principles in C++ and prepare effectively for their board examinations by clarifying complex topics and providing step-by-step explanations.

Quick info

BoardCBSE
ClassClass 12
SubjectComputer Science (C++)
Session2026
LanguageEnglish
TypeNCERT Solutions
ChapterChapter 3

Chapter summary

Chapter 3 of the CBSE Class 12 Computer Science (C++) curriculum delves into the practical implementation of Object-Oriented Programming (OOP) concepts, primarily focusing on classes. The NCERT Solutions provided here break down the definition of classes, private and public members, and the roles of constructors and destructors. It includes exercises that require students to write C++ code for class definitions and member functions, ensuring a solid grasp of how to structure OOP programs.

Learning outcomes

  • Understand the definition and structure of a C++ class.
  • Differentiate between private and public members of a class.
  • Identify and explain the purpose of constructors and destructors.
  • Implement member functions for class objects.
  • Apply OOP concepts to solve programming problems in C++.

Topics covered

Paper topics

  • Classes in C++
  • Object-Oriented Programming (OOP)
  • Data Members
  • Member Functions
  • Constructors
  • Destructors
  • Access Specifiers (Public, Private)
  • Class Definition
  • Object Creation
  • Function Calls

Important topics

  • Class Definition and Structure
  • Constructors and Destructors
  • Public vs. Private Members
  • Member Function Implementation

PDF preview

Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.

Loading document …
Page of
Loading page …

Questions and Solutions

Question 1

Observe the following C++ code and answer the questions (i) and (ii):
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;
  //Line 1
  //Line 2
} //Stops here
Solution:

To execute Function 2 (Book) and Function 3 (print) for the object T, we need to call these member functions using the object T. The constructor `Traveller()` is automatically called when object T is declared. The destructor `~Traveller()` is called when the program execution reaches the closing brace `}` of the `main` function.

  1. Line 1: To execute Function 2 (Book), we call it on object T, passing the required arguments for PNR and TName.

    T.Book(1234567, "Ravi");

  2. Line 2: To execute Function 3 (print), we call it on object T.

    T.print();

(ii) Which function will be executed at `}//Stops here`? What is this function referred as?

The function that will be executed when the program execution reaches the closing brace `}` of the `main` function is Function 4, which is the destructor of the `Traveller` class.

This function is referred to as a destructor. It is automatically called when an object of the class goes out of scope or is explicitly deleted, performing cleanup tasks.

Question 2

Write the definition of a class PIC in C++ with the following description:

Private Members

  • Pno // Data member for Picture Number (an integer)
  • Category // Data member for picture category (a string)
  • Location // Data member for Exhibition Location (a string)
  • FixLocation // A member function to assign Exhibition Location as per category

The assignment logic for `FixLocation` is:

Category Location
Classic Amina
Modern Jim Plaq
Antique Ustad Khan

Public Members

  • Enter() // A function to allow user to enter values for Pno, Category and call FixLocation() function
  • SeeAll() // A function to display all the data members.
Solution:

Here is the C++ class definition for `PIC` as per the given description:

#include <iostream>
#include <cstring>

class PIC {
  int Pno;
  char Category[20];
  char Location[20];
  void FixLocation( );

public:
  void Enter( );
  void SeeAll( );
};

void PIC::FixLocation( )
{
  // Using strcmpi for case-insensitive comparison
  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");
  else
    strcpy(Location, "Unknown"); // Default case if category doesn't match
}

void PIC::Enter( )
{
  std::cout << "Enter Picture Number: ";
  std::cin >> Pno;
  std::cout << "Enter Category (Classic/Modern/Antique): ";
  std::cin >> Category;
  
  // Call FixLocation to set the location based on category
  FixLocation( );
}

void PIC::SeeAll( )
{
  std::cout << "Picture Number: " << Pno << std::endl;
  std::cout << "Category: " << Category << std::endl;
  std::cout << "Location: " << Location << std::endl;
}

Explanation:

  • The `PIC` class has private data members `Pno`, `Category`, and `Location` to store picture details.
  • The `FixLocation` private member function is responsible for setting the `Location` based on the `Category`. It uses `strcmpi` for case-insensitive string comparison to match the category and `strcpy` to assign the location.
  • The public member function `Enter()` prompts the user to input the picture number and category, and then calls `FixLocation()` to determine the exhibition location.
  • The public member function `SeeAll()` displays all the data members (`Pno`, `Category`, `Location`) of the object.

Common mistakes

  • Incorrectly calling member functions or constructors/destructors.
  • Misunderstanding the scope and accessibility of private vs. public members.
  • Errors in string manipulation functions like strcpy or case-insensitive comparisons.
  • Forgetting to define or correctly implement member functions outside the class definition.

Revision tips

  • Review the syntax for defining classes and member functions thoroughly.
  • Practice writing code for constructors and destructors for different scenarios.
  • Pay close attention to access specifiers (private, public) and their implications.
  • Work through the provided examples to understand function calls and object creation.

Practice MCQs

Q1. In C++, what is the primary purpose of a class?

Q2. Which of the following is automatically called when an object of a class goes out of scope?

Q3. What does the `public:` access specifier in a C++ class indicate?

Q4. In the context of OOP, what is an object?

Frequently asked questions

What is the main focus of Chapter 3 in CBSE Class 12 Computer Science (C++)?

Chapter 3 focuses on the practical implementation of Object-Oriented Programming (OOP) concepts in C++, with a strong emphasis on understanding and defining classes.

What are constructors and destructors in C++ classes?

Constructors are special member functions that are automatically called when an object is created, used for initialization. Destructors are special member functions called when an object is destroyed, used for cleanup.

How do access specifiers like `public` and `private` affect class members?

The `public` specifier makes members accessible from anywhere, while `private` members are only accessible from within the class itself, enforcing encapsulation.

Are the questions in these solutions modified from the original NCERT text?

The questions remain the same in terms of the problem asked and numbering. However, the wording may be expanded for clarity. The solutions are entirely rewritten to be more detailed and helpful.

How can these NCERT Solutions help with exam preparation?

These solutions provide clear, step-by-step explanations for each question, helping students understand the underlying concepts of OOP in C++ and how to apply them, which is crucial for exam success.

Content reviewed by the NCERT Help team. Editorial Team and update policy

NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.