Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: OVERLOADING AND TEMPLATES In this chapter, you will: Learn about overloading Become aware of the restrictions on operator overloading Examine the pointer this Learn about friend functions Explore the members and nonmembers of a class Discover how to overload various operators Learn about templates Explore how to construct function templates and class templates The capability of combining data and operations on data is called encapsulation. This is the first principle of OOD. In Chapter 12, we also defined abstract data type (ADT) and discussed how classes in C++ implement ADT. In Chapter 13, we discussed how new classes could be derived from existing classes through the mechanism of inheritance and composition. Inheritance is the second principle of OOD, as defined in Chapter 13, and encourages code reuse. WHY OPERATOR OVERLOADING IS NEEDED Recall the class clockType as defined in Chapter 12. Consider the following statements. clockType myClock(8,23,34); clockType yourClock(4,5,30); myClock.printTime(); myClock.incrementSeconds(); if(myClock.equalTime(yourClock)) . . . We would like to use the following statements in place of the above statements. cout << myClock; myClock++; if(myClock == yourClock) . . . The only built-in operations on classes are the assignment operator and the member selection operator. Therefore, other operators cannot be applied, blockquoteectly, on class objects. C++ allows the programmer to extend the definitions of most of the operators to work with classes In C++s terminology, this is called operator overloading. OPERATOR OVERLOADING C++ allows the user to overload most of the operators to work effectively in a specific application. C++ does not allow the user to create new operators. Most of the existing operators can be overloaded to manipulate class objects. In order to overload an operator we must write functions. The name of the function that overloads an operator is the reserved word operator followed by the operator to be overloaded. The following function name overloads >= operator>= Operator function: The function that overloads an operator. Syntax for Operator Functions The syntax of the heading of an operator function is returnType operator operatorSymbol(arguments) The operator function is a value-returning function. In C++, operator is a reserved word. To overload an operator for a class: 1. Include the function to overload the operator (that is, the operator function) in the definition of the class. 2. Write the definition of the operator function. Overloading an Operator: Some Restrictions When overloading an operator the following should be kept in mind....
View Full Document

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Markhieber during the Spring '08 term at University of Missouri-Kansas City .

Page1 / 53


This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online