Professional Documents
Culture Documents
Contrustor Over Loading
Contrustor Over Loading
Constructor Overloading
and
Copy Constructors
Constructor Overloading and Copy Constructors
Lectures: 6, 7, 8
Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert
Lafore o Pages: 212-213, 216-217
Function Overloading
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 or the number 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.
Constructor Overloading and Copy Constructors
class example
{
private:
int one;
int two;
float three;
public:
example( ) //Nullary constructor
{
...
}
example (int on, int tw, float th) //Overloaded Constructor: parameterized ->1
{
...
}
example (int on, float th, int tw) //Another overloaded Constructor : Parameterized ->2
{
...
}
};
int main()
{
example obj; //Creation through nullary constructor
example obj2(1, 2, 3.3); //Creation through first parameterized constructor
example obj3(1, 3.3, 2); //Creation through Second parameterized constructor
return 0;
}
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 clss 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.
Home Work
Provided below is a statement for a program which you will code.
Writing Code
In the source file created in the project “student” write the following C++ code:
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
Constructor Overloading and Copy Constructors
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to finish the
tasks in the required time.
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.