Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Object Oriented Programming

Constructor Overloading
and
Copy Constructors
Constructor Overloading and Copy Constructors

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

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.

int fun (int, float, float);

int fun (int, float);


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.
Constructor Overloading and Copy Constructors

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.
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.

Home Work
Provided below is a statement for a program which you will code.

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.

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.

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:

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
Constructor Overloading and Copy Constructors

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.

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.

You might also like