Constructors and destructors Dr. Bhargavi Dept of CS CHRIST 9426669020
introduction Can we initialize the variables of objects when they are created? Similarly, when a variable of built-in type goes out of scope, can compiler automatically destroys the variable? Yes. Using: Constructors, for automatic initialization of objects. Destructors, for destroying objects when no more required.
Constructors Definition: special function which is invoked without explicit call whose task is to initialize objects of its class. When invoked? When object is created. Why called constructors? Bcoz it constructs values for data members. Explicit call statement is not required to call a constructor. Use? Convenient when number of obj is large.
Types of constructors Default constructor Parameterized constructor Overloaded multiple constructor Constructors with default arguments Copy constructors Dynamic constructors Types of constructors Default constructor Parameterized constructor Overloaded multiple constructor Constructors with default arguments Copy constructors Dynamic constructors
Characteristics of constructors They should be declared in public section. Invoked automatically at object creation. Have no return type. Thus, cannot return values. Cannot be inherited. Though, derived class can call base class constructors. May have default arguments. Cannot be virtual. We cannot refer to address of constructors An obj with constructor cannot be used as union Makes implicit call to ‘new’ and ‘delete’ operators for dynamic memory allocation
Default constructors If no constructor is defined, compiler calls default constructor without body. Default constructors are constructors without explicit definition and without arguments. Eg. A::A() Eg. classConstructorDefaulVal.cpp Eg. classConstructor.cpp Eg. privateConstructor.cpp
Parameterized constructors Practically we cannot initialize all the objects with same values. What if I want to initialize with different values to different objects? Is it possible? Yes. How? Parameterized constructors can take arguments that can be passed with individual values for each object. Definition: Constructors that can take arguments are called parameterized constructors.
Parameterized constructors Pl note that parameterized constructors require argument passing. Constructors can also be defined as inline. Constructor can accept a reference to its own class as parameter. We also call it copy constructor. Eg.6_1_ExampleOfParameterizedConstruncto r.cpp Eg. classParameterisedConstructor.cpp
overloading constructors Can we copy the values of first object into second object using constructor? Yes. Lets see how. See the example given below. Eg. 6_2_OverloadedConstructors.cpp Eg. contructor_Overloading.cpp
Constructors with default arguments Is it possible to define constructors with default arguments? Yes. How? Eg. complex C(float real, float imag=0); complex C(5.0); complex C(2.0,3.0); Eg. 6_2_OverloadedConstructors.cpp, we have already done. Eg. classConstructorDefaulVal.cpp, we have already done. Note: Its imp to distinguish between default constructors A::A() and default argument constructor A::A(int=0). They cannot be implemented together to avoid ambiguity.
Dynamic initialization of objects Can class objects be initialized dynamically? Means? During runtime provide the initialization values to objects. Is it possible? Yes. Advantage: Multiple formats are available for initialization using overloaded constructors. Provides flexibility of using different formats at runtime. Eg. 6_3_DynamicInitializationOfObject.cpp
Copy constructor We already did this during constructor overloading. Use? Initialize objects with values of another existing object. Eg. Integer i2 = i1; Integer i2(i1); Following statement is not copy constructor but, its legal and assignment operator overloading which we will see next chapter. i2 = i1; Copy constructor takes a reference to an obj of same class as itself as an argument. We cannot pass argument by value to copy constructor. When no copy constructor is defined, compiler supplies its own copy constructor. Eg. 6_4_CopyConstructor.cpp Eg. copyConstructorDestructor.cpp
Dynamic constructors Constructor can also allocate memory while creating objects. Use? Assign right amount of memory as per the requirement. Thus, optimum utilization of memory. How to do that? Using new operator. Eg. dynamicConstructor.cpp Eg. 6_5_ConstructorsWithNew.cpp
Constructing two dimensional array Eg. 6_6_ConstructingMatrixObjects.cpp
Const objects Can we have objects as const? Yes. Y? To avoid accidental modification of obj values. Eg. const matrix X(m,n); //const obj X We cannot modify m and n. Constant obj can call only const functions. High security implementation.
destructors Use? To destroy objects created by constructors. Preceded by tilde(~) sign. Eg. ~integer(){}; Never takes any argument. Never returns any value. Invoked implicitly by compiler during exit. Used to clean up storage not accessible. It is a part of good programming practice. New – delete, same, constructor – destructor. Objects get destroyed in reverse order of creation. Eg. 6_7_ImplmentationOfDestructors.cpp Eg.6_8_MemoryAllocationToAnObjectUsingDestrucor.c pp
END OF CHAPTER 6 THANK YOU