CBSE Class 12 Computer Science Chapter 4: Constructor & Destructor NCERT Solutions
This chapter provides NCERT Solutions for Class 12 Computer Science, focusing on Constructors and Destructors in C++. It covers the fundamental concepts of constructors, including their characteristics, default constructors, parameterized constructors, and copy constructors. The solutions also explain the role and invocation of destructors. Through clear explanations and illustrative examples, students will understand how these special member functions are used to initialize objects and clean up resources automatically. These solutions are designed to help students grasp the intricacies of object-oriented programming principles related to object lifecycle management, aiding in effective exam preparation and revision.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | 4. Constructor & Destructor |
Chapter summary
Chapter 4 of the CBSE Class 12 Computer Science curriculum delves into Constructors and Destructors. This section provides detailed NCERT Solutions that explain the purpose, characteristics, and usage of constructors (default, parameterized, and copy) and destructors. It clarifies how these functions automate object initialization and resource deallocation, crucial for effective C++ programming. The solutions cover key concepts and provide examples to solidify understanding.
Learning outcomes
- Understand the concept and characteristics of constructors in C++.
- Differentiate between default, parameterized, and copy constructors.
- Explain the purpose and invocation of a destructor.
- Apply constructor and destructor concepts in C++ programming examples.
- Recognize constructor overloading as an implementation of polymorphism.
Topics covered
Paper topics
- Constructors
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Destructors
- Object Initialization
- Resource Management
- Constructor Overloading
- Polymorphism
- Class Member Functions
Important topics
- Characteristics of Constructors
- Copy Constructor Definition and Usage
- Destructor Purpose and Invocation
- Constructor Overloading
- Automatic Invocation of Constructors and Destructors
PDF preview
Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.
Questions and Solutions
Question 1
A constructor function in C++ possesses several key characteristics:
- No Return Type: A constructor does not have any return type, not even `void`.
- Same Name as Class: The name of the constructor function must be identical to the name of the class it belongs to.
- Automatic Invocation: A constructor is automatically called by the system when an object of the class is created.
- Default Invocation: If a programmer does not explicitly define any constructor within a class, the compiler implicitly provides a default constructor, which is then invoked upon object creation.
Question 2
A copy constructor is a special type of overloaded constructor within a class. Its primary role is to create a new object by initializing it with the values of an existing object of the same class. The parameter passed to a copy constructor is typically a reference to an object of the same class.
Here is a C++ example illustrating a copy constructor:
class Point {
int x;
public:
// Default constructor
Point() {
x = 0;
}
// Parameterized constructor (optional, for illustration)
Point(int val) {
x = val;
}
// Copy constructor
Point(Point &p) { // Takes a reference to an object of the same class
x = p.x; // Initializes the new object's data member with the existing object's data member
}
void display() {
// cout << "X value: " << x << endl;
}
};
int main() {
Point p1; // Default constructor is called, p1.x is initialized to 0
Point p2(p1); // Copy constructor is called: p2 is created as a copy of p1
// Alternatively, the following also calls the copy constructor:
// Point p3 = p1; // Copy constructor is called: p3 is initialized from p1
// p1.display(); // Output would show x = 0
// p2.display(); // Output would show x = 0
return 0;
}
In this example, `Point(Point &p)` is the copy constructor. When `Point p2(p1);` or `Point p3 = p1;` is executed, this copy constructor is invoked, creating `p2` and `p3` as copies of `p1`.
Question 3
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; } };
- 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 (specifically, compile-time polymorphism). This is because multiple functions (in this case, constructors) with the same name (`Exam`) are defined within the same scope, but they have different parameter lists. Function 1 is a Default Constructor as it takes no arguments, initializing the object's members to default values. Function 2 is a Parameterized Constructor as it accepts arguments (`Rno` and `candname`) to initialize the object's members with specific values provided during object creation.
- What is Function 3 called? When will it be invoked? Function 3, `~Exam()`, is called a Destructor. It is automatically invoked by the system when an object of the `Exam` class goes out of scope, or when the memory allocated for the object is deallocated (e.g., when a program terminates or when `delete` is called on a dynamically allocated object). The destructor is used to perform cleanup operations, such as releasing dynamically allocated memory.
Common mistakes
- Confusing the name of the constructor with the class name.
- Not understanding when a default constructor is invoked.
- Incorrectly implementing or calling a copy constructor.
- Misunderstanding the automatic invocation of destructors.
Revision tips
- Review the four key characteristics of constructors.
- Pay close attention to the syntax and usage of copy constructors.
- Understand the automatic invocation of both constructors and destructors.
- Practice writing simple classes with default, parameterized, and copy constructors.
- Ensure you can identify when a destructor is called in a program's execution.
Practice MCQs
Q1. Which of the following is NOT a characteristic of a constructor?
Explanation: Constructors do not have a return type, not even void. The other options are valid characteristics of constructors.
Q2. What is the primary purpose of a copy constructor?
Explanation: A copy constructor is specifically designed to create a new object as a copy of an existing object of the same class.
Q3. When is a default constructor invoked?
Explanation: The default constructor is automatically called when an object is declared without any arguments, provided no other constructor is explicitly defined by the user.
Q4. What does Function 3 in the provided class `Exam` represent?
Explanation: Function 3, named `~Exam()`, is a destructor because it has the tilde (~) prefix before the class name.
Q5. Which OOP concept is demonstrated by having multiple constructor definitions (Function 1 and Function 2) in the `Exam` class?
Explanation: Having multiple constructors with different parameter lists within the same class is known as constructor overloading, a form of compile-time polymorphism.
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 can be overloaded.
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 arguments. A parameterized constructor takes arguments and is used to initialize objects with specific values.
When is a copy constructor used?
A copy constructor is used to create a new object that is a copy of an existing object of the same class. It is invoked when an object is initialized from another object of the same type.
What is a destructor and when is it called?
A destructor is a special member function that is automatically called when an object's lifetime ends (e.g., when it goes out of scope or is explicitly deleted) to release resources it might be holding.
Can a constructor have a return type?
No, a constructor function in C++ does not have any return type, not even void.
What is constructor overloading?
Constructor overloading is the concept of having multiple constructors with different parameter lists within the same class, allowing objects to be initialized in various ways.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.