Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For your Double and Integer classes add a default constructor that sets the valu

ID: 3879561 • Letter: F

Question

For your Double and Integer classes add a default constructor that sets the value to 0.0 and 0 respectively. Then add the following overloaded constructors

Double class

A Double argument

A primitive double

An Integer class

Integer class

An Integer class

A primitive int

Each of these constructors should set the data section of the class to the value being passed to it.

In addition to the overloaded constructors add the following overloaded functions

add

sub

mul

div

Each of these should take primitive values and return the base class type. For instance, a Double class should be like this:

Double d(12.50), d3;

d3 = d.add(1.5);

In this case d3 should now have a value of 14.00

Explanation / Answer

#include<iostream>
using namespace std;
class Integer
{
   public:
   int d;
   public:
       //constructors
   Integer()
   {
       d=0;  
   }  
   Integer(int dd)
   {
       d=dd;  
   }
   //methods;
   Integer add(int dd)
   {
       d=d+dd;
       Integer r(d);
       return r;
      
   }
   Integer sub(int dd)
   {
       d=d-dd;
       Integer r(d);
       return r;
      
   }
   Integer mul(int dd)
   {
       d=d*dd;
       Integer r(d);
       return r;
      
   }
   Integer div(int dd)
   {
       d=d/dd;
       Integer r(d);
       return r;
      
   }
};
class Double
{
   public:
   double d;
   public:
       //constructors
   Double()
   {
       d=0;  
   }  
   Double(double dd)
   {
       d=dd;  
   }
   //methods;
   Double add(double dd)
   {
       d=d+dd;
       Double r(d);
       return r;
      
   }
   Double sub(double dd)
   {
       d=d-dd;
       Double r(d);
       return r;
      
   }
   Double mul(double dd)
   {
       d=d*dd;
       Double r(d);
       return r;
      
   }
   Double div(double dd)
   {
       d=d/dd;
       Double r(d);
       return r;
      
   }
};

int main()
{
  
   Double d(12.50),d3;
   d3 = d.add(1.5);
   cout<<d3.d<<endl;
  
   return 0;
}

output:

14


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote