Dec 6, 2004 ----------- Quiz #8 const revisited - why use the const modifier? - const is a form of automatic error checking - const before a reference parameter makes it a constant parameter - can be used for member functions as well, if the member function does not modify the calling object void output (ostream& outs) const; - const is an all or nothing proposition - must use const everywhere that it is appropriate for a class or nowhere - if not, function calls within function calls may cause errors double calc_grade (const Student s) { ... // without information compiler will assume // getExam1() can modify data members of s s.getExam1() } - succinct summary of the const parameter modifier on p. 483 Friends (continued) - friends are not member functions, but have access to the private members of a specified class just like a member function - example class Student { public: friend bool equal (Student stud1, Student stud2); ... private: int average; ... }; main() { ... } bool equal (Student stud1, Student stud2) { return stud1.average == stud2.average; } - why use friends? - matter of efficiency and personal taste - not necessary - only use friends when it makes the definition of the function simpler and more efficient - rule of thumb - use a member function if the task being performed by the function involves only one object - use a non-member function if the task being performed involves more than one object, e.g., equal() - succinct summary of friend functions on p. 468 Overloading operators - is there a difference between an operator and a function? - precede function name with the keyword "operator" - at least one argument must be a class type - overloaded operator may be a member function or a (non-friend) function - you cannot create a new operator; can only overload existing operators such as +, -, *, /, %, and so on - you cannot change the # of operands an operator takes; e.g., you cannot change % from a binary operator to a unary operator - you cannot change the precedence of an overloaded operator - the following operators cannot be overloaded - dot (.) - scope resolution (::) - ternary (?:) - the assignment (=) and [] and -> operators are overloaded in a different manner - not covered here - an operator may be a friend of a class although this is not required - can overload unary operators (-, ++, and --) - overloading the postfix versions of ++ and -- (e.g., x++, x--) is done differently than shown here - overloading << and >> - remember that an expression involving operators returns a value cout << student << " is bright!" << endl; ((cout << student) << " is bright!") << endl; - examples friend ostream& operator << (ostream& outs, const Student& stud) { ... return outs; } friend istream& operator >> (istream& ins, Student& stud) { ... return ins; } - succinct summary of overloading >> and << on p. 499 - succinct summary of operator overloading on p. 490 Constructors for Automatic Type Conversion Grade g1 (98.2), g2; g2 = g1 + 45.5; - seems natural with overloaded numeric operators - automatic conversions apply the same way to - arguments for ordinary functions - arguments for member functions - arguments for other overloaded operators Converting a digit to an int int digit_to_int (char d) { ... }