constructor overloading in c++

Post on 17-Feb-2017

445 Views

Category:

Education

11 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Yogi’s Guide to

C++Yogi’s Guide

toC++

ConstructorOverloading

Yogendra Pal

www.learnbywatch.com | yogendra@learnbywatch.com

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.

www.learnbywatch.com | yogendra@learnbywatch.com

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.

www.learnbywatch.com | yogendra@learnbywatch.com

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.

www.learnbywatch.com | yogendra@learnbywatch.com

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.

Yogi’s Guide to

C++Yogi’s Guide

toC++

Ask Questionsto learn better

Yogendra Pal

www.learnbywatch.com | yogendra@learnbywatch.com

top related