1 / 30

Constructor & Destructor

Constructor & Destructor. By: Vaishali V Kaneria Asst. Prof., Dept of MCA, AITS, Rajkot. We are going to learn:. What is a CONSTRUCTOR? Characteristics of Constructor Constructor: In Public & In Private Default Constructor Parameterized Constructor Constructor Overloading

eadoin
Download Presentation

Constructor & Destructor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructor & Destructor By: Vaishali V Kaneria Asst. Prof., Dept of MCA, AITS, Rajkot.

  2. We are going to learn: • What is a CONSTRUCTOR? • Characteristics of Constructor • Constructor: In Public & In Private • Default Constructor • Parameterized Constructor • Constructor Overloading • Constructor with Default Arguments • Copy Constructor • Member Initialization List (MIL) • Order of Constructor Invocation • Implementing Constructors • Destructor Function

  3. What is a CONSTRUCTOR? • “Constructor is a ‘special’ member function used for initialization of objects (data members).” • “A constructor is a special member function that is a member of a class and has same name as that class.” • It is used to initialize the object of the class type with a legal initial value. • A constructor is called whenever an object of its associated class is created.

  4. A Constructor is declared and defined as follows: class number { int a, b ; public: number( void); // ** Constructor Function Declared ----- ----- }; number :: number( ) // ** Constructor Function Defined { a=0; b=0; cout<<“Hello Students! See Object is created”; }

  5. Declaration and Definition • It can be defined either inside the class definition or outside the class definition class X { int i ; public: int j,k ; X() { i = j = k =0;} }; X::X() { I = j = k =0; }

  6. Characteristics of Constructors • Constructor Functions have same name as that of class name. • They do not have return types, not even void • May have parameters • C++ has default constructors that are called whenever a class is declared even if no function with the same name exists • They should be declared in public section. • They are invoked automatically when objects are created. • Constructors can not be virtual. • They can not be inherited.

  7. Constructor: In Public & In Private • Generally a constructor should be defined under the public section of a class, so that its object can be created in any function

  8. class X { int i ; X() { i = j = k =0;} public: int j,k ; void check(void); // member function }; void X :: check (void) { X obj1; //valid } int main() { X obj2; //invalid }

  9. Default Constructor A constructor that accept no parameter is called the default constructor. If no such constructor is defined, then compiler supplies a default constructor. For Example X obj1 ; The default constructor for class X is X::X( ) The statement X obj1 ; invokes the default constructor of the compiler to create the object x.

  10. C++ Parameterized Constructor class number { int a, b ; public: number( int x, int y); // Constructor Func. Declared ----- }; number :: number(int x, int y) // Constructor Func Defined { a=x; b=y; } • number ob1; // ***** WRONG OBJECT CREATION • number ob1( 0,100 ); // ********** * Implicit Call • Known as shorthand method • Easy to implement and looks better • number ob1 = number( 0, 100 ); // ** Explicit Call

  11. C++ Parameterized Constructor

  12. C++ : Constructor Overloading class number { int a, b ; public: number( ){a=0; b=0; }// Constructor 1 number( int x, int y); // Constructor 2 ----- }; number :: number(int x, int y) // Constructor 2 Defined { a=x; b=y; }

  13. Constructor with Default Arguments class number { int a, b ; public: number( int x, int y=5 ); // Constructor ----- }; number :: number(int x, int y) // Constructor Defined { a=x; b=y; } number ob1(10); number ob2(0 , 0 );

  14. class A { inti,j; public: X(int x=10,int y=20); }; A::X(intx,int y) { i=x; j = y; } int main() { A obj1; A obj2(250); A obj3(2,4); getch(); return 0; } obj1 obj2 obj3 i=250 j=20 i=10 j=20 i=2 j=4

  15. Explicit Constructor • Automatic conversion adds to readability. • But there may be times when you do not want this automatic conversion to take place. For this purpose, C++ defines the keyword explicit.

  16. Dynamic Initialization • In earlier cases, the object is initialized with constants. The values of constants are available at compiler time. i.e., the object initialization is done at compile time. • We can use constructors to initialize objects at run time. The values to be passed to the constructors are made available at runtime and then the constructor is invoked while defining the object. We call this dynamic initialization.

  17. Dynamic Initialization

  18. Copy Constructor • A constructor that accepts a reference of its own class is called a copy constructor. • It is used to declare and initialize an object from another objects of same class. • The process of initialization with the help of a copy constructor is known as copy initialization.

  19. Copy Constructor 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 <<“\n”; } Above constructors may be used as follows sample s1 (10,12); //1st const. called sample s2 (s1) // copy const. called sample s3 = s1; copy const. called

  20. Copy Constructor

  21. Member Initialization List (MIL) • MIL is the method for initialization of the members of a class using the constructor function. • It provide the alternative for providing initializations outside the constructor body. • Its Need : • Readability • Efficiency

  22. Example of Constructor with Arguments class sum { private: int sum1,sum2; public: sum(int,int); print() {cout<<sum1<<“ “<<sum2<<endl;} }; void main () { sum obj1 (10,20); //constructor is called at this time obj1.print(); return; }

  23. Example of Constructor with Arguments sum::sum (int x, int y) { sum1=9; sum2= 10; } What is the output??

  24. the answer 9 10 (not 10 20)

  25. Order of Constructor Invocation • The objects are constructed in the order they are defined. Consider the following statement • Sample s1,s2,s3; // the order of construction is s1,s2,s3. • 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. • For example

  26. class C { private: A objA; B objB; public: C() { cout<<“Constructing C” <<endl; }}; int main() { clrscr(); C objC; getch(); } class A { public: A() { cout<<“Constructing A” <<endl; }}; class B { public: B() { cout<<“Constructing B” <<endl; }};

  27. C++ : DESTRUCTOR FUNCTION • A class can have another special member function called destructor, which is invoked when an object is destroyed. • This function is reverse of the constructors. • A special function which also has same name as that of class but is preceded with a tilde (~) sign eg., ~ number( ); • Does not have a return type (not even void) • Cannot have parameters • Called automatically when the class object is destroyed • De-allocates storage allocated to a class • Should be public (or protected) • A class cannot have more than one destructor.

  28. C++ DESTRUCTOR FUNCTION public: ~sum (); // DESTRUCTOR FUNCTION sum::~sum ( ) { //close (infile); //close (outfile); Cout<<bye; }

  29. Who is Bjarne Stroustrup??

  30. Thank You

More Related