C++ Classes Experiment The following has the name MyRational.h: #ifndef MyRational #define MyRational #include using namespace std; class MyRational { int x; int y; public:
C++ Classes Experiment The following has the name MyRational.h: #ifndef MyRational #define MyRational #includeusing namespace std; class MyRational { int x; int y; public: MyRational(int p=1,int pd=1){x = p; y = pd;} // ok void setX(int); // ok void setY(int); // ok int getX() const; // ok int getY() const; // ok // Add member function: adds the MyRational parameter to this instance // Formula: a/b + c/d = (a*d + b*c)/(b*d) MyRational& Add(const MyRational&); // Subtract member function: subtracts the MyRational parameter from this instance // Formula: a/b - c/d = (a*d - b*c)/(b*d) MyRational& Subtract(const MyRational&); // Multiply member function: multiplies the MyRational parameter with this instance // Formula: a/b * c/d = (a*c)/(b*d) MyRational& Multiply(const MyRational&); void print() { cout<<"("<
The following has the name Main.cpp:
#include#include "MyRational.h" using namespace std; int main() { MyRational a(1,3); MyRational b(1,2); MyRational c; c.Add(a).Multiply(b); b.Add(c); a.Add(b); a.print(); b.print(); c.print(); } What it wants is:
Write a Rational class, MyRational, the interface of which is provided to you in MyRational.h file. The member functions are explained with comments. Please write MyRational.cpp file which contains the implementations of the member functions that are not defined inline in the header file.
Test your code with the given sample main function (in Main.cpp file). Make sure that your implementation gives the same output with the sample output provided below.
Sample Output: Output of the sample main is given below.
(36, 36)
(8, 12)
(1, 6)
Compilation:
>g++ Main.cpp MyRational.cpp o experiment
Then, you can run your executable using:
>./experiment
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started