A constructor of a class C is a member function named C, without a return type, that the compiler calls each time an object of C is created; its member initialiser list (after the :) gives each data member its first value. The destructor ~C() is the member function the compiler calls when the object’s lifetime ends; a class has exactly one.
示例
Example 1.8 (Building )
The initialiser list sets num_ = 6, den_ = -8. The body finds den_ nonzero and negative, so it negates both, , then divides both by (std::gcd returns a non-negative result): . The sum a + b is built by the constructor too, from over , and comes out as . The expression a + 1 works because Fraction(long whole) is a constructor with one argument: the compiler uses it to convert 1 into . The defaulted operator== compares the members one by one, which is correct only because the invariant makes the representation unique.