Member Function Initializer - C++

Hello,
In the book C++ How to program 5th Edition, Section 10.2, there is some code

Header File: Increment.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 1  // Fig. 10.4: Increment.h
 2  // Definition of class Increment.
 3  #ifndef INCREMENT_H
 4  #define INCREMENT_H
 5
 6  class Increment
 7  {
 8  public:
 9     Increment( int c = 0, int i = 1 ); // default constructor
10
11     // function addIncrement definition
12     void addIncrement()
13     {
14        count += increment;
15     } // end function addIncrement
16
17     void print() const; // prints count and increment
18  private:
19     int count;
20     const int increment; // const data member
21  }; // end class Increment
22
23  #endif 


Declaration: Increment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 1  // Fig. 10.5: Increment.cpp
 2  // Member-function definitions for class Increment demonstrate using a
 3  // member initializer to initialize a constant of a built-in data type.
 4  #include <iostream>
 5  using std::cout;
 6  using std::endl;
 7
 8  #include "Increment.h" // include definition of class Increment
 9
10  // constructor
11  Increment::Increment( int c, int i )
12     : count( c ), // initializer for non-const member        
13       increment( i ) // required initializer for const member
14  {
15     // empty body
16  } // end constructor Increment
17
18  // print count and increment values
19  void Increment::print() const
20  {
21     cout << "count = " << count << ", increment = " << increment << endl;
22  } // end function print 


I can't explain command line 11 > 13 int Increment.cpp
It's a constructor for Increment Class but I haven't seen something like that before.
Please help!

I found an old topic on this forum talk about this, but it was closed, no reply for that guy problem.
http://www.cplusplus.com/forum/beginner/23088/
I suppose what you expected was a code like this.

1
2
3
4
Increment::Increment( int c, int i ){
     count = c;
     increment =1;
}


If you do it this way, it will still work but not recommended. Before entering the body of the constructor code, all the private,protected or public variables will be assigned a default value. For this relatively simple example, it won't make much difference. But as your code gets more complex, you are better off assigning values to all the class variables even before entering the body of the constructor.

Note that even the order in which you do this matters. For example

 
Increment::Increment(int c, int i):count(c),increment(i){}


is not the same as

 
Increment::Increment(int c, int i):increment(i),count(c){}


although in this relatively simple example it will make no difference. If you are on Linux, try compiling with -Wall and you will get a warning stating that increment will be initialized after count.
"Member objects are constructed in the order in which they are declared in the class definition"

That's what i found in C++ How to program book.

Thanks
Topic archived. No new replies allowed.