[CS 2337] Chapter 13: Overloading and Templates
friend function
(of a class) a nonmember function of the class that has access to all the members of the class •Use the reserved word friend in the function prototype in the class definition •Friendship is always given by the class class classIllusFriend { friend void two(/*parameters*/); . . . }; •friend does not appear in the heading of the function's definition •When writing the friend function's definition •The name of the class and the scope resolution operator are not used
operator function
a function that overloads an operator •Use reserved word operator followed by the operator as the function name
Class Templates
a single code segment for a set of related classes, called parameterized types •A template instantiation can be created with either a built-in or user-defined type •The function members of a class template are considered to be function templates
Templates
enable you to write generic code for related functions and classes
Function templates
simplify function overloading
Quick Review
•An operator that has different meanings with different data types is said to be overloaded •Operator function: a function that overloads an operator •operator is a reserved word •Operator functions are value-returning •Operator overloading provides the same concise notation for user-defined data types as for built-in data types •Only existing operators can be overloaded •The pointer this refers to the object •A friend function is a nonmember of a class •If an operator function is a member of a class •The leftmost operand of the operator must be a class object (or a reference to a class object) of that operator's class •Classes with pointer variables must overload the assignment operator, and include both the copy constructor and the destructor •In C++, template is a reserved word •Function template: a single code segment for a set of related functions •Class template: a single code segment for a set of related classes -Are called parameterized types •C++11 provides many functions to implement random number generator.
Why Operator Overloading Is Needed
•Assignment and member selection are the only built-in operations on classes •Other operators cannot be applied directly to class objects •Operator overloading extends the definition of an operator to work with a user-defined data type •C++ allows you to extend the definitions of most of the operators to work with classes
Restrictions for overloading an operator
•Cannot change the precedence of an operator •Associativity cannot be changed •Default parameters cannot be used •Cannot change number of parameters •Cannot create new operators •Cannot overload: . .* :: ?: sizeof •How the operator works with built-in types remains the same •Can overload for user-defined objects or for a combination of user-defined and built-in objects
Overloading the Stream Insertion (<<) and Extraction (>>) Operators
•Consider the expression: cout << myRectangle; •Leftmost operand is an ostream object, not a rectangleType object •Thus, the operator function that overloads << for rectangleType must be a nonmember function of the class •The same applies to the function that overloads >>
Pointer this
•Every object of a class maintains a (hidden) pointer to itself called this •When an object invokes a member function •this is referenced by the member function
Overloading Binary Operators
•If # represents a binary operator (e.g., + or ==) that is to be overloaded for rectangleType •It can be overloaded as either a member function of the class or as a friend function
Overloading Unary Operators
•If the operator function is a member of the class, it has no parameters •If the operator function is a nonmember (i.e., a friend function), it has one parameter
Syntax for Operator Functions
•It is a value-returning function •operator is a reserved word •To overload an operator for a class: •Include the operator function declaration in the class definition •Write the definition of the operator function
Operator Overloading
•Most existing C++ operators can be overloaded to manipulate class objects •New operators cannot be created
Function Overloading
•Overloading a function refers to having several functions with the same name, but different parameters •The parameter list determines which function will execute •Must provide the definition of each function
Header File and Implementation File of a Class Template
•Passing a parameter to a function takes effect at run time •Passing a parameter to a class template takes effect at compile time •Cannot compile the implementation file independently of the client code •Can put class definition and definitions of the function templates directly in the client code •Can put class definition and the definitions of the function templates in the same header file •Another alternative is to put class definition and function definitions in separate files •Include directive to the implementation file at the end of the header file •In either case, function definitions and client code are compiled together
Operator Overloading: Member versus Nonmember
•Some operators must be overloaded as member functions and some must be overloaded as nonmember (friend) functions •Binary arithmetic operator + can be overloaded either way •As a member function, operator + has direct access to data members of one of the objects •Need to pass only one object as a parameter •Overload + as a nonmember function •Must pass both objects as parameters •Code may be somewhat clearer this way
Templates
•Template: a single code body for a set of related functions (function template) and related classes (class template) •Type is the data type •declaration is either a function declaration or a class declaration •class in the heading refers to any user-defined type or built-in type •Type is a formal parameter to the template •Just as variables are parameters to functions, data types are parameters to templates
Function Templates
•Type is a formal parameter of the template used to: •Specify type of parameters to the function •Specify return type of the function •Declare variables within the function