1 / 12

Chapter 3

Chapter 3. Constructors & Destructors. Constructors. A class  constructor  is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void.

Télécharger la présentation

Chapter 3

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. Chapter 3 Constructors & Destructors

  2. Constructors • A class constructor is a special member function of a class that is executed whenever we create new objects of that class. • A constructor will have exact same name as the class and it does not have any return type at all, not even void. • Constructors can be very useful for setting initial values for certain member variables.

  3. Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator. class A { int i; public: A(); //Constructor declared }; A::A() // Constructor definition { i=1; }

  4. Types of Constructors Constructors are of three types : • Default Constructor • Parameterized Constructor • Copy Constructor

  5. Default constructor • Default constructor is the constructor which doesn't take any argument. • If the programmer does not specify the constructor in the program then compiler provides the default constructor automatically. • Default constructor get called automatically when objects of class get created. • We don’t need to call default constructor explicitly. Like using object name and (.) operator

  6. Parameterized Constructor • These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument. • This constructor is can’t be called using object name and (.) operator. • We have to call this constructor when object is created & by specifying appropriate parameters

  7. Copy constructor The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to: Initialize one object from another of the same type. copy an object to pass it as an argument to a function.

  8. Multiple constructor in class/constructor overloading C++ permits us to use all these constructor in the same class. Example class Integer { int m,n; public: integer(); // default constructor integer(int a,int b); //parameterized constructor integer(integer); // copy constructor }

  9. When more than one constructor function is defined in the class it is known as Constructor overloading

  10. Constructor with default argument It is possible to define constructor with default argument. Eg. complex(int a,int b=0); When we call that constructor it is not necessary to pass two argument to the constructor. It will automatically consider default argument. If you have specified the default argument and you are passing required parameter to constructor( two value) then it will overwrite that default argument.

  11. Destructor A destructor is used to destroy the objects that have been created by a constructor. Like a constructor the destructor is a member function whose name is same as the class name but is preceded by a tilde sing (~). Eg. ~student(); Destructor never takes any argument nor does it return any value It will be involved implicitly by the compiler when control get exit from the program. It clans up storage that is no longer accessible.

More Related