CS 1410 Programming Assignment #8: Overloading Operators in Fraction Class, Assignments of Computer Science

Programming assignment #8 for cs 1410, which involves converting member functions in a fraction class into overloaded operators. The assignment covers converting arithmetic functions into operators, creating operator<< and operator>> functions, and using conversion constructors. Students are required to modify their calc program to utilize the overloaded operators and submit three files: fraction.h, fraction.cpp, and calc.cpp.

Typology: Assignments

Pre 2010

Uploaded on 07/22/2009

koofers-user-qm8-1
koofers-user-qm8-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 2
CS 1410 Programming Assignment #8
Fraction Program (Version 3)
Assignment
Convert the member functions in your fraction class (lab #5) into overloaded operators as
described below. Modify your calc program to utilize the overloaded operators.
Program Description
1. Convert the four arithmetic functions to overloaded operators (note that all parameters
and returns are pass-by-value and that all operators are members of the fraction class)
a. friend fraction operator+(fraction f1, fraction f2)
b. friend fraction operator-(fraction f1, fraction f2)
c. friend fraction operator*(fraction f1, fraction f2)
d. friend fraction operator/(fraction f1, fraction f2)
e. Friend functions are NOT members of a class. However, they are declared (i.e.,
prototyped) in the class and the keyword “friend” allows them to access the
private data contained in the class.
2. Replace the print function with operator<< as demonstrated in class (see pp. 616-
620). Make it inline or a regular function as you choose.
3. Convert the read function to operator>> as demonstrated in class. It may be either
inline or a regular function as you choose.
4. A conversion constructor has one parameter and converts one data type to another (the
parameter type to the class-type of the defining class). For example:
fraction(int n) : numerator(n), denominator(1) {}
is a conversion constructor that converts the integer n into the fraction n/1. By using
default parameters, it is often possible to make one constructor serve both as a “normal”
constructor and as a conversion constructor. You created a conversion constructor in Lab
#4:
fraction(int n, int d = 1) . . .
pf2

Partial preview of the text

Download CS 1410 Programming Assignment #8: Overloading Operators in Fraction Class and more Assignments Computer Science in PDF only on Docsity!

Page 1 of 2

CS 1410 Programming Assignment

Fraction Program (Version 3)

Assignment

Convert the member functions in your fraction class (lab #5) into overloaded operators as described below. Modify your calc program to utilize the overloaded operators.

Program Description

  1. Convert the four arithmetic functions to overloaded operators (note that all parameters and returns are pass-by-value and that all operators are members of the fraction class) a. friend fraction operator+(fraction f1, fraction f2) b. friend fraction operator-(fraction f1, fraction f2) c. friend fraction operator*(fraction f1, fraction f2) d. friend fraction operator/(fraction f1, fraction f2) e. Friend functions are NOT members of a class. However, they are declared (i.e., prototyped) in the class and the keyword “friend” allows them to access the private data contained in the class.
  2. Replace the print function with operator<< as demonstrated in class (see pp. 616- 620). Make it inline or a regular function as you choose.
  3. Convert the read function to operator>> as demonstrated in class. It may be either inline or a regular function as you choose.
  4. A conversion constructor has one parameter and converts one data type to another (the parameter type to the class-type of the defining class). For example:

fraction(int n) : numerator(n), denominator(1) {}

is a conversion constructor that converts the integer n into the fraction n/1. By using default parameters, it is often possible to make one constructor serve both as a “normal” constructor and as a conversion constructor. You created a conversion constructor in Lab #4:

fraction(int n, int d = 1)...

Page 2 of 2

  1. Assuming that f1 and f2 are appropriately defined fraction objects, then the modifications made above permit statements of the following form:

fraction f3 = f1 + f2; cout << f3 << " = " << f1 << " + " << f2 << endl;

fraction f7 = f2 + 2; cout << f7 << " = " << f2 << " + " << 2 << endl;

The second addition operation (f2 + 2) automatically calls the conversion constructor to convert the 2 into a fraction, which can then be added to the original fraction object.

fraction f8 = 2 + f2; cout << f8 << " = " << 2 << " + " << f2 << endl;

The same conversion takes place with the expression 2 + f2.

  1. You may include a default constructor in your class if needed.

Submitting Your Code

Upload three files to WSU Online: fraction.h , fraction.cpp , and calc.cpp (Note that you must make a new directory to avoid overwriting existing files – instructions are provided on the class web site above the lab assignments). Please do not zip the files.