C For your Double and Integer classes add a default construc

C++:

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

Solution

#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:

C++: 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
C++: 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
C++: 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
C++: 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site