Question

In: Computer Science

The code must be under c++ program. For your Double and Integer classes add a default...

The code must be under c++ program.

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

In your main function write code to test instance of your classes.

Main.cpp

#include <iostream>
#include <cstdlib>
#include <string>
#include "double.h"
#include "integer.h"

using namespace std;

int main()
{
   Double d;
   d.equals(12.34);
   cout << "The Double is : " << d.toDouble() << endl;

   Integer i;
   i.equals(10);
   cout << endl;
   cout << "The Integer is : " << i.toInt() << endl;

   return 0;
}

Double.h

#ifndef DOUBLE
#define DOUBLE

class Double
{
private:
   double data;

public:
   void equals(double d);
   Double add(const Double &d);
   Double sub(const Double &d);
   Double mul(const Double &d);
   Double div(const Double &d);
   double toDouble() const;

};

#endif

Double.cpp

#include <iostream>
#include <string>
#include "Double.h"

using namespace std;

void Double::equals(double d)
{
   data = d;
}
Double Double::add(const Double& d)
{
   Double tmp;
   tmp.equals(data + d.toDouble());
   return tmp;
}
Double Double::sub(const Double& d)
{
   Double tmp;
   tmp.equals(data - d.toDouble());
   return tmp;
}
Double Double::mul(const Double& d)
{
   Double tmp;
   tmp.equals(data * d.toDouble());
   return tmp;
}
Double Double::div(const Double& d)
{
   Double tmp;
   tmp.equals(data / d.toDouble());
   return tmp;
}
double Double::toDouble() const
{
   return data;
}

Integer.h

#ifndef INTEGER
#define INTEGER

class Integer
{
private:
   int data;
public:


   void equals(int i);
   Integer add(const Integer& i);
   Integer sub(const Integer& i);
   Integer mul(const Integer& i);
   Integer div(const Integer& i);
   int toInt() const;

};
#endif

Integer.cpp

#include <iostream>
#include <string>
#include "Integer.h"

using namespace std;

void Integer::equals(int i)
{
   data = i;
}
Integer Integer::add(const Integer& i)
{
   Integer tmp;
   tmp.equals(data + i.toInt());
   return tmp;
}
Integer Integer::sub(const Integer& i)
{
   Integer tmp;
   tmp.equals(data - i.toInt());
   return tmp;
}
Integer Integer::mul(const Integer& i)
{
   Integer tmp;
   tmp.equals(data * i.toInt());
   return tmp;
}
Integer Integer::div(const Integer& i)
{
   Integer tmp;
   tmp.equals(data / i.toInt());
   return tmp;
}
int Integer::toInt() const
{
   return data;
}

Solutions

Expert Solution

Here are Double and Integer classes:

  • A default constructor is added to Double and Integer classes that sets the value to 0.0 and 0 respectively.
  • Overloaded constructors are also added to Double and Integer classes such that 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 the following overloaded functions are also added
    • add
    • sub
    • mul
    • div

Each of these take primitive values and return the base class type.

Main function is written in order to test constructors as well as overloaded functions

Source Code is given below:

// double.h

#ifndef DOUBLE
#define DOUBLE
class Double
{
private:
double data;

public:
Double(); // Default constructor
Double(double d); // Overloaded constructor
void equals(double d);
Double add(const Double &d);
Double sub(const Double &d);
Double mul(const Double &d);
Double div(const Double &d);

// Overloaded functions add, sub, mul, div with primitive values are parameter and return base class type
Double add(const double d);
Double sub(const double d);
Double mul(const double d);
Double div(const double d);
double toDouble() const;

};
#endif

------------------------------------------------------------------------------------------------------------------------------------------------

// double.cpp

#include <iostream>
#include <string>
#include "double.h"

using namespace std;

// Default constructor definition

Double::Double()
{
data=0.0;
}

// Overloaded constructor definition
Double::Double(double d)
{
data=d;
}
void Double::equals(double d)
{
data = d;
}
Double Double::add(const Double& d)
{
Double tmp;
tmp.equals(data + d.toDouble());
return tmp;
}
Double Double::sub(const Double& d)
{
Double tmp;
tmp.equals(data - d.toDouble());
return tmp;
}
Double Double::mul(const Double& d)
{
Double tmp;
tmp.equals(data * d.toDouble());
return tmp;
}
Double Double::div(const Double& d)
{
Double tmp;
tmp.equals(data / d.toDouble());
return tmp;
}

// Overloaded function definition add, sub, mul, div with primitive values are parameter and return base class type

Double Double::add(const double d)
{
Double tmp;
tmp.data=data+d;
return tmp;
}
Double Double::sub(const double d)
{
Double tmp;
tmp.data=data-d;
return tmp;
}
Double Double::mul(const double d)
{
Double tmp;
tmp.data=data * d;
return tmp;
}
Double Double::div(const double d)
{
Double tmp;
tmp.data= data / d;
return tmp;
}
double Double::toDouble() const
{
return data;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------

// integer.h

#ifndef INTEGER
#define INTEGER

class Integer
{
private:
int data;
public:
Integer(); // Default constructor
Integer(int);   // Overloaded constructor

   void equals(int i);
Integer add(const Integer& i);
Integer sub(const Integer& i);
Integer mul(const Integer& i);
Integer div(const Integer& i);

// Overloaded functions add, sub, mul, div with primitive values are parameter and return base class type

Integer add(const int i);
Integer sub(const int i);
Integer mul(const int i);
Integer div(const int i);
int toInt() const;

};
#endif

--------------------------------------------------------------------------------------------------------------------------------------------------------

//integer.cpp

#include <iostream>
#include <string>
#include "integer.h"

using namespace std;

// Default constructor definition

Integer::Integer()
{
data=0;
}

// Overloaded constructor definition
Integer::Integer(int i)
{
data=i;
}
void Integer::equals(int i)
{
data = i;
}
Integer Integer::add(const Integer& i)
{
Integer tmp;
tmp.equals(data + i.toInt());
return tmp;
}
Integer Integer::sub(const Integer& i)
{
Integer tmp;
tmp.equals(data - i.toInt());
return tmp;
}
Integer Integer::mul(const Integer& i)
{
Integer tmp;
tmp.equals(data * i.toInt());
return tmp;
}
Integer Integer::div(const Integer& i)
{
Integer tmp;
tmp.equals(data / i.toInt());
return tmp;
}

// Overloaded function definition add, sub, mul, div with primitive values are parameter and return base class type

Integer Integer::add(const int i)
{
Integer tmp;
tmp.data=data + i;
return tmp;
}
Integer Integer::sub(const int i)
{
Integer tmp;
tmp.data=data - i;
return tmp;
}
Integer Integer::mul(const int i)
{
Integer tmp;
tmp.data=data * i;
return tmp;
}
Integer Integer::div(const int i)
{
Integer tmp;
tmp.data= data / i;
return tmp;
}
int Integer::toInt() const
{
return data;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------

// main.cpp

#include <iostream>
#include <cstdlib>
#include <string>
#include "double.h"
#include "integer.h"

using namespace std;

int main()
{
Double d;
Double d1(2.6);
Double d2,d3;
cout << "Using default constructor for 'd' object The Double is : " <<d.toDouble() << endl;
cout << "Using Overloaded constructor for 'd1' object The Double is : " << d1.toDouble() << endl;

d.equals(12.34);
cout << "Using equals function assigning 12.34 to 'd' object The Double is : " << d.toDouble() << endl;

cout<<endl;
cout<<"ADDITION DOUBLE:"<<endl;
d2=d.add(d1);
cout<<"After adding two objects 'd' and 'd1' :"<<d2.toDouble()<<endl;

d3=d.add(3.5);
cout<<"After adding object 'd' and primitive value 3.5 :"<<d3.toDouble()<<endl;
cout << endl;

cout<<"SUBTRACTION DOUBLE:"<<endl;
d2=d.sub(d1);
cout<<"After adding two objects 'd' and 'd1' :"<<d2.toDouble()<<endl;

d3=d.sub(3.5);
cout<<"After adding object 'd' and primitive value 3.5 :"<<d3.toDouble()<<endl;
cout << endl;

cout<<"MULTIPLICATION DOUBLE:"<<endl;
d2=d.mul(d1);
cout<<"After adding two objects 'd' and 'd1' :"<<d2.toDouble()<<endl;

d3=d.mul(3.5);
cout<<"After adding object 'd' and primitive value 3.5 :"<<d3.toDouble()<<endl;
cout << endl;

cout<<"DIVISION DOUBLE:"<<endl;
d2=d.div(d1);
cout<<"After adding two objects 'd' and 'd1' :"<<d2.toDouble()<<endl;

d3=d.div(3.5);
cout<<"After adding object 'd' and primitive value 3.5 :"<<d3.toDouble()<<endl;
cout << endl;

Integer i,i1(20);
Integer i2,i3;
cout << "Using default constructor for 'i' object The Integer is : " <<i.toInt() << endl;
cout << "Using Overloaded constructor for 'i1' object The Integer is : " << i1.toInt() << endl;

i.equals(10);
cout << "Using equals function assigning 10 to 'i' object The Integer is : " << i.toInt() << endl;

cout<<endl;
cout<<"ADDITION INTEGER:"<<endl;
i2=i.add(i1);
cout<<"After adding two objects 'i' and 'i1' :"<<i2.toInt()<<endl;

i3=i.add(5);
cout<<"After adding object 'i' and primitive value 5 :"<<i3.toInt()<<endl;


cout<<"SUBTRACTION INTEGER:"<<endl;
i2=i.sub(i1);
cout<<"After adding two objects 'i' and 'i1' :"<<i2.toInt()<<endl;

i3=i.sub(5);
cout<<"After adding object 'i' and primitive value 5 :"<<i3.toInt()<<endl;

cout<<"MULTIPLICATION INTEGER:"<<endl;
i2=i.mul(i1);
cout<<"After adding two objects 'i' and 'i1' :"<<i2.toInt()<<endl;

i3=i.mul(5);
cout<<"After adding object 'i' and primitive value 5 :"<<i3.toInt()<<endl;

cout<<"DIVISION INTEGER:"<<endl;
i2=i.div(i1);
cout<<"After adding two objects 'i' and 'i1' :"<<i2.toInt()<<endl;

i3=i.div(5);
cout<<"After adding object 'i' and primitive value 5 :"<<i3.toInt()<<endl;

cout << endl;
return 0;
}

--------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT


Related Solutions

C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
code in c++ using the code given add a hexadecimal to binary converter and add a...
code in c++ using the code given add a hexadecimal to binary converter and add a binary to hexadecimal converter #include <iostream> #include <string> #include<cmath> #include<string> using namespace std; int main() { string again; do { int userChoice; cout << "Press 2 for Decimal to Binary"<< endl; cout << "Press 1 for Binary to Decimal: "; cin >> userChoice; if (userChoice == 1) { long n; cout << "enter binary number" << endl; cin>>n; int decnum=0, i=0, remainder; while(n!=0) {...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
A java program with classes and methods to manage employment. Program should be able to store/add...
A java program with classes and methods to manage employment. Program should be able to store/add employees, calculate payroll, display or schedule shifts. All classes should have at least a null constructor and copy constructor. Other constructors are up to your discretion. Include all necessary accessors and modifiers. To test the classes, create a container class with simply a main() method. The container class will have attributes that are class objects of each of the classes you create. The main()...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT