Professional Documents
Culture Documents
(LAB-04)
Constructor Overloading and Copy Constructors
Table of Contents
1. Introduction 34
4. Concept Map 34
4.1 Function Overloading 34
4.2 Constructor Overloading – Parameterized and Nullary Constructors 35
4.3 Copy Constructors 35
7. Practice Tasks 38
7.1 Practice Task 1 38
7.2 Practice Task 2 39
7.3 Outcomes 39
7.4 Testing 39
9. Evaluation Criteria 40
Lectures: 6, 7, 8
Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore
o Pages: 212-213, 216-217
4. 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 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 flavours 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, int tw, float th) //Overloaded Constructor: parameterized ->1
...
example (int on, float th, int tw) //Another overloaded Constructor : Parameterized ->2
...
};
int main()
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.
6.1 Tools
Visual Studio 2008.
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
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.
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. When you finish them, put these tasks in the following folder:
\\dataserver\assignments$\OOP\Lab04
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.
University of Management and Technology Page 38
Department of Computer Science, SST
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
9. 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.
10.1 Books
- Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\