In: Computer Science
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
Integer class
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
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;
}
Here are Double and Integer classes:
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