0% found this document useful (0 votes)
16 views

Solved Lab03CopyConstructor

The document describes a lab assignment to implement and understand the working of copy constructors in C++. It provides background information on copy constructors, including their types and syntax. It then outlines two tasks for the lab: 1) Observe, execute, and provide the output of a program demonstrating when a compiler-supplied copy constructor is called. 2) Write a C++ program implementing default, parametrized, and user-defined copy constructors and demonstrating their usage.

Uploaded by

Swaira Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Solved Lab03CopyConstructor

The document describes a lab assignment to implement and understand the working of copy constructors in C++. It provides background information on copy constructors, including their types and syntax. It then outlines two tasks for the lab: 1) Observe, execute, and provide the output of a program demonstrating when a compiler-supplied copy constructor is called. 2) Write a C++ program implementing default, parametrized, and user-defined copy constructors and demonstrating their usage.

Uploaded by

Swaira Riaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab No.

03
Name: Swaira Riaz Roll_No. 22-CSE-18
Submitted to: DR.Nadia Rasheed Date: 10-27-2023

Objective: To implement and understand the working of copy constructor

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.

Syntax Of User-defined Copy Constructor:


1. Class_name(const class_name &old_object);

Consider the following situation:

class A
{
A(A &x) // copy constructor.
{
// copyconstructor.
}
}
In the above case, copy constructor can be called in the following ways:

Copy Constructor is called in the following scenarios:


o When we initialize the object with another existing object of the same class type. For
example, Student s1 = s2, where Student is the class.
o When the object of the same class type is passed by value as an argument.
o When the function returns the object of the same class type by value.

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:

You might also like