Struct Constructor in C++?

前端 未结 16 1823
轻奢々
轻奢々 2020-11-27 08:53

Can a struct have a constructor in C++?

I have been trying to solve this problem but I am not getting the syntax.

16条回答
  •  醉酒成梦
    2020-11-27 09:34

    In c++ struct and c++ class have only one difference by default struct members are public and class members are private.

    /*Here, C++ program constructor in struct*/ 
    #include 
    using namespace std;
    
    struct hello
        {
        public:     //by default also it is public
            hello();    
            ~hello();
        };
    
    hello::hello()
        {
        cout<<"calling constructor...!"<

提交回复
热议问题