Constructor and destructor in c++

6
Yogi’s Guide to C++ Yogi’s Guide to C++ Constructors & Destructors Yogendra Pal

Transcript of Constructor and destructor in c++

Page 1: Constructor and destructor in c++

Yogi’s Guide to

C++Yogi’s Guide

toC++

Constructors &Destructors

Yogendra Pal

Page 2: Constructor and destructor in c++

www.learnbywatch.com | [email protected]

At the end of this tutorial you will be able to • Explain the need of constructor and destructor.

• Explain when constructors and destructors are called.

• Identify the constructor and destructor from a given c++ program.

• Write a constructor and destructor of a given class.

Page 3: Constructor and destructor in c++

Yogi’s Guide to

C++Yogi’s Guide

toC++ Problem with Time class

www.learnbywatch.com | [email protected]

Page 4: Constructor and destructor in c++

www.learnbywatch.com | [email protected]

What is a constructor?• A constructor is a special function of a class.

• It has the same name of the class.

• Constructor function can not return a value.

• Automatically called when an object is created.

Page 5: Constructor and destructor in c++

www.learnbywatch.com | [email protected]

What is a destructor?• A destructor is a special function of a class.

• It has the same name of the class with ~ sign.

• Destructor function can not return a value.

• Automatically called when an object is destroyed.

Page 6: Constructor and destructor in c++

Yogi’s Guide to

C++Yogi’s Guide

toC++

Ask your questionsto learn better

Yogendra Pal

www.learnbywatch.com | [email protected]