Ask a Teacher
when we use constructor function in c++ |
A member function with the same name as its class is a constructor function. Constructors cannot return values. Specifying a constructor with a return type is an error, as is taking the address of a constructor. If a class has a constructor, each object of that type is initialized with the constructor prior to use in a program. (For more information about initialization, see Initialization Using Special Member Functions.) Constructors are called at the point an object is created. Objects are created as:
Example: // constructors.cpp // compile with: /c class MyClass { public: MyClass(){} MyClass(int i) : m_i(i) {} private: int m_i; }; |