A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.
class StackNode {
public:
int val;
StackNode* top; // pointer for top
StackNode* prev; // pointer for previous node
// constructor
StackNode (int x) {
this->val = x; // save data
prev = NULL;
}
}
As you can see that, 'StackNode' is memeber function of StackNode class as a constructor. And there are some general characteristics for constructor.
- Constructor has same name as the class itself
- Constructors don’t have return type
- A constructor is automatically called when an object is created.
- If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).
It is simple summary of constructor in C++.
Reference
[1] https://www.geeksforgeeks.org/constructors-c/
'개발' 카테고리의 다른 글
Windows10에 Django 개발 환경 설치하기 (0) | 2020.01.16 |
---|---|
왜 Django는 좋을까? (0) | 2020.01.14 |
[책 소개 및 1장] WebRTC는 무엇인가? (0) | 2020.01.02 |