Presentation is loading. Please wait.

Presentation is loading. Please wait.

CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)

Similar presentations


Presentation on theme: "CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)"— Presentation transcript:

1 CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)

2 INTRODUCTION The main aim of c++ is to create user defined Data Types such as class. This means that we should be able to initialize a Class type variable (Object) when it is declared and When a variable of built in types goes out of scope,the compiler automatically destroys the variable. C++ provides the special member functions for the above purpose called Constructor and Destructor respectively.

3 Constructors It is a Special Member function with the same name as its class name. It is used to initialize the objects of that class type with some legal values. It is called itself when the object of that class type is declared. It can be declared within the class as well as outside the class with scope resolution operator.

4 Example of Constructor A constructor is defined and declared as follows: c lass Student c lass Student { int rollno,marks; int rollno,marks; public: public:.. Student()//Constructor { Rollno=0; Rollno=0; marks=0; marks=0; }.. }; };

5 NEED FOR CONSTRUCTORS A Constructor for a class is needed so that the compiler automatically initializes an object as soon as it is created. A class constructor,if defined is called whenever a program creates an object of that class. Generally a constructor should be defined under the public sections of the class,so that its objects can be created in any Function Generally a constructor should be defined under the public sections of the class,so that its objects can be created in any Function

6 TYPES OF CONSTRUCTORS 1 Default Constructor: A Constructor that accepts no arguments or default arguments is called the Default Constructor. In the previous example Student :: Student() is a default constructor.

7 Example of Default Constructor A default constructor is defined and declared as follows: c lass Student c lass Student { int rollno,marks; int rollno,marks; public: public: Student( ) // Default Constructor Student( ) // Default Constructor { Rollno=0; Rollno=0; marks=0; marks=0; } Student( int a=5, int b= 50 )//Default Constructor { Rollno=a; Rollno=a; marks=b; marks=b; }.. }; };

8 Significance of Default constructor- These constructors are useful when- We want to create objects without giving initial values. We want to create objects with prescribed initial values. To create the array of objects of the class

9 For creating the array of objects- Erroneous c lass student { int rollno,marks; int rollno,marks; public: public: student( int a, int b )// Constructor { Rollno = a ; Rollno = a ; marks = b ; marks = b ; }.. }; }; Int main() { student s[5]; //will give error as no default const. present. student s[5]; //will give error as no default const. present. student s1(2, 45);// ok as corresponding const. is there student s1(2, 45);// ok as corresponding const. is there}

10 For creating the array of objects- Corrected c lass Student { int rollno,marks; int rollno,marks; public: public: Student( ) // Default Constructor Student( ) // Default Constructor { Rollno = 0; Rollno = 0; marks = 0; marks = 0; } Student( int a, int b )// Constructor { Rollno = a ; Rollno = a ; marks = b ; marks = b ; }.. }; }; Int main() { Student s[5]; // OK as default const. present. Student s[5]; // OK as default const. present. Student s1(2, 45);// Ok as corresponding const. is present }

11 Invocation of Constructors- Invocation of Constructors- In parameterized constructors we must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways: 1) By calling the function implicitly e.g: ABC ob1(10,10.5,’a’); 2) By calling the function explicitly ABC ob1=ABC(10,10.5,’a’); //Here ABC is constructor which is called with parameters to create an object ob1.

12 Temporary Instance When constructor is called explicitly a temporary instance is created. When constructor is called explicitly a temporary instance is created. It is one which lives in the memory as long as it is being used or referenced in an expression and after that it dies. These instances are anonymous i.e. they do not bear name. For example- Class sample { int I,j; public: sample (int a, int b) { i=a; j=b;} void print ( void) {cout<<i<<j<<“\n”;}}; void test (void) { sample s1(2,5); s1.print(); sample(4,9).print();//Temporary instance }

13 2 Parameterized Constructors: The constructors that can take arguments are called Parameterized Constructors.

14 Parameterized Constructors Class ABC { int i; int i; Public: Public: float j; float j; char k; char k; ABC( int a, float b, char c) //Parameterized Constructor { i=a; i=a; j=b; j=b; k=c; k=c; }.. };

15 3. Copy Constructor- This constructor is in the form of Classname(classname& ) The compiler uses this constructor when initialization of one instance takes place with the help of other instance Sample s1; //Default Constructor Sample s2=s1;//Copy Constructor

16 Copy constructor- A copy const. takes a reference to an object of same class as argument. For example- Class sample { int i,j; public: sample (int a, int b)//parameterized const. { i=a; j=b;} sample (sample & s)//copy const. { i=s.i; j=s.j;} void print ( void) {cout<<i<<j<<“\n”;}}; The const can be used as Sample s1 (4, 5);//paramet. Const is used Sample s2 (s1);// copy const is used Sample s3=s1;// copy const is used

17 Copy constructor is called when- 1.Object is passed by value 2.A function returns an object Qn- Why the arguments to a copy const. is passed by reference?

18 Order of constructor Invocation- Sample s1, s2, s3; The sequence of const. invocation will be S1, s2, s3. But in nested class the const. of nested class is called first followed by enclosing class. For example Class student { }; Class subject { }; Class admission { student s1; subject sb1; }; Void main() { admission ad; } The sequence of const. invocation will be ???? ????

19 DYNAMIC CONSTRUCTORS The constructors can also be used to allocate memory while creating objects.This will enable the system to allocate right amount of memory for each object when the objects are not of same memory. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. The memory is allocated with the new operator.

20 Constructor Overloading A class having no. of constructors with different no. or type of arguments is called to be overloaded. This process is called function overloading.

21 Characteristics of Constructors They are invoked automatically when the objects are created. They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Like other c++ functions, they can have default arguments. They should be declared in the public section.

22 Destructors A destructor is a member function which 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 the same as the class name but preceded by a tilde. For example,the destructor for the class can be defined as shown below: For example,the destructor for the class can be defined as shown below: ~student(){ } ~student(){ }

23 Example of Destructor A destructor is defined and declared as follows: Class sample Class sample { int i,j; int i,j; Public: Public:.. Sample(int a,int b)//Constructor { i=a; i=a; j=b; j=b; } ~Sample() //Destructor { cout<<“Destructor at work”; cout<<“Destructor at work”; }. }; };

24 NEED FOR DESTRUCTORS During the construction of an object by the constructor, resources are allocated for use e.g.,allocation of memory, opening of file etc. these allocated resources must be deallocated before the object is destroyed. A destructor is responsible for this task and performs all clean up jobs like closing a file, deallocating and releasing memory area automatically.

25 Characteristics of Destructors Destructor functions are invoked automatically when the objects are destroyed. Destructor functions also, obey the usual access rules as other member functions do. No argument can be provided to a destructor, neither does it return any value. They can’t be inherited. A destructor may not be static. Member functions may be called from within a destructor.

26 Problem 1. Explain Constructors and Destructors with Example. 1. Explain Constructors and Destructors with Example. 2. Explain the advantages of function overloading over default arguments.


Download ppt "CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)"

Similar presentations


Ads by Google