Constructor overloading in C++
-
Upload
learn-by-watch -
Category
Education
-
view
445 -
download
11
Transcript of Constructor overloading in C++
Yogi’s Guide to
C++Yogi’s Guide
toC++
ConstructorOverloading
Yogendra Pal
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.
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.
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.
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.
Yogi’s Guide to
C++Yogi’s Guide
toC++
Ask Questionsto learn better
Yogendra Pal
www.learnbywatch.com | [email protected]