Header Ads Widget

What is Constructor in C++ || Constructor in C++ || Function type

 

Constructor in C++

 


 

i. Constructor is a special type of function which has the same name as the class name.

ii. Constructor is being automatically called at the time of object declaration.

iii. The return type of the constructor is the class type.

 

Syntax of constructor:-

Class_name()
{
//code
}

 

 Example of constructor:-

            #include<iostream>
            using namespace std;
            class try
            {
               public:
               try()
            {
             cout<<”Hello”;
            }
            };


            int main()
            {
            try obj;
            return 0;
            }

Output

            Hello

Post a Comment

0 Comments