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

Tut Constructor

2,764

Published on

Constructor, destructor,default constructor

Constructor, destructor,default constructor

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,764
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
135
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/Destructor Functions Presentation By : Ms. Nita Arora PGT Comp. Sc. Kulachi Hansraj Model School
  • 2. OBJECTIVES:After this presentation we shall be able to answer following questions: <ul><li>What is a CONSTRUCTOR Function? </li></ul><ul><li>Characteristics of Constructor </li></ul><ul><li>Default Constructor </li></ul><ul><li>Parameterized Constructor </li></ul><ul><li>Constructor Overloading </li></ul><ul><li>Constructor with Default Arguments </li></ul><ul><li>Copy Constructor </li></ul><ul><li>Guidelines for implementing Constructors </li></ul><ul><li>Destructor Function </li></ul>
  • 3. What is a CONSTRUCTOR Function? <ul><li>Special member function used for initialization of objects (data members). </li></ul><ul><li>A constructor function is called whenever an object is created. </li></ul><ul><li>Constructors are also called when an object is created as a part of another object. </li></ul>
  • 4. A Constructor is declared and defined as follows: <ul><li>class number </li></ul><ul><li>{ int a, b ; </li></ul><ul><li> public: </li></ul><ul><li> number( void); // ** Constructor Function Declared </li></ul><ul><li> ----- </li></ul><ul><li> ----- </li></ul><ul><li>}; </li></ul><ul><li>number :: number( ) // ** Constructor Function Defined </li></ul><ul><li>{ </li></ul><ul><li> a=0; </li></ul><ul><li> b=0; </li></ul><ul><li>} </li></ul>
  • 5. Characteristics of Constructors <ul><li>Constructor Functions have same name as that of class name. </li></ul><ul><li>They do not have return types, not even void </li></ul><ul><li>May have parameters </li></ul><ul><li>C++ has default constructors that are called whenever a class is declared even if no function with the same name exists </li></ul><ul><li>They should be declared in public section. </li></ul><ul><li>They are invoked automatically when objects are created. </li></ul>
  • 6. C++ The default constructor takes no arguments . If no such constructor is defined, then compiler supplies a default constructor. For Example X x ; The default constructor for class X is X::X( ) The statement X x ; invokes the default constructor of the compiler to create the object x. Default Constructor
  • 7. C++ Parameterized Constructor <ul><li>class number </li></ul><ul><li>{ int a, b ; </li></ul><ul><li> public: </li></ul><ul><li> number( int x, int y); // Constructor Func. Declared </li></ul><ul><li> ----- }; </li></ul><ul><li>number :: number(int x, int y) // Constructor Func Defined </li></ul><ul><li>{ a=x; b=y; } </li></ul><ul><li>number ob1; // ***** WRONG OBJECT CREATION </li></ul><ul><li>number ob1( 0,100 ); // ********** * Implicit Call </li></ul><ul><ul><li>Known as shorthand method </li></ul></ul><ul><ul><li>Easy to implement and looks better </li></ul></ul><ul><li>number ob1 = number( 0, 100 ); // ** Explicit Call </li></ul>
  • 8. C++ : Constructor Overloading <ul><li>class number </li></ul><ul><li>{ int a, b ; </li></ul><ul><li> public: </li></ul><ul><li> number( ){a=0; b=0; } // Constructor 1 </li></ul><ul><li> number( int x, int y); // Constructor 2 </li></ul><ul><li> ----- }; </li></ul><ul><li>number :: number(int x, int y) // Constructor 2 Defined </li></ul><ul><li>{ a=x; b=y; } </li></ul>
  • 9. C++ : Constructor with Default Arguments <ul><li>class number </li></ul><ul><li>{ int a, b ; </li></ul><ul><li> public: </li></ul><ul><li> number( int x, int y=5 ); // Constructor </li></ul><ul><li> ----- }; </li></ul><ul><li>number :: number(int x, int y) // Constructor Defined </li></ul><ul><li>{ a=x; b=y; } </li></ul><ul><li>number ob1(10); </li></ul><ul><li>number ob2(0 , 0 ); </li></ul>
  • 10. Example of Constructor <ul><li>class sum </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>sum(); </li></ul><ul><li>private: </li></ul><ul><li>int sum1,sum2; </li></ul><ul><li>}; </li></ul>
  • 11. C++ <ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>sum obj1; //constructor is called at this </li></ul><ul><li>//time </li></ul><ul><li>cout<<“end of main”<<endl; </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul>
  • 12. C++ Sample Functions What is the output??
  • 13. C++ Functions <ul><li>sum::sum () </li></ul><ul><li>{ </li></ul><ul><li>sum1=0; </li></ul><ul><li>sum2=10; </li></ul><ul><li>cout<<sum1<<“ “<<sum2<<endl; </li></ul><ul><li>} </li></ul>
  • 14. the answer 0 10 end of main
  • 15. Example of Constructor withArguments <ul><li>class sum </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>sum(int,int); </li></ul><ul><li>private: </li></ul><ul><li>int sum1,sum2; </li></ul><ul><li>}; </li></ul>
  • 16. C++ <ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>sum obj1 (10,20); //constructor is </li></ul><ul><li>// called at this time </li></ul><ul><li>cout<<“end of main ”; </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul>
  • 17. C++ Functions <ul><li>sum::sum (int x,int y)) </li></ul><ul><li>{ </li></ul><ul><li>sum1=x; </li></ul><ul><li>sum2=y; </li></ul><ul><li>cout<<sum1<<“ sum1”; </li></ul><ul><li>cout<<sum2<<“ sum2”<<endl; </li></ul><ul><li>} </li></ul>
  • 18. C++ Sample Functions <ul><li>sum::sum (int x,int y)) </li></ul><ul><li>{ </li></ul><ul><li>sum1=x; </li></ul><ul><li>sum2=y; </li></ul><ul><li>cout<<sum1<<“ sum1”; </li></ul><ul><li>cout<<sum2<<“ sum2”<<endl; </li></ul><ul><li>} </li></ul>10 sum1 20 sum2 end main What is the output??
  • 19. C++ : DESTRUCTOR FUNCTION <ul><li>A special function which also has same name as that of class but is preceded with a tilde (~) sign eg., ~ number( ); </li></ul><ul><li>Does not have a return type (not even void) </li></ul><ul><li>Cannot have parameters </li></ul><ul><li>Called automatically when the class object is destroyed </li></ul><ul><li>De-allocates storage allocated to a class </li></ul><ul><li>Should be public (or protected) </li></ul>
  • 20. public: ~sum (); // DESTRUCTOR FUNCTION sum::~sum ( ) { close (infile); close (outfile); } C++ DESTRUCTOR FUNCTION

×