Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use a template so the class works with any kind of number (i.e. int, float, double) fractionType num1(5, 6); fractionType num1(5.1, 6.2); Tip: get your

image text in transcribedimage text in transcribed

Use a template so the class works with any kind of number (i.e. int, float, double)

fractionType num1(5, 6);

fractionType num1(5.1, 6.2);

Tip: get your class working with integer values first for numerator and denominator and convert to a template after it is working with ints. When converting to a template you may need to move all of your implementations into the .h header file. Instead of creating function definitions in the header and putting the implementations in the .cpp file, you wont even have a .cpp. File. Just put the function implementation inside the header file - no need for a function definition if you do this.

i.e. Typical definition in the .h file

bool operator

resulting implementation in the .cpp file

bool fractionType::operator

{

return(numerator / denominator

}

Simply eliminate the definition in the .h and replace it with the function itself.

i.e. class definition

template class T>

class fractionType

your private class variables......

private:

T numerator; //variable to store the numerator

T denominator; //variable to store the denominator

and an example ...

bool operator rightFr) const {

return (numerator * rightFr.denominator

denominator * rightFr.numerator);

}

IMPORTANT: The above are just samples of a portion of the fraction class - you are required to add templating to the entire fraction class.

Below is a template of the class given to you with all of the functions empty - you need to make sure your fraction class implements all of the folloeing:

#ifndef H_fraction

#define H_fraction

#include

using namespace std;

template class T>

class fractionType

{

// overload stream insertion and extraction operators

friend ostream& operatorconst fractionType& fraction) {

}

friend istream& operator>> (istream& is, fractionType& fraction) {

}

public:

//Constructor

fractionType(T num = 0, T den = 1) {

}

//overload +

fractionType operator+(fractionType rightFr) {

}

//overload *

fractionType operator*(fractionType rightFr) {

}

//overload -

fractionType operator-(fractionType rightFr) {

}

//overload /

fractionType operator/(fractionType rightFr) {

}

//overload relational operators

bool operator==(fractionType rightFr) const {

}

bool operator!=(fractionType rightFr) const {

}

bool operator rightFr) const {

}

bool operator rightFr) const {

}

bool operator>=(fractionType rightFr) const {

}

bool operator>(fractionType rightFr) const {

}

private:

T numerator; //variable to store the numerator

T denominator; //variable to store the denominator

};

#endif

10. Rational fractions are of the form a/b, in which a and b are integers and b=0. In this exercise, by "fractions" we mean rational fractions. Suppose a/b and c/d are fractions. Arithmetic operations on fractions are defined by the following rules: a/b+c/d=(ad+bc)/bda/bc/d=(adbc)/bda/bc/d=ac/bd(a/b)/(c/d)=ad/bc;inwhichc/d=0. Fractions are compared as follows: a/bopc/d if adopbc, in which op is any of the relational operations. For example, a/bx; Details should store 2/3 in x. The statement Write a C++ program that, using the class fractionType, performs operations on fractions. Among other things, test the following: Suppose x,y, and z are objects of type fractionType. If the input is 2/3, the statement cinx; Details Program code. In the code, the words in the variable names are merged. Line 1:c in >>x, semi colon. should store 2/3 in x. The statement coutx+yendl; Details Program code. In the code, the words in the variable names are merged. Line 1: c out

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming Languages 12th International Symposium Dbpl 2009 Lyon France August 2009 Proceedings Lncs 5708

Authors: Philippa Gardner ,Floris Geerts

2009th Edition

3642037925, 978-3642037924

More Books

Students also viewed these Databases questions

Question

Distinguish between hearing and listening.

Answered: 1 week ago

Question

Use your voice effectively.

Answered: 1 week ago