Your SlideShare is downloading. ×
0
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Constructor and destructor
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Constructor and destructor

820

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
820
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. CONSTRUCTOR AND DESTRUCTOR
  • 2. 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; }
  • 3. Error :The default constructor is definedunder private section; so it is not accessible in function main( ).
  • 4. 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; }
  • 5. Error :The constructor should not return any value. Here constructor contains return statement.
  • 6. 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.
  • 7. b) x = 10, y=20 x = 4, y=20 x = 6, y=8
  • 8. 4. A class named employee is having thefollowing 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.
  • 9. 1. i) default constructor, ii) parameterized constructor, iii) copy constructor2. short note about default constructor,parameterized constructor and copyconstructor3. ~employee( ) { }
  • 10. 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?
  • 11. a)Through this statement, ananonymous object (temporary instance)is created and initialized with the value5.5 and that object calls the memberfunction area( )b) 94.985
  • 12. 6. While declaration, initialization ispossible with variables, arrays andstructures. 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.
  • 13. 7. A constructor is usually definedin 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.
  • 14. 8. Is constructor definitioncompulsory? Give justification tosupport 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.
  • 15. 9. List down the characteristics ofconstructors.• 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.
  • 16. 10. List down the characteristics ofdestructors.• 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.

×