Your SlideShare is downloading. ×
0
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & Destructor
Constructor & 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 & Destructor

9,337

Published on

I am PGT(Comp. Sc) in KV(AFS) Utarlai …

I am PGT(Comp. Sc) in KV(AFS) Utarlai
publishing Constructor and Destructor for
Class XII

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
9,337
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
524
Comments
0
Likes
1
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 & Destructor
  • 2. Constructors <ul><li>Def- A constructor is a special member function that is a member of a class and has same name as that class. </li></ul><ul><li>Use- It is used to initialize the object of the class type with a legal initial value. </li></ul>
  • 3. Special Characteristics of Constructors <ul><li>These are called automatically when the objects are created. </li></ul><ul><li>All the objects of the class having a constructor are initialized before some use. </li></ul><ul><li>These should be declared in the public section for availability to all the functions. </li></ul><ul><li>Constructor has no return type even void. </li></ul><ul><li>They can not be inherited. </li></ul><ul><li>These cannot be static. </li></ul><ul><li>Default and copy constructor are generated by the compiler whenever required. Generated constructors are public. </li></ul><ul><li>Constructors can have default argument as other C++ functions. </li></ul>
  • 4. Declaration and Definition <ul><li>It can be defined either inside the class definition or outside the class definition </li></ul><ul><li>class X { int i ; </li></ul><ul><li>public: </li></ul><ul><li>int j,k ; </li></ul><ul><li>X() { i = j = k =0;} </li></ul><ul><li> }; </li></ul>
  • 5. <ul><li>class X { int i ; </li></ul><ul><li>public: </li></ul><ul><li>int j,k ; </li></ul><ul><li>X() { i = j = k =0;} </li></ul><ul><li> }; </li></ul><ul><li>X::X() </li></ul><ul><li>{ </li></ul><ul><li>I = j = k =0; </li></ul><ul><li>} </li></ul>
  • 6. Note <ul><li>Generally a constructor should be defined under the public section of a class, so that its object can be created in any function </li></ul>
  • 7. <ul><li>class X { int i ; </li></ul><ul><li>X() { i = j = k =0;} </li></ul><ul><li>public: </li></ul><ul><li>int j,k ; </li></ul><ul><li>void check(void); // member function </li></ul><ul><li> }; </li></ul><ul><li>void X :: check (void) </li></ul><ul><li>{ </li></ul><ul><li>X obj1; //valid </li></ul><ul><li>} </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>X obj2; //invalid </li></ul><ul><li>} </li></ul>
  • 8. Default constructor <ul><li>A constructor that accept no parameter is called the default constructor. </li></ul>
  • 9. Copy Constructor <ul><li>A copy Constructor is used to initialize an object from another object. </li></ul><ul><li>If you have not defined a copy constructor, the complier automatically creates it and it is public </li></ul><ul><li>Copy constructor always takes argument as a reference object. </li></ul><ul><li>Consider the following class definition </li></ul>
  • 10. class sample { int i, j; public: sample ( int a, int b) { i = a; j = b; } sample ( sample & s) { i = s.i ; j = s.j ; } void print(void) { cout<<i <<“ “<<j <<“ ”; } Above constructors may be used as follows sample s1 (10,12); //1 st const. called sample s2 (s1) // copy const. called sample s3 = s1; copy const. called
  • 11. Home work <ul><li>In a class does not define a constructor, what will be the initial value of its object. </li></ul><ul><li>What are the significance of default constructor. </li></ul><ul><li>How many situations when a copy constructor is automatically called. </li></ul>
  • 12. Default Arguments <ul><li>Just like any other function a constructor can also have default arguments </li></ul><ul><li>This constructor is equivalent to default constructor. Because its also allows us to create objects without any value provided. </li></ul><ul><li>For example </li></ul>
  • 13. <ul><li>class A </li></ul><ul><li>{ </li></ul><ul><li>int i,j; </li></ul><ul><li>public: </li></ul><ul><li>X(int x=10,int y=20); </li></ul><ul><li>}; </li></ul><ul><li>A::X(int x,int y) </li></ul><ul><li>{ </li></ul><ul><li>i=x; </li></ul><ul><li>j = y; </li></ul><ul><li>} </li></ul>int main() { A obj1; A obj2(250); A obj3(2,4); getch(); return 0; } i=10 j=20 i=250 j=20 i=2 j=4 obj1 obj2 obj3
  • 14. Order of Constructor Invocation <ul><li>The objects are constructed in the order they are defined. Consider the following statement </li></ul><ul><li>Sample s1,s2,s3; // the order of construction is s1,s2,s3. </li></ul><ul><li>But when a class containing objects of another class as its members. In such a case, the constructor for member objects are invoked first and then only, the constructor for containing class is invoked. </li></ul><ul><li>For example </li></ul>
  • 15. <ul><li>class A </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>A() </li></ul><ul><li>{ </li></ul><ul><li>cout<<“Constructing A” </li></ul><ul><li><<endl; </li></ul><ul><li>}}; </li></ul><ul><li>class B </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>B() </li></ul><ul><li>{ </li></ul><ul><li>cout<<“Constructing B” </li></ul><ul><li><<endl; </li></ul><ul><li>}}; </li></ul>class C { private: A objA; B objB; public: C() { cout<<“Constructing C” <<endl; }}; int main() { clrscr(); C objC; getch(); }

×