Sunday 14 May 2017

CBSE Class 12 - Computer Science - C++ - Constructors and Destructors (Q and A) (#cbseNotes)

C++ - Constructors and Destructors

CBSE Class 12 - Computer Science - Questions and Answers

CBSE Class 12 - Computer Science - C++ - Constructors and Destructors (Q and A) (#cbseNotes)

Q1: What is a constructor? How it is different from other member functions?

Answer: A constructor is a special member function that must be defined with the same name as the class and it is used to initialize the data members of the class. Constructor is invoked automatically when an object of a class is created. Constructors unlike other functions cannot return values, so they cannot specify a return type (not even void ).Generally, constructors are declared public.



Q2: Name the types of constructors.

Answer: Types of constructors:
Default Constructor (No argument constructor)
Parameterized Constructor (Overloaded Constructor)
Copy Constructor




Q3: What do you understand by Default constructor? What is its role?

Answer: A default constructor accepts no parameters.
Default constructors are significant in the following situations:

When no constructor is defined in the class, compiler provides the default constructor as an inline public member of its class. But it will not initialize members.

② When an array of objects is declared, the default constructor is used to initialize all the elements.


Q4: What is a parameterized constructor?

Answer: A constructor that accepts parameters for its invocation is known as parameterized Constructors, also called as Regular Constructors. There may be many definitions of the parameterized constructor depending upon the type and number of parameters passed to the constructor and so it is also called overloaded constructor.


e.g. class student {
 char name[20];
 int rollmo;

 public:
  student(char* sname, int rnum );
  int getrollno();
};  


Here constructor student(char* sname, int rnum ); is a  parameterized constructor.


Q5: What is a destructor? When it is called?

Answer: A destructor is also a member function whose name is the same as the class name but is preceded by tilde(“~”).It is automatically by the compiler when an object is destroyed.

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed.

A destructor is called for a class object when that object passes out of scope or is explicitly deleted.


Q6: Study the following c++ snippet and answer the following questions:


class TEST
{ 
 int Regno,Max,Min,Score;
 public:
  TEST( ) // function 1
  {
  }

  TEST (int Pregno,int Pscore) // function 2
  {
  Regno = Pregno ;Max=100;Max=100;Min=40;Score=Pscore;
  }

  ~ TEST ( )   // function 3
  { 
   cout<<”TEST Over”<<endl;
                }
};

(a) Name the function 1
(b) Name the function 2
(c) Name the function 3


Answer:
(a) Default Constructor
(b) Parameterized Constructor
(c) Destructor


Q7: Do constructors and destructors have any return type? Can these be declared as void function functions?

Answer: Constructors and destructors do not have return type, not even void nor can they return values.


Q8: Can we use references or pointers to constructors and destructor?

Answer: No because their addresses cannot be taken.


Q9: What is a copy constructor? Give an example.

Answer: A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.

A copy constructor is used to create new object with the similar values of existing object. A copy constructor is invoked when one object is defined and initialized with another object of the same class.

For Example:

  class Test { 
 int i, j;
 public:
  Test(int a, int b) // constructor
  { 
   i=a;
   j=b;
  }

 Test (Test & obj) //copy constructor
 { 
  j=obj.j; 
  i=obj.j;
  cout <<”\n Copy constructor working \n”;
 }
 void print (void)
 {
  cout <<i<< j<< ”\n”;
 }
};


Q10: Why must the argument in the copy constructor be passed by reference?

Answer: The argument to a copy constructor is passed by reference. If an argument is passed by value, a copy of its will be constructed. But the copy constructor is creating a copy of the object for itself, thus ,it calls itself and goes into an infinite loop. Again the called copy constructor requires another copy so again it is called. In fact it calls itself again and again until the compiler runs out of the memory.Thus in the copy constructor, the argument must be passed by reference.


Q11: What are the different scenarios that may result in a call to a copy constructor?

Answer: When an object is returned by value
When an object is passed (to a function) by value as an argument
When an object is thrown or caught in an exception.
When an object is placed in a brace-enclosed initializer list (object declaration or through function arguments)


Q12: What is implicit copy constructor?

Answer: If a class doesn't have a copy constructor, C++ creates a default copy constructor (also called implicit copy constructor) for it. The default copy constructor does the member wise assignment.

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.