Solved Lab03CopyConstructor
Solved Lab03CopyConstructor
03
Name: Swaira Riaz Roll_No. 22-CSE-18
Submitted to: DR.Nadia Rasheed Date: 10-27-2023
Background
Copy Constructor in C++
A copy constructor is a type of constructor that creates a copy of another object. If we want one object
to resemble another object, we can use a copy constructor. The copy constructor is a constructor
which creates an object by initializing it with an object of the same class, which has been created
previously.
If no copy constructor is written in the program compiler will supply its own copy constructor. If the
class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy
constructor.
The copy constructor is used to:
1. Initialize one object from another of the same type.
2. Copy an object to pass it as an argument to a function.
3. Copy an object to return it from a function.
Copy Constructor is of two types:
o Default Copy constructor: The compiler defines the default copy constructor. If the user
defines no copy constructor, compiler supplies its constructor.
o User Defined constructor: The programmer defines the user-defined constructor.
class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
In the above case, copy constructor can be called in the following ways:
Lab Task:
1.Observe, execute and provide output for the program given in file?
#include<iostream>
using namespace std;
class Number
{
int a;
public:
Number()
{
a = 0;
}
Number(int num)
{
a = num;
}
// When no copy constructor is found, compiler supplies its own copy constructor
Number(Number & obj)
{
cout<<"Copy constructor called!!!"<<endl;
a = obj.a;
}
void display()
{
cout<<"The number for this object is: "<< a <<endl;
}
};
int main()
{
Number x, y, z(45), z2;
x.display();
y.display();
z.display();
Number z1(z); // Copy constructor invoked
z1.display();
z2 = z; // Copy constructor not called
z2.display();
Number z3 = z; // Copy constructor invoked
z3.display();
// z1 should exactly resemble z or x or y
return 0;
}
Result:
2. Write a C++ program that implements all three types of constructors and their usage in
constructing an object.
#include<iostream>
#include<string>
using namespace std;
class customer
{
private:
string name ;
string ID ;
double Total_bill;
public:
customer() // default constructor
{
name = "Swaira Riaz";
ID = "****";
Total_bill = 500.01;
}
customer(string customer_name,string customer_ID,double
customer_Total_bill) // parametrized constructor
{
name = customer_name;
ID = customer_ID;
Total_bill =customer_Total_bill;
}
customer(const customer & other) // copy constructor
{
name = other.name;
ID = other.ID;
Total_bill = other.Total_bill;
}
void display()
{
cout<<"Name :"<<name<<", ID: "<<ID<<", Total_Bill: "<<Total_bill<<endl;
}
};
int main()
{
customer customer1;
cout<<"customer1(In default constructor)"<<endl;
customer1.display();
customer customer2("shia","****",799.9);
cout<<"customer2(in parametrized cunstructor)"<<endl;
customer2.display();
customer customer3 (customer2);
cout<<"customer3(copy constructor called!!)"<<endl;
customer3.display();
return 0;
}
Result: