3-Constructor Overloading and Copy Constructor
3-Constructor Overloading and Copy Constructor
Page 18
Constructor Overloading and Copy Constructors
3. Concept Map
Function overloading is an advanced concept in modern languages where two function can have the same name. The
question that arises here is that how does a compiler know which function to call. The simple answer is that the
function calling is determined based on the type of parameters/ the number of parameters being passed/ order of
parameters being passed. Hence two function can have the same name but there must be a difference in the number
of parameters, type of parameters or the order of parameters. For example in the function prototypes below the
function fun( ) has been overloaded and its different flavors are presented.
It is important to highlight here that if two functions have a different return type then it does not mean that they are
overloaded. For a function to be overloaded it is necessary for the function to exhibit changes in the parameters.
class example
{
private:
int one;
int two;
float three;
public:
example( ) //Nullary constructor
{
...
}
example (int on, float th, int tw) //Another overloaded Constructor
: Parameterized ->2
{
...
}
};
int main()
{
example obj; //Creation through nullary constru
example obj2(1, 2, 3.3); //Creation through first parameterized constru
example obj3(1, 3.3, 2); //Creation through Second parameterized constru
return 0;
}
Page 20
Constructor Overloading and Copy Constructors
example objects that do not use pointers or arrays. Although the default copy constructor will copy any type of
object but it is strongly recommended that the copy constructor be used only for objects that have non pointer data
members. The default copy constructor performs a member by member copy i.e. the copy constructor creates an
exact copy where data members are copied one by one to the new object. Always remember that in copy
constructors the original object is maintained in its original state and the copy changes are only exhibited in the
newly created object. In this lab we will just restrict ourself to the use of the default copy constructor.
The default copy constructor can be explicitly called as follows:
Both statements create an object of the class. Of course any of the above statements can be used for copying an
object into another object.
Caution: The copy constructor is always called at the time of creating a new object. If at any time after the creation
of objects you copy contents of one object to the other then the copy constructor is not called. We will discuss more
on this in future lectures.
6.1 Tools
Visual Studio 2012.
Page 21
Constructor Overloading and Copy Constructors
class student
{
private:
string name;
int age;
public:
Figure 1: The student class demonstrating the use of a parameterized and nullary constructor. Also note the default
copy constructor being used
Page 22
Constructor Overloading and Copy Constructors
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
6. Practice Tasks
In the main you will construct three objects that demonstrate the use of the three constructors. After calling the
constructor it will take over and will handover control to the area function, and then the price calculation function.
Remember that the user should not have access to modifying the price.
Determine the access specifiers, data members and member functions. Also note that each constructor can / will
have a distinct functionality in its body. Hence do not try to copy code from one constructor to the other. Focus on
the design clarity and quality of your code.
Page 23
Constructor Overloading and Copy Constructors
7.3 Outcomes
After completing this lab, students will be able to construct a class object by using parameterized constructors. The
students will also be familiar with the use of a constructor and destructor.
7.4 Testing
Test Cases for Practice Task-1
Sample Inputs Sample Outputs
Using Nullary constructor Your LCD purchase details are:
Length = 48
Length = 48 Width=30
Width = 30
Price=93600
Page 24
Constructor Overloading and Copy Constructors
7. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is assigned
marks which will be evaluated by the instructor in the lab depending on the accomplishment of the assigned tasks.
8. Further Reading
10.1 Books
• Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
10.2 Slides
Page 25