SlideShare a Scribd company logo
1 of 10
Download to read offline
write the To Do's to get the exact output:
NOte: A valid Fraction number has a non-negative numerator and a positive denominator.
Default constructor initializes the object to safe empty state (an object with denom equals -1).
The two-argument constructor also validates the parameters and sets the object to the safe empty
state if the parameters are not valid.
Write the definitions and prototypes of following functions in Fraction.cpp and Fraction.h
respectively (They are indicated in the files by //TODO tag):
Define isEmpty function as a member function, which returns true if the object is in safe empty
state (an object is in the safe empty state if denominator (denom) equals -1).
Define display function, which sends a Fraction number to the output stream (with the
“Numerator/denominator” format). This function just prints "Invalid Fraction Object!" in the
screen if the object is in the safe empty state. In case that object denominator equals 1, it just
print the numerator.
Define the operator functions for the following operators:
“+=”, “+”, “*”
The overload of the above operators should make the following code possible:
The member operator+ : Adds two Fraction numbers and returns a Fraction number as the result.
This function returns an object with the safe empty state if either of Fraction numbers (operands)
is in safe empty state. It makes following code possible:
A+B ( where A and B are Fraction objects)
The member operator+= : Adds two Fraction numbers and assigns the result to the left operand,
then returns a reference to the left operand. If either of Fraction numbers (operands) is in safe
empty state, it initializes the left operand to the safe empty state, then returns a reference to the
left operand. It makes following code possible:
A+=B ( where A and B are Fraction objects)
The member operator* : Multiplies two Fraction numbers and returns a Fraction number as the
result. This function returns an object with the safe empty state if either of Fraction numbers
(operands) is in safe empty state. It makes following code possible:
A*B ( where A and B are Fraction objects)
fraction.cpp
#include "Fraction.h"
using namespace std;
namespace sict{
Fraction::Fraction(){
denom =-1; // safe empty state
}
Fraction::Fraction(int n, int d) // n: numerator, d: denominator
{
if(n >= 0 && d > 0){
num = n;
denom = d;
reduce();
}
else
denom =-1; // set to safe empty state
}
int Fraction::gcd() // returns the greatest common divisor of num and
denom
{
int mn = min(); // min of num and denom
int mx = max(); // mX of num and denom
for (int x=mn ; x > 0 ; x--) // find the greatest common divisor
if( mx % x == 0 && mn % x == 0)
return x;
return 1;
}
void Fraction::reduce() // simplify the Fraction number
{
int tmp = gcd();
num /= tmp;
denom /= tmp;
}
int Fraction::max ()
{
return (num >= denom) ? num : denom;
}
int Fraction::min()
{
return (num >= denom) ? denom : num;
}
// in_lab
// TODO: write the implementation of display function HERE
// TODO: write the implementation of isEmpty function HERE
// TODO: write the implementation of member operator+= function HERE
// TODO: write the implementation of member operator+ function HERE
// TODO: write the implementation of member operator* function HERE
}
fraction.h
#ifndef SICT_Fraction_H__
#define SICT_Fraction_H__
#include
using namespace std;
namespace sict{
class Fraction{
private:
int num; // Numerator
int denom; // Denominator
int gcd(); // returns the greatest common divisor of num and denom
int max(); // returns the maximum of num and denom
int min(); // returns the minimum of num and denom
public:
void reduce(); // simplifies a Fraction number by dividing the
// numerator and denominator to their greatest common divisor
Fraction(); // default constructor
Fraction(int n , int d=1); // construct n/d as a Fraction number
void display() const;
bool isEmpty() const;
// member operator functions
// TODO: write the prototype of member operator+= function HERE
// TODO: write the prototype of member operator+ function HERE
// TODO: write the prototype of member operator* function HERE
};
};
#endif
main.cpp
include
#include "Fraction.h"
using namespace sict;
using namespace std;
int main(){
cout << "------------------------------" << endl;
cout << "Fraction Class Operators Test:" << endl;
cout << "------------------------------" << endl;
Fraction A;
cout << "Fraction A; // ";
cout << "A = ";
A.display();
cout << endl;
Fraction B(1, 3);
cout << "Fraction B(1, 3); // ";
cout << "B = ";
B.display();
cout << endl;
Fraction C(-5, 15);
cout << "Fraction C(-5, 15); //";
cout << " C = " ;
C.display();
cout << endl;
Fraction D(2, 4);
cout << "Fraction D(2, 4); //";
cout << " D = ";
D.display();
cout << endl;
Fraction E(8, 4);
cout << "Fraction E(8, 4); //";
cout << " E = " ;
E.display();
cout << endl;
cout << endl;
cout << "A+B equals ";
(A+B).display();
cout << endl;
cout << "B+3 equals ";
(B+3).display();
cout << endl;
cout << "B+D equals ";
(B+D).display();
cout << endl;
cout << "(A = D = (B+=E)) equals " ;
(A = D = (B+=E)).display();
cout << endl;
cout << "Now A, D, B, and E equal " ;
A.display();
cout << ", " ;
D.display();
cout << ", ";
B.display();
cout << ", ";
E.display();
cout << endl;
return 0;
}
Output Example:
(Your output should exactly match the following)
------------------------------
Fraction Class Operators Test:
------------------------------
Fraction A; // A = Invalid Fraction Object!
Fraction B(1, 3); // B = 1/3
Fraction C(-5, 15); // C = Invalid Fraction Object!
Fraction D(2, 4); // D = 1/2
Fraction E(8, 4); // E = 2
A+B equals Invalid Fraction Object!
B+3 equals 10/3
B+D equals 5/6
(A = D = (B+=E)) equals 7/3
Now A, D, B, and E equal 7/3, 7/3, 7/3, 2
Solution
#include "Fraction.h"
using namespace std;
namespace sict{
Fraction::Fraction(){
denom =-1; // safe empty state
}
Fraction::Fraction(int n, int d) // n: numerator, d: denominator
{
if(n >= 0 && d > 0){
num = n;
denom = d;
reduce();
}
else
denom =-1; // set to safe empty state
}
int Fraction::gcd() // returns the greatest common divisor of num and
denom
{
int mn = min(); // min of num and denom
int mx = max(); // mX of num and denom
for (int x=mn ; x > 0 ; x--) // find the greatest common divisor
if( mx % x == 0 && mn % x == 0)
return x;
return 1;
}
void Fraction::reduce() // simplify the Fraction number
{
int tmp = gcd();
num /= tmp;
denom /= tmp;
}
int Fraction::max ()
{
return (num >= denom) ? num : denom;
}
int Fraction::min()
{
return (num >= denom) ? denom : num;
}
// in_lab
// TODO: write the implementation of display function HERE
// TODO: write the implementation of isEmpty function HERE
// TODO: write the implementation of member operator+= function HERE
// TODO: write the implementation of member operator+ function HERE
// TODO: write the implementation of member operator* function HERE
}
fraction.h
#ifndef SICT_Fraction_H__
#define SICT_Fraction_H__
#include
using namespace std;
namespace sict{
class Fraction{
private:
int num; // Numerator
int denom; // Denominator
int gcd(); // returns the greatest common divisor of num and denom
int max(); // returns the maximum of num and denom
int min(); // returns the minimum of num and denom
public:
void reduce(); // simplifies a Fraction number by dividing the
// numerator and denominator to their greatest common divisor
Fraction(); // default constructor
Fraction(int n , int d=1); // construct n/d as a Fraction number
void display() const;
bool isEmpty() const;
// member operator functions
// TODO: write the prototype of member operator+= function HERE
// TODO: write the prototype of member operator+ function HERE
// TODO: write the prototype of member operator* function HERE
};
};
#endif
main.cpp
include
#include "Fraction.h"
using namespace sict;
using namespace std;
int main(){
cout << "------------------------------" << endl;
cout << "Fraction Class Operators Test:" << endl;
cout << "------------------------------" << endl;
Fraction A;
cout << "Fraction A; // ";
cout << "A = ";
A.display();
cout << endl;
Fraction B(1, 3);
cout << "Fraction B(1, 3); // ";
cout << "B = ";
B.display();
cout << endl;
Fraction C(-5, 15);
cout << "Fraction C(-5, 15); //";
cout << " C = " ;
C.display();
cout << endl;
Fraction D(2, 4);
cout << "Fraction D(2, 4); //";
cout << " D = ";
D.display();
cout << endl;
Fraction E(8, 4);
cout << "Fraction E(8, 4); //";
cout << " E = " ;
E.display();
cout << endl;
cout << endl;
cout << "A+B equals ";
(A+B).display();
cout << endl;
cout << "B+3 equals ";
(B+3).display();
cout << endl;
cout << "B+D equals ";
(B+D).display();
cout << endl;
cout << "(A = D = (B+=E)) equals " ;
(A = D = (B+=E)).display();
cout << endl;
cout << "Now A, D, B, and E equal " ;
A.display();
cout << ", " ;
D.display();
cout << ", ";
B.display();
cout << ", ";
E.display();
cout << endl;
return 0;
}

More Related Content

Similar to write the To Dos to get the exact outputNOte A valid Fraction .pdf

The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf4babies2010
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfmallik3000
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,pptAllNewTeach
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...kushwahashivam413
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...bhargavi804095
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfravikapoorindia
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introductionraghukatagall2
 
When we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfWhen we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfarihantkitchenmart
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
can someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfcan someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfvinaythemodel
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 

Similar to write the To Dos to get the exact outputNOte A valid Fraction .pdf (20)

The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
When we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfWhen we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdf
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
7 functions
7  functions7  functions
7 functions
 
can someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfcan someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdf
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
03-fortran.ppt
03-fortran.ppt03-fortran.ppt
03-fortran.ppt
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 

More from jyothimuppasani1

How does an open source operating system like Linux® affect Internet.pdf
How does an open source operating system like Linux® affect Internet.pdfHow does an open source operating system like Linux® affect Internet.pdf
How does an open source operating system like Linux® affect Internet.pdfjyothimuppasani1
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfjyothimuppasani1
 
how can I replace the Newsfeed text with a custom on in SharePoi.pdf
how can I replace the Newsfeed text with a custom on in SharePoi.pdfhow can I replace the Newsfeed text with a custom on in SharePoi.pdf
how can I replace the Newsfeed text with a custom on in SharePoi.pdfjyothimuppasani1
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfHello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfjyothimuppasani1
 
For each of the following reseach questions identify the observation.pdf
For each of the following reseach questions identify the observation.pdfFor each of the following reseach questions identify the observation.pdf
For each of the following reseach questions identify the observation.pdfjyothimuppasani1
 
Find an Eulerian trail in the following graph. Be sure to indicate th.pdf
Find an Eulerian trail in the following graph. Be sure to indicate th.pdfFind an Eulerian trail in the following graph. Be sure to indicate th.pdf
Find an Eulerian trail in the following graph. Be sure to indicate th.pdfjyothimuppasani1
 
File encryption. [32] Write a program which accepts a filename as a .pdf
File encryption. [32] Write a program which accepts a filename as a .pdfFile encryption. [32] Write a program which accepts a filename as a .pdf
File encryption. [32] Write a program which accepts a filename as a .pdfjyothimuppasani1
 
Explain the relevance that medical standards of practice have to one.pdf
Explain the relevance that medical standards of practice have to one.pdfExplain the relevance that medical standards of practice have to one.pdf
Explain the relevance that medical standards of practice have to one.pdfjyothimuppasani1
 
Find the mission and values statements for four different hospita.pdf
Find the mission and values statements for four different hospita.pdfFind the mission and values statements for four different hospita.pdf
Find the mission and values statements for four different hospita.pdfjyothimuppasani1
 
Did colonial rule freeze colonized societies by preserving old s.pdf
Did colonial rule freeze colonized societies by preserving old s.pdfDid colonial rule freeze colonized societies by preserving old s.pdf
Did colonial rule freeze colonized societies by preserving old s.pdfjyothimuppasani1
 
Do you think that nonhuman animals have interests Does this mean th.pdf
Do you think that nonhuman animals have interests Does this mean th.pdfDo you think that nonhuman animals have interests Does this mean th.pdf
Do you think that nonhuman animals have interests Does this mean th.pdfjyothimuppasani1
 
Discuss the importance of recognizing and implementing different ent.pdf
Discuss the importance of recognizing and implementing different ent.pdfDiscuss the importance of recognizing and implementing different ent.pdf
Discuss the importance of recognizing and implementing different ent.pdfjyothimuppasani1
 
Considering the challenges she is facing, what Anitas plan before .pdf
Considering the challenges she is facing, what Anitas plan before .pdfConsidering the challenges she is facing, what Anitas plan before .pdf
Considering the challenges she is facing, what Anitas plan before .pdfjyothimuppasani1
 
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdfData Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdfjyothimuppasani1
 
Conceptual skills are most important at theSolutionConceptual .pdf
Conceptual skills are most important at theSolutionConceptual .pdfConceptual skills are most important at theSolutionConceptual .pdf
Conceptual skills are most important at theSolutionConceptual .pdfjyothimuppasani1
 
A variable whose scope is restricted to the method where it was decl.pdf
A variable whose scope is restricted to the method where it was decl.pdfA variable whose scope is restricted to the method where it was decl.pdf
A variable whose scope is restricted to the method where it was decl.pdfjyothimuppasani1
 
•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdf•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdfjyothimuppasani1
 
You are to write a GUI program that will allow a user to buy, sell a.pdf
You are to write a GUI program that will allow a user to buy, sell a.pdfYou are to write a GUI program that will allow a user to buy, sell a.pdf
You are to write a GUI program that will allow a user to buy, sell a.pdfjyothimuppasani1
 
What are the factors that contribute to H+ gain or loss How do lung.pdf
What are the factors that contribute to H+ gain or loss How do lung.pdfWhat are the factors that contribute to H+ gain or loss How do lung.pdf
What are the factors that contribute to H+ gain or loss How do lung.pdfjyothimuppasani1
 
Write down the four (4) nonlinear regression models covered in class,.pdf
Write down the four (4) nonlinear regression models covered in class,.pdfWrite down the four (4) nonlinear regression models covered in class,.pdf
Write down the four (4) nonlinear regression models covered in class,.pdfjyothimuppasani1
 

More from jyothimuppasani1 (20)

How does an open source operating system like Linux® affect Internet.pdf
How does an open source operating system like Linux® affect Internet.pdfHow does an open source operating system like Linux® affect Internet.pdf
How does an open source operating system like Linux® affect Internet.pdf
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdf
 
how can I replace the Newsfeed text with a custom on in SharePoi.pdf
how can I replace the Newsfeed text with a custom on in SharePoi.pdfhow can I replace the Newsfeed text with a custom on in SharePoi.pdf
how can I replace the Newsfeed text with a custom on in SharePoi.pdf
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfHello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdf
 
For each of the following reseach questions identify the observation.pdf
For each of the following reseach questions identify the observation.pdfFor each of the following reseach questions identify the observation.pdf
For each of the following reseach questions identify the observation.pdf
 
Find an Eulerian trail in the following graph. Be sure to indicate th.pdf
Find an Eulerian trail in the following graph. Be sure to indicate th.pdfFind an Eulerian trail in the following graph. Be sure to indicate th.pdf
Find an Eulerian trail in the following graph. Be sure to indicate th.pdf
 
File encryption. [32] Write a program which accepts a filename as a .pdf
File encryption. [32] Write a program which accepts a filename as a .pdfFile encryption. [32] Write a program which accepts a filename as a .pdf
File encryption. [32] Write a program which accepts a filename as a .pdf
 
Explain the relevance that medical standards of practice have to one.pdf
Explain the relevance that medical standards of practice have to one.pdfExplain the relevance that medical standards of practice have to one.pdf
Explain the relevance that medical standards of practice have to one.pdf
 
Find the mission and values statements for four different hospita.pdf
Find the mission and values statements for four different hospita.pdfFind the mission and values statements for four different hospita.pdf
Find the mission and values statements for four different hospita.pdf
 
Did colonial rule freeze colonized societies by preserving old s.pdf
Did colonial rule freeze colonized societies by preserving old s.pdfDid colonial rule freeze colonized societies by preserving old s.pdf
Did colonial rule freeze colonized societies by preserving old s.pdf
 
Do you think that nonhuman animals have interests Does this mean th.pdf
Do you think that nonhuman animals have interests Does this mean th.pdfDo you think that nonhuman animals have interests Does this mean th.pdf
Do you think that nonhuman animals have interests Does this mean th.pdf
 
Discuss the importance of recognizing and implementing different ent.pdf
Discuss the importance of recognizing and implementing different ent.pdfDiscuss the importance of recognizing and implementing different ent.pdf
Discuss the importance of recognizing and implementing different ent.pdf
 
Considering the challenges she is facing, what Anitas plan before .pdf
Considering the challenges she is facing, what Anitas plan before .pdfConsidering the challenges she is facing, what Anitas plan before .pdf
Considering the challenges she is facing, what Anitas plan before .pdf
 
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdfData Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
 
Conceptual skills are most important at theSolutionConceptual .pdf
Conceptual skills are most important at theSolutionConceptual .pdfConceptual skills are most important at theSolutionConceptual .pdf
Conceptual skills are most important at theSolutionConceptual .pdf
 
A variable whose scope is restricted to the method where it was decl.pdf
A variable whose scope is restricted to the method where it was decl.pdfA variable whose scope is restricted to the method where it was decl.pdf
A variable whose scope is restricted to the method where it was decl.pdf
 
•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdf•Design (create) 3 questions for a quiz show game and design regular.pdf
•Design (create) 3 questions for a quiz show game and design regular.pdf
 
You are to write a GUI program that will allow a user to buy, sell a.pdf
You are to write a GUI program that will allow a user to buy, sell a.pdfYou are to write a GUI program that will allow a user to buy, sell a.pdf
You are to write a GUI program that will allow a user to buy, sell a.pdf
 
What are the factors that contribute to H+ gain or loss How do lung.pdf
What are the factors that contribute to H+ gain or loss How do lung.pdfWhat are the factors that contribute to H+ gain or loss How do lung.pdf
What are the factors that contribute to H+ gain or loss How do lung.pdf
 
Write down the four (4) nonlinear regression models covered in class,.pdf
Write down the four (4) nonlinear regression models covered in class,.pdfWrite down the four (4) nonlinear regression models covered in class,.pdf
Write down the four (4) nonlinear regression models covered in class,.pdf
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

write the To Dos to get the exact outputNOte A valid Fraction .pdf

  • 1. write the To Do's to get the exact output: NOte: A valid Fraction number has a non-negative numerator and a positive denominator. Default constructor initializes the object to safe empty state (an object with denom equals -1). The two-argument constructor also validates the parameters and sets the object to the safe empty state if the parameters are not valid. Write the definitions and prototypes of following functions in Fraction.cpp and Fraction.h respectively (They are indicated in the files by //TODO tag): Define isEmpty function as a member function, which returns true if the object is in safe empty state (an object is in the safe empty state if denominator (denom) equals -1). Define display function, which sends a Fraction number to the output stream (with the “Numerator/denominator” format). This function just prints "Invalid Fraction Object!" in the screen if the object is in the safe empty state. In case that object denominator equals 1, it just print the numerator. Define the operator functions for the following operators: “+=”, “+”, “*” The overload of the above operators should make the following code possible: The member operator+ : Adds two Fraction numbers and returns a Fraction number as the result. This function returns an object with the safe empty state if either of Fraction numbers (operands) is in safe empty state. It makes following code possible: A+B ( where A and B are Fraction objects) The member operator+= : Adds two Fraction numbers and assigns the result to the left operand, then returns a reference to the left operand. If either of Fraction numbers (operands) is in safe empty state, it initializes the left operand to the safe empty state, then returns a reference to the left operand. It makes following code possible: A+=B ( where A and B are Fraction objects) The member operator* : Multiplies two Fraction numbers and returns a Fraction number as the result. This function returns an object with the safe empty state if either of Fraction numbers (operands) is in safe empty state. It makes following code possible: A*B ( where A and B are Fraction objects) fraction.cpp #include "Fraction.h" using namespace std; namespace sict{ Fraction::Fraction(){ denom =-1; // safe empty state
  • 2. } Fraction::Fraction(int n, int d) // n: numerator, d: denominator { if(n >= 0 && d > 0){ num = n; denom = d; reduce(); } else denom =-1; // set to safe empty state } int Fraction::gcd() // returns the greatest common divisor of num and denom { int mn = min(); // min of num and denom int mx = max(); // mX of num and denom for (int x=mn ; x > 0 ; x--) // find the greatest common divisor if( mx % x == 0 && mn % x == 0) return x; return 1; } void Fraction::reduce() // simplify the Fraction number { int tmp = gcd(); num /= tmp; denom /= tmp; } int Fraction::max () { return (num >= denom) ? num : denom; } int Fraction::min() { return (num >= denom) ? denom : num; }
  • 3. // in_lab // TODO: write the implementation of display function HERE // TODO: write the implementation of isEmpty function HERE // TODO: write the implementation of member operator+= function HERE // TODO: write the implementation of member operator+ function HERE // TODO: write the implementation of member operator* function HERE } fraction.h #ifndef SICT_Fraction_H__ #define SICT_Fraction_H__ #include using namespace std; namespace sict{ class Fraction{ private: int num; // Numerator int denom; // Denominator int gcd(); // returns the greatest common divisor of num and denom int max(); // returns the maximum of num and denom int min(); // returns the minimum of num and denom public: void reduce(); // simplifies a Fraction number by dividing the // numerator and denominator to their greatest common divisor Fraction(); // default constructor Fraction(int n , int d=1); // construct n/d as a Fraction number void display() const; bool isEmpty() const; // member operator functions
  • 4. // TODO: write the prototype of member operator+= function HERE // TODO: write the prototype of member operator+ function HERE // TODO: write the prototype of member operator* function HERE }; }; #endif main.cpp include #include "Fraction.h" using namespace sict; using namespace std; int main(){ cout << "------------------------------" << endl; cout << "Fraction Class Operators Test:" << endl; cout << "------------------------------" << endl; Fraction A; cout << "Fraction A; // "; cout << "A = "; A.display(); cout << endl; Fraction B(1, 3); cout << "Fraction B(1, 3); // "; cout << "B = "; B.display(); cout << endl; Fraction C(-5, 15); cout << "Fraction C(-5, 15); //"; cout << " C = " ; C.display(); cout << endl; Fraction D(2, 4); cout << "Fraction D(2, 4); //"; cout << " D = ";
  • 5. D.display(); cout << endl; Fraction E(8, 4); cout << "Fraction E(8, 4); //"; cout << " E = " ; E.display(); cout << endl; cout << endl; cout << "A+B equals "; (A+B).display(); cout << endl; cout << "B+3 equals "; (B+3).display(); cout << endl; cout << "B+D equals "; (B+D).display(); cout << endl; cout << "(A = D = (B+=E)) equals " ; (A = D = (B+=E)).display(); cout << endl; cout << "Now A, D, B, and E equal " ; A.display(); cout << ", " ; D.display(); cout << ", "; B.display(); cout << ", "; E.display(); cout << endl; return 0; } Output Example: (Your output should exactly match the following) ------------------------------
  • 6. Fraction Class Operators Test: ------------------------------ Fraction A; // A = Invalid Fraction Object! Fraction B(1, 3); // B = 1/3 Fraction C(-5, 15); // C = Invalid Fraction Object! Fraction D(2, 4); // D = 1/2 Fraction E(8, 4); // E = 2 A+B equals Invalid Fraction Object! B+3 equals 10/3 B+D equals 5/6 (A = D = (B+=E)) equals 7/3 Now A, D, B, and E equal 7/3, 7/3, 7/3, 2 Solution #include "Fraction.h" using namespace std; namespace sict{ Fraction::Fraction(){ denom =-1; // safe empty state } Fraction::Fraction(int n, int d) // n: numerator, d: denominator { if(n >= 0 && d > 0){ num = n; denom = d; reduce(); } else denom =-1; // set to safe empty state } int Fraction::gcd() // returns the greatest common divisor of num and denom { int mn = min(); // min of num and denom
  • 7. int mx = max(); // mX of num and denom for (int x=mn ; x > 0 ; x--) // find the greatest common divisor if( mx % x == 0 && mn % x == 0) return x; return 1; } void Fraction::reduce() // simplify the Fraction number { int tmp = gcd(); num /= tmp; denom /= tmp; } int Fraction::max () { return (num >= denom) ? num : denom; } int Fraction::min() { return (num >= denom) ? denom : num; } // in_lab // TODO: write the implementation of display function HERE // TODO: write the implementation of isEmpty function HERE // TODO: write the implementation of member operator+= function HERE // TODO: write the implementation of member operator+ function HERE // TODO: write the implementation of member operator* function HERE } fraction.h #ifndef SICT_Fraction_H__ #define SICT_Fraction_H__ #include using namespace std;
  • 8. namespace sict{ class Fraction{ private: int num; // Numerator int denom; // Denominator int gcd(); // returns the greatest common divisor of num and denom int max(); // returns the maximum of num and denom int min(); // returns the minimum of num and denom public: void reduce(); // simplifies a Fraction number by dividing the // numerator and denominator to their greatest common divisor Fraction(); // default constructor Fraction(int n , int d=1); // construct n/d as a Fraction number void display() const; bool isEmpty() const; // member operator functions // TODO: write the prototype of member operator+= function HERE // TODO: write the prototype of member operator+ function HERE // TODO: write the prototype of member operator* function HERE }; }; #endif main.cpp include #include "Fraction.h" using namespace sict; using namespace std; int main(){ cout << "------------------------------" << endl; cout << "Fraction Class Operators Test:" << endl; cout << "------------------------------" << endl;
  • 9. Fraction A; cout << "Fraction A; // "; cout << "A = "; A.display(); cout << endl; Fraction B(1, 3); cout << "Fraction B(1, 3); // "; cout << "B = "; B.display(); cout << endl; Fraction C(-5, 15); cout << "Fraction C(-5, 15); //"; cout << " C = " ; C.display(); cout << endl; Fraction D(2, 4); cout << "Fraction D(2, 4); //"; cout << " D = "; D.display(); cout << endl; Fraction E(8, 4); cout << "Fraction E(8, 4); //"; cout << " E = " ; E.display(); cout << endl; cout << endl; cout << "A+B equals "; (A+B).display(); cout << endl; cout << "B+3 equals "; (B+3).display(); cout << endl; cout << "B+D equals ";
  • 10. (B+D).display(); cout << endl; cout << "(A = D = (B+=E)) equals " ; (A = D = (B+=E)).display(); cout << endl; cout << "Now A, D, B, and E equal " ; A.display(); cout << ", " ; D.display(); cout << ", "; B.display(); cout << ", "; E.display(); cout << endl; return 0; }