1BigMommaClass {
2 BigMommaClass(int, int);
3
4private:
5 ThingOne thingOne;
6 ThingTwo thingTwo;
7};
8
9BigMommaClass::BigMommaClass(int numba1, int numba2): thingOne(numba1 + numba2), thingTwo(numba1, numba2) {
10// Code here
11}
1
2struct Rectangle {
3 int width; // member variable
4 int height; // member variable
5
6 // C++ constructors
7 Rectangle()
8 {
9 width = 1;
10 height = 1;
11 }
12
13 Rectangle( int width_ )
14 {
15 width = width_;
16 height = width_ ;
17 }
18
19 Rectangle( int width_ , int height_ )
20 {
21 width = width_;
22 height = height_;
23 }
24 // ...
25};
26
27
1class MyClass { // The class
2 public: // Access specifier
3 MyClass() { // Constructor
4 cout << "Hello World!";
5 }
6};
1struct S {
2 int n;
3 S(int); // constructor declaration
4 S() : n(7) {} // constructor definition.
5 // ": n(7)" is the initializer list
6 // ": n(7) {}" is the function body
7};
8S::S(int x) : n{x} {} // constructor definition. ": n{x}" is the initializer list
9int main()
10{
11 S s; // calls S::S()
12 S s2(10); // calls S::S(int)
13}