Using C++ 11Write a program which overloads fractions arithmetic operations for division. And include the overloading operators which we did in the class like multiplication, comparison, insertion, and extraction operators.Here is the class:class NumDays {   int hr; //declaring hour variable to store hours   float days; //declaring days variable to store days   public:    //default constructor    NumDays()   {     hr = 0;     days = 0.0;   };  // parametrized constructor  NumDays(int hrs)   {     hr = hrs;     days = float(hrs / 8.0);   };  // implementing getters method to return hours value from class  int getHour()   {     return hr;   }  // implementing getters method to return no of days from class  float getDays()   {     return days;   }  // method to implement addition operator  NumDays operator + (NumDays obj)   {     int hrs = getHour() + obj.getHour();     NumDays temp(hrs);     return temp;   }  // method to implement subtraction operator  NumDays operator - (NumDays obj)   {     int hrs = getHour() - obj.getHour();     NumDays temp(hrs);     return temp;   }   const NumDays & operator++() //implementing prefix addition operator   {     ++hr;     days = float(hr / 8.0);     return *this;   }   const NumDays & operator--() //implementing prefix subtraction operator   {     --hr;     days = float(hr / 8.0);     return *this;   }   const NumDays operator++(int) //implementing postfix addition operator   {     NumDays temp( * this);     ++hr;     days = float(hr / 8.0);     return temp;   }   const NumDays operator--(int) //implementing postfix subtraction operator   {     NumDays temp( * this);     --hr;     days = float(hr / 8.0);     return temp;   } };

Question
Asked Oct 24, 2019

Using C++ 11

Write a program which overloads fractions arithmetic operations for division. And include the overloading operators which we did in the class like multiplication, comparison, insertion, and extraction operators.

Here is the class:

class NumDays {

 

  int hr; //declaring hour variable to store hours

 

  float days; //declaring days variable to store days

 

  public:

    //default constructor

    NumDays()

 

  {

 

    hr = 0;

 

    days = 0.0;

 

  };

  // parametrized constructor

  NumDays(int hrs)

 

  {

 

    hr = hrs;

 

    days = float(hrs / 8.0);

 

  };

  // implementing getters method to return hours value from class

  int getHour()

 

  {

 

    return hr;

 

  }

  // implementing getters method to return no of days from class

  float getDays()

 

  {

 

    return days;

 

  }

  // method to implement addition operator

  NumDays operator + (NumDays obj)

 

  {

 

    int hrs = getHour() + obj.getHour();

 

    NumDays temp(hrs);

 

    return temp;

 

  }

  // method to implement subtraction operator

  NumDays operator - (NumDays obj)

 

  {

 

    int hrs = getHour() - obj.getHour();

 

    NumDays temp(hrs);

 

    return temp;

 

  }

 

  const NumDays & operator++() //implementing prefix addition operator

 

  {

 

    ++hr;

 

    days = float(hr / 8.0);

 

    return *this;

 

  }

 

  const NumDays & operator--() //implementing prefix subtraction operator

 

  {

 

    --hr;

 

    days = float(hr / 8.0);

 

    return *this;

 

  }

 

  const NumDays operator++(int) //implementing postfix addition operator

 

  {

 

    NumDays temp( * this);

 

    ++hr;

 

    days = float(hr / 8.0);

 

    return temp;

 

  }

 

  const NumDays operator--(int) //implementing postfix subtraction operator

 

  {

 

    NumDays temp( * this);

 

    --hr;

 

    days = float(hr / 8.0);

 

    return temp;

 

  }

 

};

 

check_circleExpert Solution
Step 1

To write a program which overloads fractions arithmetic operation for division add the following main() function in class.

 

//main method

int main()

{

  //object declaration

NumDays object(20),object2(5),object3,object4;

//first object days and hours print

cout<<"Day of first object: "<<object.getDays()<<" and Hour:"<<object.getHour()<<"\n";

//second object days and hours print

cout<<"Day of second object: "<<object2.getDays()<<" and Hour:"<<object2.getHour()<<"\n\n";

//calculated object3 value

object3=object2-object;

//third object days and hours print

cout<<"Day of object3=object2-object: "<<object3.getDays()<<" and Hour:"<<object3.getHour()<<"\n";

object3=object+object2;

//third object days and hours print

cout<<"Day of  object3=object2+object: "<<object3.getDays()<<" and Hour:"<<object3.getHour()<<"\n";

object4=object3++;

//forth object days and hours print

cout<<"Day of  object4=object3++: "<<object4.getDays()<<" and Hour:"<<object4.getHour()<<"\n";

object4=++object3;

//forth object days and hours print

cout<<"Day of object4=++object3: "<<object4.getDays()<<" and Hour:"<<object4.getHour()<<"\n";

object4=object3--;

//forth object days and hours print

cout<<"Day of object4=object3-- :"<<object4.getDays()<<" and Hour:"<<object4.getHour()<<"\n";

object4=--object3;

//forth object days and hours print

cout<<"Day of third object4=--object3: "<<object4.getDays()<<" and Hour:"<<object4.getHour()<<"\n";

 

};

 

Include following header file and namespace in program:

#include<iostream>

using namespace std;

Step 2

Program:

#include<iostream>

using namespace std;

class NumDays {

 

 

 

  int hr; //declaring hour variable to store hours

 

 

 

  float days; //declaring days variable to store days

 

 

 

  public:

 

    //default constructor

 

    NumDays()

 

 

 

  {

 

 

 

    hr = 0;

 

 

 

    days = 0.0;

 

 

 

  };

 

  // parametrized constructor

 

  NumDays(int hrs)

 

 

 

  {

 

 

 

    hr = hrs;

 

 

 

    days = float(hrs / 8.0);

 

 

 

  };

 

  // implementing getters method to return hours value from class

 

  int getHour()

 

 

 

  {

 

 

 

    return hr;

 

 

 

  }

 

  // implementing getters method to return no of days from class

 

  float getDays()

 

 

 

  {

 

 

 

    return days;

 

 

 

  }

 

  // method to implement addition operator

 

  NumDays operator + (NumDays obj)

 

 

 

  {

 

 

 

    int hrs = getHour() + obj.getHour();

 

 

 

    NumDays temp(hrs);

 

 

 

    return temp;

 

 

 

  }

 

  // method to implement subtraction operator

 

  NumDays operator - (NumDays obj)

 

 

 

  {

 

 

 

    int hrs = getHour() - obj.getHour();

 

 

 

    NumDays temp(hrs);

 

 

 

    return temp;

 

 

 

  }

 

 

 

  const NumDays & operator++() //implementing prefix addition operator

 

 

 

  {

 

 

 

    ++hr;

 

 

 

    days = float(hr / 8.0);

 

 

 

    return *this;

 

 

 

  }

 

 

 

  const NumDays & operator--() //implementing prefix subtraction operator

 

 

 

  {

 

 

 

    --hr;

 

 

 

    days = float(hr / 8.0);

 

 

 

    return *this;

 

 

 

  }

 

 

 

  const NumDays operator++(int) //implementing postfix addition operator

 

 

 

  {

 

 

 

    NumDays temp( * this);

 

 

 

    ++hr;

 

 

 

    days = float(hr / 8.0);

 

 

 

    return temp;

 

 

 

  }

 

 

 

  const NumDays operator--(int) //implementing postfix subtraction operator

 

 

 

  {

 

 

&...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour*

See Solution
*Response times may vary by subject and question
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Write a program Gas.java that computes and displays the price a person will pay for gas at the gas s...

A: public class Gas {      public static void main(String[] args)       {             if (args.length &...

question_answer

Q: Consider the sender A transmits 12 data packets to B using stop wait 3rd packet is lost while transm...

A: In stop wait protocol, both the sender and receiver have a windows size of one. The lost packets in ...

question_answer

Q: How do i solve this problem in python? ( has 3 steps)

A: NOTE:The given information is incomplete (Formula to calculate the minutes to walk or run for a give...

question_answer

Q: Why is it important to present accounting and financial information in a manner that is attractive a...

A: It is important to present accounting and financial information in a manner that is attractive and e...

question_answer

Q: Hi, I have to display the first 20 Fibonacci numbers to the console. The first two numbers are given...

A: Program: class PrSpecial { public static final int N = 20; public static void main(String[] args){ i...

question_answer

Q: My C++ program is supposed to check if a string input is in descending order. When I try to run it, ...

A: The given C++ code has few logical errors due to which it is not resulting the correct output. Revis...

question_answer

Q: Hello,  Just want to check if the attached Crow's Foot ERD is correct for the following problem: Gi...

A: The given crow feet representation is not the correct way to draw crow feet ERD. There is only one b...

question_answer

Q: What are the differences between the top-down and bottom-up approaches to information security? Why ...

A: Top-down approach:Top-down approach is the division of system into subsystems which are done to gain...

question_answer

Q: Write a loop that reads positive integers from standard input and that terminates when it reads an i...

A: To check the condition that the number is positive and print the sum, count of even and odd integers...