0% found this document useful (0 votes)
28 views5 pages

Copy Cons

Oop

Uploaded by

mianh2221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Copy Cons

Oop

Uploaded by

mianh2221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming

Week 5

Lecture 1 & 2

Copy Constructor:
C++ provides a special type of constructor which takes an object as an argument and is used to
copy values of data members of one object into another object. In this case, copy constructors are
used to declaring and initializing an object from another object.

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. The copy constructor is used to −

 Initialize one object from another of the same type.

 Copy an object to pass it as an argument to a function.

 Copy an object to return it from a function.

If a copy constructor is not defined in a class, the compiler itself defines one. If the class has
pointer variables and has some dynamic memory allocations, then it is a must to have a copy
constructor.

For example:

Calc C2(C1);
Or

Calc C2 = C1;

The process of initializing through a copy constructor is called the copy initialization.

Page 1 of 5
Object Oriented Programming

Programming Example:

class CopyCon {
int a, b;

public:
CopyCon(int x, int y)
{
a = x;
b = y;
cout << "\nHere is the initialization of Constructor";
}
void Display()
{
cout << "\nValues : \t" << a << "\t" << b;
}
};

void main()
{
CopyCon Object(30, 40);
//Copy Constructor
CopyCon Object2 = Object;
Object.Display();
Object2.Display();
}

Destructor Function:
As the name implies, destructors are used to destroy the objects that have been created by the
constructor within the C++ program. Destructor names are same as the class name but they are

Page 2 of 5
Object Oriented Programming

preceded by a tilde (~). It is a good practice to declare the destructor after the end of using
constructor. Here's the basic declaration procedure of a destructor:

How destructor Works:


A destructor is a special member function that works just opposite to constructor, unlike
constructors that are used for initializing an object, destructors destroy (or delete) the object.
Similar to constructor, the destructor name should exactly match with the class name.

Syntax of Destructor

~class_name()
{
//Some code
}
similar to constructor, the destructor name should exactly match with the class name. A
destructor declaration should always begin with the tilde(~) symbol as shown in the syntax
above.

When does the destructor get called?

A destructor is automatically called when:


1) The program finished execution.
2) When a scope (the { } parenthesis) containing local variable ends.
3) When you call the delete operator.

Destructor rules

1) Name should begin with tilde sign(~) and must match class name.
2) There cannot be more than one destructor in a class.
3) Unlike constructors that can have parameters, destructors do not allow any parameter.
4) They do not have any return type, just like constructors.
5) When you do not specify any destructor in a class, compiler generates a default destructor
and inserts it into your code.

Page 3 of 5
Object Oriented Programming

Programming Example:

#include <iostream>
using namespace std;
class HelloWorld{
public:
//Constructor
HelloWorld(){
cout<<"Constructor is called"<<endl;
}
//Destructor
~HelloWorld(){
cout<<"Destructor is called"<<endl;
}
//Member function
void display(){
cout<<"Hello World!"<<endl;
}
};
int main(){
//Object created
HelloWorld obj;
//Member function called
obj.display();
return 0;
}

Page 4 of 5
Object Oriented Programming

Output of the Program

Constructor is called
Hello World!
Destructor is called

Page 5 of 5

You might also like