#include <stdio.h>
Class Test
{
Private
int i;
Int J;
int k;
Public:
void Initialize ()
{
i=0;
J=1;
I=p;
}
void print ()
{
printf ("i=%d,j=%d,k=%d", i,j,k);
}
}; Semicolons are critical
int main ()
{
Test T1;
T1.initialize (); constructor function
T1.print ();
return 0;
}
Using constructors
#include <stdio.h>
Class Test
{
Private
int i;
Int J;
int k;
Public:
Test (int v)////is the same as the class name
{////constructor definition can have parameter cannot have return type
No return type is different from return type void
I=j=k=v;
}
void print ()
{
printf ("i=%d, j=%d, k=%d \ n", i,j,k);
}
}; Semicolons are critical
int main ()
{
Test T1 (1); Automatically call constructors
Test t2=2; constructor function
Test t3= Test (3);
T1.print ();
T2.print ();
T3.print ();
Test Ta[3]={test (1), Test (2), Test (3)};
for (int i=0; i<3; i++)
{
Ta[i].print ();
}
return 0;
}
C + + constructor learning