Topics |
---|
1. A C++ class prototype is given below:
(a) Write the specialties of the functions marked as Function 1, Function 2 and Function 3.
(b) Write definitions of any two of these functions.
(a) Function 1 is a constructor without any parameter and hence may be considered as a user-defined default constructor.
Function 2 is a parameterized constructor.
Function 3 is copy constructor.
(b) Function 2 may be defined as follows:
Test : : Test (int x)
{ a = x; }
Function 3 may be defined as follows:
Test : : Test (Test &ob)
{ a = ob . a ; }
2.Read the following class definition:
Suppose ob is an object of class ABC declared in the main () function. What happens when the statement ob.dispval() is executed? What should be included in the class definition to get the output as follows?
a = 5 and b = 2.5
When the statement ob.dispval(); is executed, two garbage values will be displayed as the values of a and b.
To get the given output, the class should contain a constructor. Therefore the following constructor definition should be included within the class under the public access label.
ABC ()
{ a = 5 ;
b = 2.5 ;
}
3.Observe the following class definition :
(a) Name the type of construction defined in the above class.
(b) There is an error in the definition of Constructor 2. What is that?
(c) Give the remedy for the above error and make necessary modifications.
(a) Constructor 1 is a parameterized constructor and constructor 2 is the copy constructor.
(b) In constructor 2, the argument should be reference object.
(c) The constructor header should be as follows:
Number (Number &ob)
4.The C++ statement int a = 0; is known as variable initialization. It is possible with the objects of a class also. Explain this concept with the help of examples.
Initializing an object is possible, but it requires constructor. Constructor is a member function of class which will be executed automatically when an object of the class is created. For example, consider the following class:
The function with the same name of the class (here Sample) is a constructor. The following statement assigns the value 0 to an object on its creation.
Sample ob (0) ;
If such a constructor is not defined by the user, complier will supply a constructor by which the object gets garbage value.
5.The prototype of a member function of a class 'Sample' is given below;
Sample (Sample &);
What is the specialty of this function?
This function is copy constructor, by which an object can be initialized with another object. It mans that, the data members of one object will be copied into those of another object during its creation.
6.Suppose Sample is a class with two data members d1 and d2 and the following are the object creation statements:
Sample ob1 (3, 2.5) ;
Sample ob2 (ob1) ;
To create objects as given above, the class requires two special member functions. Write complete definitions for those functions. What are those functions called?
To make the above object creation statements valid, the class should have following special member functions.
For the first statement:
For the second statement:
The above functions are called parameterized constructor and copy constructor respectively.
7.While declaration, initialization is possible with variables, arrays and structures. What about objects of class? Justify your answer.
Initialization is possible with objects, only if the class has constructor functions. Class is a data type, in which the data members can only be processed by member functions. By initialization of an object, it means that the data members of that object are assigned with user specified values. Assigning values is an operation and hence it can be performed only by member function. Here comes the role of constructor. It is special member function which is executed automatically when objects are created.
8.A constructor is usually defined in public section. Why?
The constructor should be invoked when an object is created. A member function can be called (or invoked) only if it is declared under public access label. Constructor is a member function and it should be invoked because of an object declared outside the class. Hence constructor is defined in public section.
9. Is constructor definition compulsory? Give justification to support your answer.
Constructor definition is not compulsory. In the absence of user-defined constructor, compiler will supply a constructor by which objects are initialized with garbage values. Such constructor is called default constructor.
10. Read the following code segment,
(a) Explain the working of constructor for the creation of each object.
(b) What will be the output of the above code?
(a) When the object p1 is created, no values are supplied and hence the parameterized constructor will be invoked with default values of the arguments. During the creation of object p2, the given value (that is ; 4) is passed to the argument a and b will use its default value. While creating the object p3, the values 3 and 1 will be passed to the arguments a and b of the constructor.
(b) The output will be as follows:
X = 10 Y = 5
X = 4 Y = 5
X = 3 Y = 1
11.Consider the class defined in question 10. Suppose the constructor was defined as follows:
(a) The object creation statements pair ob; fails to compile. Why?
(b) Write a sample object creation statement that will be compiled successfully.
(a) The parameters of the constructor do not have default values. Hence, while creating an object, values are to be passed to the constructor. That is why compilation error occurs.
(b) pair ob (5, 2);
12.Constructor is a member function, but it differs from other member functions in many aspects. List down these differences.
Constructor differs from other member functions in the following respects:
Constructor |
Ordinary Member |
Name is the same as that of the class. |
Any valid identifier is used as name. |
Executed automatically, when object of the class is executed. |
Executed only when it is called using an object. |
Defined under public access label. |
Can be defined under any access label. |
No return type is specified. |
Any data type is used as return type. |
Implicit and explicit calls are possible. |
Only explicit call is possible. |
A constructor may not be static. |
They may be static if they access only static members. |
13.The following class definition contains the prototypes of constructors.
(a) Name the category to which each of these constructors belongs.
(b) Write down the purpose of each of these constructors.
(c) How many destructors are needed for this class? Define them.
(a) First constructor belongs to user-defined default constructor.
Second constructor belongs to parameterized constructor.
Third constructor belongs to copy constructor.
(b) The first constructor initializes objects with the values assigned within the body of the constructor. All such objects will have the same values for their data members. Parameterized constructor can initialize objects with different values suppled by user during the creation of objects. Copy constructor copies the values of data members of one object into another object.
(c) A class requires only one destructor. The destructor for this class will be as follows:
~ sample () ;
14.What is a temporary instance of a class? How is it created?
An object without a name that exists in the memory as long as the constructor is executed is known as temporary instance. The explicit call to the constructor allows creating temporary instance (object), which lives in the memory as long as it is being used and referenced in an expression and after its use it dies.
15.The following code defines a class for storing the length and breadth of a rectangle:
(a) What is the purpose of the statement in the main () function?
(b) What is the output of the program?
(a) The statement in the main function creates a temporary object of class rectangle with 5 and 3 as the values of its data members. This object then calls the function showarea().
(b) The showarea() function is executed for the temporary instance with values for the temporary instance with values 5 and 3 for its data members len and bre respectively. Hence the output will be 15.
16.Explain different methods of calling constructors.
Constructors can be called either implicitly or explicitly. Constructor is called implicitly when objects are created, with arguments if any, using the class name. On the other hand, in explicit call, the constructor function is mentioned explicitly in an assignment statement as in:
Classname objectname
= classname (arguments) ;
17.Explain the situations when copy constructors are executed.
(i) When an object is passed by value to a function (not to the constructor function)
(ii) When a function returns an object.
18. Why copy constructor is always called by reference method?
If the copy constructor is called by value method, on passing the arguments its copy is constructed and for copying the values, the copy constructor is again invoked. That is, it calls itself again and again and on each call for passing the values of the actual arguments the formal objects are created. AT last, the compiler will complain out of memory. Hence copy constructor is always called by reference method.
19.Distinguish between constructors and destructors.
Constructors |
Destructor |
Invoked when objects are created. |
Invoked when objects are destroyed. |
Arguments can be passed |
Arguments cannot be passed. |
More than one constructor can be defined for the same class. |
Only one destructor is declared for a class. |
Constructors can be overloaded. |
Destructors cannot be overloaded. |
Constructors cannot be virtual. |
Destructors can be virtual. |
20.Explain the different types of constructors.
There are three types of constructors; default constructors, parameterized constructors and copy constructors.
21.State true or false and give reasons.
(a) Member functions can be called from within a constructor.
(b) All objects of a class must have a name.
(a) True. Constructor is a member function of a class, and a member function can call another member function of the same class.
(b) False. There can be an object with no name, which is known as temporary instance.
22. Find the volume of rectangular box using parameterized constructor.
23.Can we define constructor with default arguments? Give an example.
Yes, Constructor can be defined with default arguments.