Tutorials
C++In this C++ tutorial you will learn about Class Constructors and destructors in C++ viz., Constructors, What is the use of Constructor, General Syntax of Constructor, Destructors, What is the use of Destructors and General Syntax of Destructors.
The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.
Constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:
<class name> { arguments};
The default constructor for a class X has the form
X::X()
In the above example the arguments is optional.
The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator.
There are several forms in which a constructor can take its shape namely:
This constructor has no arguments in it. Default Constructor is also called as no argument constructor.
For example:
| class Exforsys { private: int a,b; public: Exforsys(); ... }; Exforsys :: Exforsys() { a=0; b=0; } |
This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.
For example to invoke a copy constructor the programmer writes:
Exforsys e3(e2);
or
Exforsys e3=e2;
Both the above formats can be sued to invoke a copy constructor.
For Example:
|
#include <iostream.h> void main() |
In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is
Example of Copy Constructor
e3=50
Some important points about constructors:
Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.
~ classname();
The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.
For example:
|
class Exforsys |
| I really liked the detailed information on this topic.... |
| I really liked the detailed information on this topic.... |
| this information is very useful...thanx |
| I RELLY LIKE THE INFORMATION GIVEN. It describe detailed information about the topic |
| why the name of Constructor is same has class name ? |
|
#include<iostream.h> class calc { private: int num1,num2,res; public: calc(int,int); void sum(); }; calc::calc(intx,inty) { num1=x; num2=y; res=0; } void calc::sum() { res=num1+num2; cout<<"the total of two numbers is:"<<res; } int main() { int var1,var2; cout<<"val1"; cin>>var1; cout<<"val2"; cin>>var2; calc cl(var1,var2); cl.sum(); return 0; } |
| It's a good explaination... |
| i wanna receive the content . |
| It is a good explanation but destructor is mentioned very little.It is not explained detailed.If it could have detailed more.we will be able to learn more.But constructor is good. |
| What is significance of the destructors in the case of pure virtual function? |
|
#include<iostream.h> #include<conio.h> class abc { int a,b; public: abc(int x,int y) { a=x; b=y; } abc(abc&s) { a=s.a; b=s.b; } void disp() { cout<<a<<b; } }; void main() { clrscr(); abc a1(4,5); abc a2=a1; a1.disp(); getch(); } |
| When we pass object as argument to a function, then whether constructor or destructor are called or not. |
|
what is the differnce between privat and public access specifiers........ #include class calc { private: int num1,num2,res; public: calc(int,int); void sum(); }; calc::calc(intx,inty) { num1=x; num2=y; res=0; } void calc::sum() { res=num1+num2; cout<<"the total of two numbers is:"< } int main() { int var1,var2; cout<<"val1"; cin>>var1; cout<<"val2"; cin>>var2; calc cl(var1,var2); cl.sum(); return 0; } [in this prog if we not use private wat will be the output]what is the main use of private and publis....plz help me am in great confusion |
| What about recursive constructors? Please explain about it! |
| what is parameterized constructor? explain with example. |