CBSE Class 12 Computer Science C++: Constructor and Destructor NCERT Solutions

NCERT Solutions PDF Class 12 PDF

This chapter delves into the fundamental concepts of Constructors and Destructors in C++ programming, crucial for object-oriented programming (OOP). The NCERT Solutions for Class 12 Computer Science provide detailed explanations and examples for understanding how constructors initialize objects and how destructors clean up resources. Key topics covered include the characteristics of constructor functions, the purpose and implementation of copy constructors, and the distinction between default and parameterized constructors. The solutions also explain the role of destructors in managing object lifecycles. These solutions are designed to help students grasp these essential OOP principles, clarify doubts, and prepare effectively for their board examinations by offering step-by-step problem-solving approaches.

Quick info

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

Chapter summary

This chapter focuses on Constructors and Destructors in C++ for Class 12 Computer Science. It covers the essential characteristics of constructors, their role in object initialization, and the concept of constructor overloading with default and parameterized constructors. The solutions also explain the functionality and use cases of copy constructors with illustrative examples. Furthermore, the chapter introduces destructors, explaining their purpose and invocation time. These NCERT Solutions provide clear, step-by-step explanations to help students understand object lifecycle management in C++.

Learning outcomes

  • Understand the characteristics of constructor functions in C++.
  • Explain the concept and purpose of copy constructors.
  • Differentiate between default and parameterized constructors.
  • Identify and explain the role of destructor functions.
  • Illustrate the use of constructors and destructors with C++ code examples.

Topics covered

Paper topics

  • Constructors
  • Destructors
  • Constructor Characteristics
  • Default Constructor
  • Parameterized Constructor
  • Constructor Overloading
  • Copy Constructor
  • Object Initialization
  • Object Lifecycle Management
  • C++ OOP Concepts

Important topics

  • Constructor Characteristics
  • Copy Constructor
  • Default vs. Parameterized Constructors
  • Destructor Functionality
  • Constructor Overloading

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

Write four characteristics of constructor function used in a class. [Delhi, 2014]
Solution: The key characteristics of a constructor function in C++ are:
  1. No Return Type: A constructor function does not have any return type, not even void.
  2. Same Name as Class: The name of the constructor function must be identical to the name of the class it belongs to.
  3. Automatic Invocation: A constructor is automatically called by the system when an object of the class is created.
  4. Default Constructor Invocation: If a class does not explicitly declare any constructor, the compiler automatically provides a default constructor, which is then invoked when objects are created.

Question 2

What is a copy constructor? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it. [Delhi, 2015]
Solution: A copy constructor is a special type of constructor in C++ that is used to create a new object and initialize it with the values of an existing object of the same class. It takes a reference to an object of the same class as its parameter.

Here is an example illustrating the definition and usage of a copy constructor:

class point {

int x;

public:

point () { x = 0; } // Default constructor

point (Point &p) { x = p.x; } // Copy constructor }; void main () {

point p1; // Default constructor is called, p1.x becomes 0

point p2 (p1); // Copy constructor is called, p2.x is initialized with p1.x (which is 0)

// Alternatively, the following also calls the copy constructor:

// point p3 = p1; } In this example, `point(Point &p)` is the copy constructor. When `point p2(p1);` or `point p3 = p1;` is executed, the copy constructor is invoked to create `p2` (or `p3`) as a copy of `p1`.

Question 3

Answer the questions (i) and (ii) after going through the following class: [Delhi, 2015]

class Exam { int Rollno; char Cname [25]; float Marks; public: Exam() //Function 1 { Rollno = 0; strcpy (Cname, " "); Marks = 0.0; } Exam (int Rno, char candname[ ] ) //Function 2 { Rollno = Rno; strcpy(Cname, candname); } ~Exam ( ) //Function 3 { cout << "Result will be intimated shortly" << endl; } void Display ( ) //Function 4 { cout << "Roll no :" << Rollno; cout << "Name :" << Cname; cout << "Marks :" << Marks; } };

(i) Which OOP concept does Function 1 and Function 2 implement? Explain.

Function 1 and Function 2 implement the OOP concept of Constructor Overloading, which is a form of Polymorphism. This is because multiple constructor functions are defined within the same class scope, each having a different parameter list. Function 1 is a Default Constructor as it takes no arguments. Function 2 is a Parameterized Constructor as it accepts arguments (`int Rno` and `char candname[]`) to initialize the object's data members.

(ii) What is Function 3 called? When will it be invoked?

Function 3, `~Exam()`, is called a Destructor. It is automatically invoked when an object of the `Exam` class goes out of scope (e.g., at the end of the block or function where it was declared) or when the memory allocated for the object is explicitly deallocated using `delete`.

Common mistakes

  • Confusing constructor overloading with other forms of polymorphism.
  • Incorrectly implementing or calling copy constructors.
  • Not understanding the automatic invocation of default constructors.
  • Misinterpreting the timing of destructor invocation.

Revision tips

  • Memorize the key characteristics of constructors.
  • Practice writing and calling copy constructors with different scenarios.
  • Understand the automatic nature of default constructors and destructors.
  • Review the provided code examples for both constructors and destructors thoroughly.

Practice MCQs

Q1. Which of the following is NOT a characteristic of a constructor function?

Q2. What is the primary purpose of a copy constructor?

Q3. In C++, when is a destructor function invoked?

Q4. Which type of constructor is automatically provided by the compiler if no constructor is explicitly defined in the class?

Frequently asked questions

What are the main characteristics of a constructor in C++?

A constructor in C++ has no return type, shares the same name as the class, is invoked automatically when an object is created, and a default constructor is used if none is explicitly defined.

What is the difference between a default constructor and a parameterized constructor?

A default constructor takes no arguments and is called when an object is created without any values. A parameterized constructor accepts arguments, allowing objects to be initialized with specific values upon creation.

When is a copy constructor called?

A copy constructor is called when a new object is created and initialized with the values of an existing object of the same class, or when an object is passed by value to a function.

What is the purpose of a destructor?

A destructor is a special member function used to deallocate memory or release resources that were acquired by an object during its lifetime. It is automatically called when an object goes out of scope.

Can a constructor have a return type?

No, a constructor function in C++ does not have any return type, not even 'void'.

How do these solutions help in exam preparation?

These solutions provide clear, step-by-step explanations and illustrative examples for constructors and destructors, helping students understand complex OOP concepts and practice problem-solving for their exams.

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

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