Constructor:- Constructor is the member function of the class whose work is to initialised objects. Important Properties/characteristics ...
Constructor:-Constructor is the member function of the class whose work is to initialised objects.
Important Properties/characteristics of Constructor:-
1)Constructor declare in public section.
2)Constructor don't have return type.
3)Constructor can not be virtual type.
4)Constructor is used to allocate memory for variables.
5)Constructor is member function of class.
6)Constructor name is same as name of class.
7)Class may have more than 2 Constructor.
8)Constructor may be parametrised and copied.
9)Constructor concept used in many languages like c ++(c plus plus),java,c#(c sharp) etc.
Type of Constructor:-
1)Default Constructor.
2)Copy Constructor.
3)Parameterized Constructor.
Example of Constructor(in c++):-I explain Constructor example in c++ but you can also created it with c sharp(#)(in place of cout puts Console.WriteLine and in place of cin puts Console.ReadLine) as same as like java.(But some concept are different)
#include<iostream.h>
#include<conio.h>
class number
{
int x,y,z;
public:
number()
{
x=20;
y=40;
}
void sum()
{
z=x+y;
cout<<z;
}};
void main()
{
class no; //object
clrscr();
no.sum();
getch();
}
Important Properties/characteristics of Constructor:-
1)Constructor declare in public section.
2)Constructor don't have return type.
3)Constructor can not be virtual type.
4)Constructor is used to allocate memory for variables.
5)Constructor is member function of class.
6)Constructor name is same as name of class.
7)Class may have more than 2 Constructor.
8)Constructor may be parametrised and copied.
9)Constructor concept used in many languages like c ++(c plus plus),java,c#(c sharp) etc.
Type of Constructor:-
1)Default Constructor.
2)Copy Constructor.
3)Parameterized Constructor.
Example of Constructor(in c++):-I explain Constructor example in c++ but you can also created it with c sharp(#)(in place of cout puts Console.WriteLine and in place of cin puts Console.ReadLine) as same as like java.(But some concept are different)
#include<iostream.h>
#include<conio.h>
class number
{
int x,y,z;
public:
number()
{
x=20;
y=40;
}
void sum()
{
z=x+y;
cout<<z;
}};
void main()
{
class no; //object
clrscr();
no.sum();
getch();
}
If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.
COMMENTS