In ANSI standard C++, a friend function of a class is not a member function of the class (hence does not need the scope resolution operator). It is, however, given the special privilege of having access to the private member variables of the class, just as the member functions do. A friend function must be declared in the class by inserting the keyword friend before the function return type in the class declaration.
Although Visual C++ is not strictly standard to ANSI and hence there have been problems with getting friend functions to work, Dev-C++ should be fine. To see these ideas in a working program and to ensure that your Dev-C++ compiler can handle friend functions, you can download and try compiling and running MoneyReader.cpp and infile.txt. These files are from Chapter 8, Display 8.8 of your text.
+, -, *, /, %, ==, <, >, <=, >=
are really C++ functions that use a special syntax for listing the
function arguments.
Rather than using a prefix syntax in which the function is before the
arguments (this is what you have been defining so far), these operators
use the infix syntax, where the function name is between the arguments.
An example will help highlight the utility of overloading these
operators.
Consider fractions.
These rational numbers can be represented as the quotient of 2 integers,
so to represent data of this type, you may define a class called
Fraction that has in it a member variable for the numerator
and another for the denominator.
The constructor to make new Fraction variables can be
defined so that you can create two variables for the values of 1/2 and
3/4 using the following statements:
Fraction x(1, 2); //Represents the fraction 1/2 Fraction y(3, 4); //Represents the fraction 3/4 |
add( x, y ) that take as arguments two
Fraction variables and adds them together.
This works, but is awkward.
A more familiar way to add these two fractions is to use the notation
x+y, which is possible with operator overloading!
A predefined operator such as + can be overloaded to add two fractions
by using the reserved word operator in the function definition.
The definition of addition operator can look like the following, which
is a friend function of the Fraction class and returns
the result of the addition, which is also of type Fraction:
friend Fraction operator +(Fraction x, Fraction y) {
//Create the function definition here as usual.
}
|
==" can also be
overloaded to test the equality of two fractions:
// within the Fraction class declaration
friend bool operator ==(Fraction x, Fraction y)
// some code here, such as the definition for main()
// define how the == operator works here.
bool operator ==(Fraction x, Fraction y) {
//Create the function definition here as usual.
}
|
<< and >> operators can also be
overloaded to facilitate easier output and input of Fraction
data types:
// within the Fraction class declaration
friend ostream& operator <<(ostream& outs, Fraction x)
// some code here, such as the definition for main()
// define how the << operator works here.
ostream& operator <<(ostream& outs, Fraction x) {
//Create the function definition here as usual.
}
|
cout to write x, which
is an object of type Fraction and originally could not
be output this way, to the screen as you would any predefined variable
types.
| Statement | Resulting Output | ||||
|---|---|---|---|---|---|
|
|
Your task in this lab is to write a rational number class as an ADT in a source code file called YourLastName(s)_306L7.cpp (depending on whether you do it alone or with a partner). Rational numbers can be expressed as a fraction p/q where p and q are each whole numbers and q does not equal 0. The member variables that represent the numerator and denominator should be integers and private to the class.
Include member functions for this class that does at least the following:
>> operator so that takes an
istream argument and gets a rational number written in the
form p/q from either the keyboard or a file depending upon
the istream parameter.
This function should change the value of the current fraction appropriately.
<< operator so that takes an
ostream argument and outputs a rational number as a fraction.
For example, suppose that you have a variable rational that is an
object of the class of rational numbers you designed, and the numerator is
a 2 and the demoninator is 3.
The following statement on the left will output the string on the right to
the console:
| Statement | Resulting Output | ||||
|---|---|---|---|---|---|
|
|
<, <=, >, and >=
operators by creating four functions that return a Boolean value depending
upon the relative size of two fractions.
| Hint: | You might want to use the member function that converts the rational number into a decimal here. |
|---|
==" operator by creating a function that
returns a Boolean depending upon the equality of two fractions.
| Warning: | Because 1/2 = 2/4, this function requires some care. |
|---|
Here are some relationships that you may find useful:
| | |
| | |
| | |
Be sure to:
const modifier in all member functions in which you
are not changing the value of the current fraction.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name(s)
// Lab #7: Put a sentence about what your program does here.
/* Put what your program does here */
When you have completed your program and have it working to your satisfaction, drop the source code and your Microsoft Word Lab write-up YourLastName(s)_306L7.doc into the CSC306_L07 dropbox on the Academic server.