constructor and destructor

16

Click here to load reader

Upload: selvin-josy-bai-somu

Post on 16-Apr-2017

1.314 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Constructor and destructor

CONSTRUCTOR AND

DESTRUCTOR

Page 2: Constructor and destructor

1. Point out the errors, if any, in the following program.class date{

private:int day,month,year;

date(){

day = 2;month = 10;year = 2008;

}};

void main(){

date today;}

Page 3: Constructor and destructor

Error : The default constructor is defined under private section; so it is not

accessible in function main( ).

Page 4: Constructor and destructor

2. Point out the errors, if any, in the following program.class value{

private:int i;float f;

public:value(){

i = 0;f = 0.0;return 1;

}};void main(){

value vi;}

Page 5: Constructor and destructor

Error : The constructor should not return

any value. Here constructor contains return statement.

Page 6: Constructor and destructor

3. Read the following code segment.#include <iostream.h>class sample{

int x, y;public: sample(int a = 10, int b = 20) {

x = a;y = b;

} void show() {

cout << “\n x = “ << x << “y = “ << y; }

};void main(){

sample s1;sample s2(4);sample s3 = sample(6,8);s1.show();s2.show();s3.show();

}Explain the working of constructor for the creation of each object.What will be the output of the above code.

Page 7: Constructor and destructor

b) x = 10, y=20 x = 4, y=20 x = 6, y=8

Page 8: Constructor and destructor

4. A class named employee is having the following constructors.

i) employee();ii) employee(int, float);iii) employee(employee &f);

A) Name the category to which each constructor belongs.B) Write down the purpose of each.C) Define the destructor for this class.

Page 9: Constructor and destructor

1. i) default constructor, ii) parameterized constructor, iii) copy constructor2. short note about default constructor, parameterized constructor and copy constructor3. ~employee( ) { }

Page 10: Constructor and destructor

5. The following code defines a class for finding out the area of a circle.#include<iostream.h>class circle{

float rad;public:

circle(float x){

rad = x;}void area(){

cout << 3.14 * rad * rad;}

};void main(){

circle(5.5).area();}What is the purpose of the statement in the main function?What is the output?

Page 11: Constructor and destructor

a)Through this statement, an anonymous object (temporary instance) is created and initialized with the value 5.5 and that object calls the member function area( )b) 94.985

Page 12: Constructor and destructor

6. While declaration, initialization is possible with variables, arrays and structures. What about of classes? Justify your answer.

• But an object of a class cannot be initialized in similar fashion because the access labels restrict the accessibility of the data members.

Page 13: Constructor and destructor

7. A constructor is usually defined in public section. Why?

• Generally a constructor is defined under the public section of a class so that its objects can be created in any function.

Page 14: Constructor and destructor

8. Is constructor definition compulsory? Give justification to support your answer.

• Yes, constructor definition is compulsory. A Constructor is defined like other member functions of a class. It can be defined either inside or outside the class specification.

Page 15: Constructor and destructor

9. List down the characteristics of constructors. • They should be declared in the public section.• They are invoked automatically when the objects are

created.• They do not have return types, not even void and

therefore, and they cannot return values.• They cannot be inherited.• Like other C++ functions, they can have default

arguments.• They cannot be virtual.• We cannot refer to their addresses.• They cannot be static.

Page 16: Constructor and destructor

10. List down the characteristics of destructors. • They are invoked automatically when the objects are

destroyed.• They obey the usual access rules that other member

functions do.• They cannot be inherited.• No argument can be provided to a destructor, neither

does it return any value.• They cannot be virtual.• We cannot refer to their addresses.• They cannot be static.• If there is no destructor in a class, a default destructor is

generated by the compiler.