C++ copy constructor

C++ Copy constructor

We briefly mentioned about the copy constructor in the previous pages we used the copy constructor

integer(integer &i);

A copy CONSTRUCTOR is used for declare and initialize an object from another object below example

integer I2(I1);

would define the object I2 and at the same time initialize it to the value of I1. Another form of This statement is

Integer I2=I1;

The process of initializing through a copy constructor is known as copy initialization member let us take example in the below

I2=I1;

will not invoke the copy constructor. However it I1 , I2 are objects. This statement is legally and simply assigns the values of I1 to I2, member by member. This is the task of the overloaded assignment operator=. discuss in the next pages

A copy constructor takes a reference of an object of the same class as itself as an argument. let us consider a simple example of constructing and using a copy constructor as show in the program

// COPY CONSTRUCTOR
#include<iostream>
using namespace std;
class code
{
int id;
public :
Code()
{
}
Code()
{
id=x.id;
}
void display()
{
cout << id;
}
};
void main()
{
code A(100); //object A is created and initialized
code B(A); // copy constructor called
code C=A; // copy constructor called again
code D; // D is created, not initialized
D=A; // copy constructor not called
std::cout << "\n id of A:";
A.display();
std::cout<<"\n id of B:";
B.display();
std::cout<<"\n id if C:";
C.display();
std::cout<<"\n id of D:";
D.display();
}

id of A: 100
id of B: 100
id of C: 100
id of D: 100

A reference variable has been used as an argument to the copy constructor. We cannot Pass the argument by value to a copy constructor When no copy constructor is defined the compiler supplies its own copy constructor

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise