Constructor:
Constructor is a special member function whose task is to initialize the objects of its class. Constructor is invoked automatically when objects are created. It is known as special because it has same name as the class name to which it belong. It is called constructor because it constructs the values of the data members of the class.The following are the properties of the constructor functions:
1. Constructor should be declared in the public section.
2. They do not have return type even void.
3. They are invoked automatically whenever an object of associated class is created.
4. They can not have default arguments.
5. No. one can refer to their addresses.
6. It can not be virtual.
Example program:
#include<iostream>
#include<conio.h>
using namespace std;
class simple
{
private:
int a,
b;
public:
simple();
//constructor
declared
void
print()
{
cout<<"A= "<<a<<"\n";
cout<<"B=
"<<b<<"\n";
}
};
simple::simple()
//constructor defined
{
a=10;
b=12;
}
int main()
{
clrscr();
simple obj;
//constructor called
obj.print();
getch();
return 0;
}
|
Output:
A= 10
B= 12
Copy Constructor:
The copy constructor is a constructor which creates an object by initializing it with an another object of the same class, which has been created previously. It invokes when a new object is initialized with exciting one. It is an overloaded constructor. A copy constructor takes a reference to an object of the same class as itself as an argument. when no copy constructor defined, the C++ compiler supplies its own copy constructor.syntax:
class_name(const class_name &obj)
{
// program statement
}
Example program:
#include<iostream>
using namespace std;
class point
{
private:
int m, n;
public:
point(int a, int b)
{
m=a;
n=b;
}
point(const point
&p)
//copy constructor
{
m=p.m;
n=p.n;
}
int getA()
{
return m;
}
int getB()
{
return n;
}
};
int main()
{
point obj1(15, 20); //simple constructor is called
point obj2 = obj1; //copy constructor is called
cout<<"Object value 1a:
"<<obj1.getA()<<", Object value 1b:
"<<obj1.getB();
cout<<"\n Object value 2a:
"<<obj2.getA()<<", Object value 2b:
"<<obj2.getB();
return 0;
}
|
Output:
Object value 1a: 15, Object value 1b: 20
Object value 2a: 15, Object value 2b: 20
Destructor:
Destructor is used to destroy the object that have been created by object. Destructor is a member function whose name is same as the class name like a constructor. But a destuctor is preceded by tilde(~) sign.Suntax:
~code() { }
The following properties of destructor:
1. A destructor invoked implicitly by the compiler upon exit from the program.
2. It is used to clean up the storage that is no longer accessible.
3. It never takes any argument nor return any value.
It is a good practice to declare destructor in a program since it releases memory space for future use. Objects are destroyed in the reverse order of creation.
Example program:
#include<iostream>
using namespace std;
int count=0;
class display
{
public:
display()
{
count++
cout
<<"\n Objects created "<<count;
}
~display()
{
cout
<<"\n Objects destroyed "<<count;
count--;
}
};
int main()
{
cout<<"\n\nMAIN\n";
display A1, A2;
{
cout
<< "\n\n BLOCK\n";
display A3;
}
cout<<"\n\nRE-ENTER MAIN\n";
return 0;
}
|
MAIN
Object created 1
Object created 2
BLOCK
Object created 3
Object destroyed 3
RE-ENTER MAIN
Object destroyed 2
Object destroyed 1
No comments:
Post a Comment