08/16/2022

A constructor in is a special member function that is use to initialize the object of its class. It
is special because its name is the same as the class name. So, The constructor is invoke
whenever an object of its associate class is creat. It is call a constructor because it constructs the value of data members of the class. Then, The constructor doesn’t have a return type. Moreover, I do not specify a constructor C++ compiler that generates the default constructor.

Characteristics of constructor

  1. They should be declared in the public section.
  2. Constructors are invoking automatically when the objects are create.
  3. They do not have return types, not even void and therefore, they cannot return values.
  4. They cannot be inherent, though a derived class can call the base class constructor.
  5. Like other C++ functions, they can have default arguments.
  6. Constructors cannot be virtual.
  7. We cannot refer to their addresses.
  8. An object with a constructor (or destructor) cannot be use as a member of a union.
  9. They make ‘implicit calls’ to the operators new and delete them when memory allocation is require.
  10. Note: when a constructor is declare for a class, the initialization of the class objects becomes mandatory.

Introduction to Copy Constructor:

A copy constructor is used to declare and initialize an object from another object.
integer i2(i1);
would define the object i2 and at the same time initialize it to the values of i1.
Another form of this statement is.
integer i2 = i1;
The process of initializing through a copy constructor is known as copy initialization.
The statement.
i2 = i1;
will not invoke the copy constructor.
However, if i1 and i2 are objects, this statement is legal and simply assigns the values of i1
to i2, member-by-member.
This is the task of overloading the assignment operator (=).
A copy constructor takes a reference to an object of the same class as itself as an argument.

Then, Using C programming to perform this Copy Constructor operation you can follow any language.

So, try it your self on online compiler.

#include<iostream.h>
#include<conio.h>
class Samplecopyconstructor
{
 private:
 int x, y; //data members
 
 public:
 Samplecopyconstructor(int x1, int y1)
 {
 x = x1;
 y = y1;
 }
 
 /* Copy constructor */
 Samplecopyconstructor (const Samplecopyconstructor &sam) {
 x = sam.x;
 y = sam.y;
 }
 
 void display()
 {
 cout<<x<<" "<<y<<endl;
 }
};
/* main function */
int main()
{
 Samplecopyconstructor obj1(10, 15); // Normal constructor
 Samplecopyconstructor obj2 = obj1; // Copy constructor
clrscr();
 cout<<"Normal constructor : ";
 obj1.display();
 cout<<"Copy constructor : ";
 obj2.display();
getch();
 return 0;
}
OUTPUT:
Normal constructor : 10 15
Copy constructor : 10 15

So, for more update going to –learning points – Home | A better learning future starts here

For more OOCP concept go for –Class and Object – Learning Points

Leave a Reply

Your email address will not be published.