Constructor overloading in C++

6
Yogi’s Guide to C++ Yogi’s Guide to C++ Constructor Overloading Yogendra Pal

Transcript of Constructor overloading in C++

Page 1: Constructor overloading in C++

Yogi’s Guide to

C++Yogi’s Guide

toC++

ConstructorOverloading

Yogendra Pal

Page 2: Constructor overloading in C++

www.learnbywatch.com | [email protected]

At the end of this tutorial you will be able to • Overload a constructor.

• Differentiate between initialized and uninitialized object.

• Identify overloaded functions from a given c++ program.

• Identify which function will execute on a call to overloaded function.

• Call a constructor explicitly and implicitly.

Page 3: Constructor overloading in C++

www.learnbywatch.com | [email protected]

Constructor Overloading• Creating more than one constructors for a c++ class.

• Signature of each constructor should be different.

• Allow to create both initialized and uninitialized objects.

Page 4: Constructor overloading in C++

www.learnbywatch.com | [email protected]

Create an initialized object

Uninitialized Object• Time t;

• Time t = Time();

• Member variables are not initialized.

Initialized Object• Time t(04,02,1985);

• Time t = Time(04, 02, 1985);

• Member variables are initialized.

Page 5: Constructor overloading in C++

www.learnbywatch.com | [email protected]

How to use Constructors?• Two ways to initialize an object by using a constructor:

• An Explicit call to constructor: Time t1 = Time(12, 24, 10);

• An Implicit call to constructor: Time t1(12, 24, 10);

• Constructor is called whenever you create an object of a class.

• The constructor creates object.

• There is no object until the constructor finishes it’s work.

• You can’t call a constructor with an object.

Page 6: Constructor overloading in C++

Yogi’s Guide to

C++Yogi’s Guide

toC++

Ask Questionsto learn better

Yogendra Pal

www.learnbywatch.com | [email protected]