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: 3702954 • 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

I've seen other solutions here, but none of them utilize all the constructors and functions that are required by my instructor. Here is the code from the last assignment that is gonna be used to help build this one:

double.h

#ifndef DOUBLE

#define DOUBLE

class Double

{

private:

double d;

public:

void equals(const double n);

Double add(const Double &d);

Double sub(const Double &d);

Double mult(const Double &d);

Double div(const Double &d);

double toDouble()const;

void printDouble();

};

#endif

integer.h

#ifndef INTEGER

#define INTEGER

class Integer

{

private:

int i;

public:

void equals(const int n);

Integer add(const Integer &i);

Integer sub(const Integer &i);

Integer mult(const Integer &i);

Integer div(const Integer &i);

int toInt()const;

void printInt();

};

#endif

double.cpp

#include <iostream>

#include "Double.h"

using namespace std;

void Double::equals(const double n)

{

d = n;

}

Double Double::add(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() + d.toDouble());

return tmp;

}

Double Double::sub(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() - d.toDouble());

return tmp;

}

Double Double::mult(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() * d.toDouble());

return tmp;

}

Double Double::div(const Double &d)

{

Double tmp;

tmp.equals(this->toDouble() / d.toDouble());

return tmp;

}

double Double::toDouble()const

{

return this->d;

}

void Double::printDouble()

{

cout << this->d << endl;

}

integer.cpp

#include <iostream>

#include "Integer.h"

using namespace std;

void Integer::equals(const int n)

{

i = n;

}

Integer Integer::add(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() + i.toInt());

return tmp;

}

Integer Integer::sub(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() - i.toInt());

return tmp;

}

Integer Integer::mult(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() * i.toInt());

return tmp;

}

Integer Integer::div(const Integer &i)

{

Integer tmp;

tmp.equals(this->toInt() / i.toInt());

return tmp;

}

int Integer::toInt()const

{

return this->i;

}

void Integer::printInt()

{

cout << this->i << endl;

}

main.cpp

#include <iostream>

#include "Double.h"

#include "Integer.h"

using namespace std;

int main()

{

Double d1;

Double d2,d3,d4,d5,d6;

d1.equals(3.85);

d2.equals(4.4);

d3 = d1.add(d2);

d4 = d1.sub(d2);

d5 = d1.mult(d2);

d6 = d1.div(d2);

d3.printDouble();

d4.printDouble();

d5.printDouble();

d6.printDouble();

Integer i1;

Integer i2, i3, i4, i5,i6;

i1.equals(14);

i2.equals(7);

i3 = i1.add(i2);

i4 = i1.sub(i2);

i5 = i1.mult(i2);

i6 = i1.div(i2);

i3.printInt();

i4.printInt();

i5.printInt();

i6.printInt();

return 0;

}

Explanation / Answer

I will give you two solutions, Choose any of the one which suits to you

Solution1:

#include <iostream>
using namespace std;

class Double
{

   private:
   double num;

   public:
   Double()     //default constructor
   {
       num =0.0;
   }
   Double(double num)   //primitive using constructor
   {
       this->num = num;
   }

   //methods add,sub,mul and div returning object of the class
   Double add(Double d)     
   {
       Double temp;
       temp.num = this->num+d.num;
       return temp;
   }

   Double sub(Double d)
   {
       Double temp;
       temp.num = this->num-d.num;
       return temp;
   }

   Double mul(Double d)
   {
       Double temp;
       temp.num = this->num*d.num;
       return temp;
   }

   Double div(Double d)
   {
       Double temp;
       temp.num = this->num/d.num;
       return temp;
   }
   double getNum() //get method to return num
   {
       return num;
   }

};
class Integer
{
   private:
   int num;

   public:
   Integer()          //default constructor
   {
       num =0;
   }
   Integer(int num)   //constructor using primitive type
   {
       this->num = num;
   }
   //add,sub,div and mul arithmetic operations returning object of the class
   Integer add(Integer i)
   {
       Integer temp;
       temp.num = this->num+i.num;
       return temp;
   }
   Integer sub(Integer i)
   {
       Integer temp;
       temp.num = this->num-i.num;
       return temp;
   }
   Integer mul(Integer i)
   {
       Integer temp;
       temp.num = this->num*i.num;
       return temp;
   }
   Integer div(Integer i)
   {
       Integer temp;
       temp.num = this->num/i.num;
       return temp;
   }
   int getNum()
   {
       return num;
   }

};

int main()
{
Double d(12.50), d3;
Double d2(1.5);

cout<<"Double Arithmetic"<<endl;
cout<<"d : "<<d.getNum()<<endl;
cout<<"d2 : "<<d2.getNum()<<endl;

d3 = d.add(d2);

cout<<"d+d2 : "<<d3.getNum()<<endl;

d3 = d.sub(d2);

cout<<"d-d2 : "<<d3.getNum()<<endl;

d3 = d.mul(d2);

cout<<"d*d2 : "<<d3.getNum()<<endl;

d3 = d.div(d2);

cout<<"d/d2 : "<<d3.getNum()<<endl;


Integer i(12), i3;
Integer i2(2);

cout<<"Integer Arithmetic"<<endl;
cout<<"i : "<<i.getNum()<<endl;
cout<<"i2 : "<<i2.getNum()<<endl;

i3 = i.add(i2);

cout<<"i+i2 : "<<i3.getNum()<<endl;

i3 = i.sub(i2);

cout<<"i-i2: "<<i3.getNum()<<endl;

i3 = i.mul(i2);

cout<<"i*i2: "<<i3.getNum()<<endl;

i3 = i.div(i2);

cout<<"i/i2: "<<i3.getNum()<<endl;


   return 0;
}

output:

Double Arithmetic
d : 12.5
d2 : 1.5
d+d2 : 14
d-d2 : 11
d*d2 : 18.75
d/d2 : 8.33333
Integer Arithmetic
i : 12
i2 : 2
i+i2 : 14
i-i2: 10
i*i2: 24
i/i2: 6

Solution2:

#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