A C++ class defines a new data type with data members and member functions. The compiler enforces language rules, but the designer is responsible for giving the type a coherent meaning and useful behavior. section 1.4.1 section 7.4
A common mistake is to treat a class as a collection of fields rather than a type with responsibilities. Decide which values are valid, which operations clients need, and how instances should behave. Consider initialization, destruction, performance, and, when relevant, inheritance. section 7.2.1 section 7.4
A class invariant is a condition that must hold after construction and between method calls. Constructors and member functions must preserve a meaningful state: for example, a Student’s age should be nonnegative. Encapsulation keeps the knowledge needed to maintain that state within the class, so clients do not have to coordinate unrelated data. section 7.2.1
Make special-member intent explicit
A Container may permit copying but prohibit assignment. Instead of hiding an unimplemented assignment operator in the private section, declare it = delete. Use = default for a default constructor when compiler-generated behavior is appropriate. This makes the intended operations easier to read and maintain. section 2.4
Listing 2.8 A simple class with defaulted and deleted members
class Container {
private:
std::vector<int> values;
public:
Container() = default; #1
Container(const Container& o) : values(o.values) {}
Container& operator=(const Container& o) = delete; #2
void add(int n) { values.push_back(n); }
};
int main() {
Container c1;
c1.add(42);
Container c2(c1);
// c2 = c1; #3
return 0;
}
The Container listing contrasts a defaulted constructor with a deleted assignment operator while retaining a copy constructor. section 2.4
Do not write special members automatically: compiler-supplied behavior is often sufficient. Check the rules when adding one, however; for example, supplying any constructor prevents automatic generation of a default constructor. section 2.4
Design a usable numeric type
A Rational type needs more than numerator and denominator fields. Its interface should prevent invalid states and provide intuitive operations. One design validates the denominator during construction and exposes addition and multiplication operators; this is more deliberate than allowing unrestricted changes to the stored values. section 7.4
Listing 7.4 An implementation that provides intuitive use
class Rational {
private:
int num;
int den;
static int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }
void reduce();
int validate(int v) {
return v != 0 ? v : throw
std::invalid_argument("zero denominator");
}
public:
Rational(int n, int d=1) : num(n), den(validate(d)) { reduce(); }
Rational operator+(const Rational& o) const;
Rational operator*(const Rational& o) const;
friend std::ostream& operator<<(std::ostream&, const Rational&);
};
void Rational::reduce() {
int div = gcd(num, den);
num = (den > 0 ? 1 : -1) * num / div;
den = abs(den) / div;
}
Rational Rational::operator+(const Rational& o) const { #1
return Rational(num * o.den + den * o.num, den * o.den);
}
Rational Rational::operator*(const Rational& o) const { #2
return Rational(num * o.num, den * o.den);
}
std::ostream& operator<<(std::ostream& out, const Rational& r) { #3
out << r.num << '/' << r.den;
return out;
}
int main() {
Rational r1(1, 3);
Rational r2(2, 4);
std::cout << r1 + r2 << '\n';
//Rational(1, 0);
return 0;
}
The Rational listing shows denominator validation and operator declarations alongside private representation. section 7.4




















