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
0 Comments