You are on page 1of 9

Lab Manual for Object Oriented Programming

(LAB-04)
Constructor Overloading and Copy Constructors

University of Management and Technology


Department of Computer Science, SST
Constructor Overloading and Copy Constructors

Table of Contents
1. Introduction 34

2. Activity Time boxing 34

3. Objective of the Experiment 34

4. Concept Map 34
4.1 Function Overloading 34
4.2 Constructor Overloading – Parameterized and Nullary Constructors 35
4.3 Copy Constructors 35

5. Home Work Before Lab 36


5.1 Practices from home 36

6. Procedure & Tools 36


6.1 Tools 36
6.2 Setting-up Visual Studio 2008 36
6.3 Walkthrough Task 36

7. Practice Tasks 38
7.1 Practice Task 1 38
7.2 Practice Task 2 39
7.3 Outcomes 39
7.4 Testing 39

8. Evaluation Task (Unseen) 39

9. Evaluation Criteria 40

10. Further Reading 40


10.1 Books 40
10.2 Slides 40

University of Management and Technology Page 33


Department of Computer Science, SST
Constructor Overloading and Copy Constructors

Lab 04: Constructor Overloading and Copy


Constructors
1. Introduction
In the previous lab a detailed practice session was conducted that focused on access specifiers and constructors.
Constructors are special functions that are automatically called when an object is created. This lab is geared towards
an extended use of constructors through overloading. Another flavour of constructors is the copy constructor which
creates an object by using a previously implemented object. This can be accomplished through the use of a copy
constructor.

Relevant Lecture Material

 Lectures: 6, 7, 8
 Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore
o Pages: 212-213, 216-217

2. Activity Time boxing


Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
5.1 Evaluation of Design 25 mins 25 mins
6.2 Setting-up Visual Studio 5 mins 5 mins
6.3 Walkthrough Tasks 35 mins 35 mins
7 Practice tasks 50 + 10 (mins) 60 mins
8 Evaluation Task 55 mins 55 mins
Total Time 180 Minutes

3. Objective of the Experiment


After completing this lab the student should be able to:
 Develop a constructor and then overload it
 Understand the difference between a parameterized constructor and non parameterized constructor
 Develop a copy constructor and facilitate the copying of data from one object to the other.

4. Concept Map

4.1 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 flavours are presented.

int fun (int, float, float);

int fun (int, float);

University of Management and Technology Page 34


Department of Computer Science, SST
Constructor Overloading and Copy Constructors

int fun (float, float, int);

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.

4.2 Constructor Overloading – Parameterized and Nullary Constructors


Constructors are designed to help initialize/ create an object. A constructor is a special function that is automatically
called upon the creation of an object. The important fact that needs to be communicated is that constructors do not
have a return type. In fact using the keyword void is also prohibited.
Constructors are of two types namely:
 Nullary Constructors / Parameterless Constructors – Those constructors that do not need a parameter to be
called.
 Parameterized Constructors – Those constructors that require parameters for their calling
Inside a class C++ allows us to overload a constructor just like any other function. This means in the same class you
can have a nullary constructor alongside a parameterized constructor. Consider the code below for further reference.

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;

4.3 Copy Constructors


Copy constructors provide a function through which you can create a new object from an existing already created
object. This feature is commonly used in simple programming and hence its need can arise at any time when working
with objects. C++ provides a default copy constructor that will assist in the copying of simple objects.

University of Management and Technology Page 35


Department of Computer Science, SST
Constructor Overloading and Copy Constructors

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:

clss obj2(obj1); // Function calling notation


clss obj2 = obj1; //Assignment statement notation

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.

5. Home Work Before Lab


Provided below is a statement for a program which you will code and submit to your lab instructor.

5.1 Practices from home


Your task is to create a class called examination. The class has data members duration, credit_hours, course title,
month, date, year and time. Your task is to create the individual member functions and call them using the class
constructor. Be very vigilant in determining the access specifiers for the data members and member functions.

6. Procedure & Tools

6.1 Tools
Visual Studio 2008.

6.2 Setting-up Visual Studio 2008 [Expected time = 5 mins]


Setup Visual Studio and make a project named “student”.

6.3 Walkthrough Task [Expected time = 35 mins]


Write a program that creates a class called student. The data members of the class are name and age.
 Create a nullary constructor and initialize the class object.
 Create a parameterized constructor that can set the values being passed from the main function.
 Create a display function called showall( ) which will be used to show values that have been set.
Use the default copy constructor to show that copying of simple objects can be accomplished through the use of the
default copy constructor.

6.3.1 Writing Code


In the source file created in the project “student” write the following C++ code:

University of Management and Technology Page 36


Department of Computer Science, SST
Constructor Overloading and Copy Constructors

class student
{
private:
string name;
int age;
public:

student() //Nullary constructor


{
cout<<"Enter name ";
cin>>name;
cout<<"\nEnter age ";
cin>>age;
}
student(string n, int a) //parameterized Constructor
{
name=n;
age=a;
}
void showall()
{
cout<<"\nName= "<<name;
cout<<"\nAge= "<<age;
}
};
int main()
{
student s1; //Creation through nullary constructor
student s2("Ali", 30); //Creation through parameterized constructor
s1.showall();
s2.showall();
student s3(s1); //Calling copy constructor for s3
s3.showall();
return 0;
}

Figure 1: The student class demonstrating the use of a parameterized and nullary constructor. Also note the default
copy constructor being used

University of Management and Technology Page 37


Department of Computer Science, SST
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.3.3 Executing the Program


A sample output after running the program is shown below. Also run the code with other possible inputs.

Figure 2: Final output of student program.

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

7.1 Practice Task 1 [Expected time = 50 mins]


VISION is a world leader in manufacturing LCD Televisions. The company has decided that it will allow its customers
to give the dimensions of the TV (in length and width). Once the length and width are ordered the company will
manufacture the TV according to your requirements. In this regard they want you to create a program that will assist
them. Carefully read all the instructions and follow the requirements.
 Create a class called vision
 Create three constructors as follows:
- A nullary constructor that calls the setlength( ) and setwidth( ) function.
- A parameterized constructor that will receive the length and width as integers
- A parameterized constructor that will receive the length and width in float
 By using a special function calculate the area of the TV
 Create a function to calculate the price of the TV by multiplying the area with Rs. 65.
 Create a display( ) function to show the details of the purchased TV.

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.2 Practice Task 2 [Expected time = 10 mins]


Once you have completed the above task you are also required to use the default copy constructor to show that
values have been copied to the new created object. Use the display( ) function to show the individual objects.

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

Using parameterized constructor (integer) Your LCD purchase details are:


Length = 48
Length = 48 Width=30
Width = 30
Price=93600

Using parameterized constructor (float) Your LCD purchase details are:


Length = 48
Length = 40.5 Width=30
Width = 30.5
Price=80291.25

Test Cases for Practice Task-2


Sample Inputs Sample Outputs
Initialize only one of the objects and use it for Call the display function using the new object;
copying values into another object by using the
copy constructor The new object will contain the values of the previous object.

Table 2: Confirmation of practice tasks T1, T2

Practice Tasks Confirmation


T1
T2

8. Evaluation Task (Unseen) [Expected time = 55 Mins]


The lab instructor will assign you an unseen task depending upon the progress of the students.

University of Management and Technology Page 39


Department of Computer Science, SST
Exception Handling

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.

Table 3: Evaluation of the Lab

Sr. Task No Description Marks


No.
1 4.1 Problem Modelling 10
2 6 Procedures and Tools 5
3 7.1 Practice task 1 with Testing 30
4 7.2 Practice task 2 with Testing 20
5 8.1 Evaluation Tasks (Unseen) 25
6 Good Programming 10
Practices
Total Marks 100

10. Further Reading

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$\

University of Management and Technology Page 40


Department of Computer Science, SST

You might also like