#ifndef UC_H_INCLUDED // These include guards prevent errors from class #define UC_H_INCLUDED // redefinition if this header file is included twice. #include #include // Avoid using namespace in a header file; that’s namespace pollution! // This class UC (for UpperCase) illustrates member and non-member functions. // It’s like the string class, but its data has no lowercase letters. // // The class is incomplete, but it shows how UC interacts with the other // popular strings: C-style strings and std::string. // // It implements ctors, assignment, +, +=, and ostream insertion (<<). // // TODO: // • string=UC; // • UC=char, UC+=char, UC+char, char+UC // • comparisons class UC { using string = std::string; // local alias for std::string to ease typing public: // Constructors (alias ctors) UC(); UC(const char *); UC(const string &); UC(const UC &); // Destructor ~UC(); // Assignment operators // Return a reference to *this. UC& operator=(const UC &); // operator+=, which modifies this object. // It’s a sort of assignment operator, so return a reference to *this. UC& operator+=(const UC &); // Accessors to extract the underlying data. [[nodiscard]] const string& str() const; [[nodiscard]] const char *c_str() const; private: void upify(); // Convert data to uppercase. string data; // The actual data for this object lives here. }; // Here, operator+ must be a non-member function, because it might have // a non-UC as the left operand, e.g.: // UC u("foo"); // cout << "xyz" + u; // operator+, which returns a fresh UC object. // Return a temporary object by value. UC operator+(const UC &, const UC &); // This must be a non-member function. std::ostream& operator<<(std::ostream &, const UC &); #endif /* UC_H_INCLUDED */