Search This Blog

Loading...

how to create constructor in c++

Use of constructor is to initialize the variables.
It doesn't have the return type.
It is automatically called when object of that class is created.

#include<stdio.h>
#include<conio.h>

class demoClass
{

    demoClass()   // constructor with same name as class name
    {
        int number=0;
        string name="";
    }

};

void main()
{
    demoClass obj;  //create a object of class.
}

No comments:

Post a Comment