Question: C++-Inheritance Create a class called Number that is derived from string. You will use this as a base class to derive your Integer and Double
C++-Inheritance
Create a class called Number that is derived from string. You will use this as a base class to derive your Integer and Double classes.
This means that your current data sections within the Integer and Double classes will go away. Because Number is derived from string and Integer and Double are derived from Number they all become strings. This gives you a built in data section.
You are going to have to make some fundamental changes to your classes to make this work because you are currently using the primitives double and int as the data section within your class.
At this point your Number class will only contain the following code:
A no argument constructor that sets the data section to "0"
An overloaded constructor that takes a string and sets the data section to the value being passed to it.
Make sure that you call the appropriate constructor from your derived classes first.
*NOTE: I have seen students write their own string class. This is not what is being asked. C++ has a built in string class and you should be using it.
My Code
Main.cpp
#include
#include
#include "double.h"
#include "integer.h"
//#include "menu.h"
#include
using namespace std;
using namespace Diane;
void writeNumbers(std::vector
void writeNumbers(std::vector
void readNumbers();
/*void intAdd();
void intSub();
void intMul();
void intDiv();
void dblAdd();
void dblSub();
void dblMul();
void dblDiv();*/
void Exit();
bool isDecimal(std::string s);
using Diane::Double;
using Diane::Integer;
int main()
{
/*Menu *m = Menu::Instance();
m->addMenu("1. Integer Add ", intAdd);
m->addMenu("2. Integer Subtract", intSub);
m->addMenu("3. Integer Multiply ", intMul);
m->addMenu("4. Integer Divide ", intDiv);
m->addMenu("5. Double Add ", dblAdd);
m->addMenu("6. Double Subtract", dblSub);
m->addMenu("7. Double Multiply ", dblMul);
m->addMenu("8. Double Divide ", dblDiv);
m->addMenu("9. Exit ", Exit);
m->addMenu("10. Read Numbers", readNumbers);
m->runMenu();*/
//writeNumbers();
readNumbers();
return 0;
}
void writeNumbers(vector
{
ofstream outfile("Integers.txt");
if (!outfile)
{
cout << "Error opening file" << endl;
return;
}
//for()
outfile << "This text will no be inside of example.txt" << endl;
}
void writeNumbers(vector
{
}
void readNumbers()
{
ifstream inFile("Numbers.txt");
if (!inFile)
{
cout << "unable to open the file for reading" << endl;
return;
}
string input;
vector
vector
while (getline(inFile, input))
{
//input determine if double or an integer
bool isDouble = isDecimal(input);
//if Double is double put inside dNumber
if (isDouble)
dNumbers.push_back(new Double(input));
else
iNumbers.push_back(new Integer(input));
}
/*
for (int i = 0; i < iNumbers.size(); ++i)
{
cout << iNumbers[i]->toInt() << endl;
}
*/
for (auto v : dNumbers)
{
cout << v->toDbl() << endl;
}
for (auto v : iNumbers)
{
cout << v->toInt() << endl;
}
inFile.close();
}
//void intAdd()
//{
// //Menu *m = Menu::Instance();
// int a, b;
// cout << "Enter two Integers " << endl;
// cin >> a >> b;
//
// Integer x(a);
// Integer y(b);
//
// Integer *z = new Integer(x.sub(y));
// cout << "The difference is " << z->toInt() << endl;
// m->waitKey();
//}
//
//void intSub()
//{
// Menu *m = Menu::Instance();
// int a, b;
// cout << "Enter two Integers " << endl;
// cin >> a >> b;
//
// Integer x(a);
// Integer y(b);
//
// Integer *z = new Integer(x.sub(y));
// cout << "The difference is " << z->toInt() << endl;
// m->waitKey();
//}
//void intMul()
//{
// Menu *m = Menu::Instance();
// int a, b;
// cout << "Enter two Integers " << endl;
// cin >> a >> b;
// Integer x(a);
// Integer y(b);
//
// Integer *z = new Integer(x.mul(y));
// cout << "The product is " << z->toInt() << endl;
// m->waitKey();
//}
//void intDiv()
//{
// Menu *m = Menu::Instance();
// int a, b;
// cout << "Enter two Integers " << endl;
// cin >> a >> b;
//
// Integer x(a);
// Integer y(b);
//
// Integer *z = new Integer(x.div(y));
// cout << "The quotient is " << z->toInt() << endl;
// m->waitKey();
//}
//void dblAdd()
//{
// Menu *m = Menu::Instance();
// double a, b;
// cout << "Enter two doubles " << endl;
// cin >> a >> b;
//
// Double x(a);
// Double y(b);
//
// Double *z = new Double(x.add(y));
// cout << "The sum is " << z->toDbl() << endl;
// m->waitKey();
//}
//void dblSub()
//{
// Menu *m = Menu::Instance();
// double a, b;
// cout << "Enter two doubles " << endl;
// cin >> a >> b;
//
// Double x(a);
// Double y(b);
//
// Double *z = new Double(x.sub(y));
// cout << "The difference is " << z->toDbl()<< endl;
// m->waitKey();
//}
//void dblMul()
//{
// Menu *m = Menu::Instance();
// double a, b;
// cout << "Enter two doubles " << endl;
// cin >> a >> b;
//
// Double x(a);
// Double y(b);
//
// Double *z = new Double(x.mul(y));
// cout << "The product is " << z->toDbl() << endl;
// m->waitKey();
//}
//void dblDiv()
//{
// Menu *m = Menu::Instance();
// double a, b;
// cout << "Enter two doubles " << endl;
// cin >> a >> b;
//
// Double x(a);
// Double y(b);
//
// Double *z = new Double(x.div(y));
// cout << "The quotient is " << z->toDbl() << endl;
// m->waitKey();
//}
void Exit()
{
exit(1);
}
bool isDecimal(std::string s)
{
string::iterator p;
//check string for decimal return true if decimal found
for (p = s.begin(); p < s.end(); p++)
{
if (*p == '.')
{
return true;
}
}
return false;
}
Integer.h
#ifndef INTEGER
#define INTEGER
#include
using std::string;
namespace Diane
{
class Integer
{
private:
int value;
bool nan;
void Nan(string str);
void create(int input);
public:
Integer();
Integer(int input);
Integer(const Integer &input);
void equals(int input);
Integer add(const Integer &input);
Integer sub(const Integer &input);
Integer mul(const Integer &input);
Integer div(const Integer &input);
Integer add(int input);
Integer sub(int input);
Integer mul(int input);
Integer div(int input);
int toInt() const;
Integer operator +(const Integer &input);
Integer operator -(const Integer &input);
Integer operator *(const Integer &input);
Integer operator /(const Integer &input);
Integer &operator =(const Integer &input);
Integer &operator =(int &input);
bool operator ==(const Integer &input);
bool operator ==(int input);
bool operator !=(const Integer &input);
string toString();
void equals(string str);
bool isNan();
Integer operator =(string str);
Integer(string str);
};
}
#endif
Integer.cpp
#include
#include"integer.h"
#include
#include
using namespace std;
namespace Diane
{
void Integer::create(int input)
{
//this->value = new int;
if (this->value == NULL)
{
this->nan = true;
}
else
this->nan = false;
this->equals(input);
}
Integer::Integer()
{
this->create(0);
}
Integer::Integer(int input)
{
this->create(input);
}
Integer::Integer(const Integer &input)
{
this->create((int)input.toInt());
}
void Integer::equals(int input)
{
value = input;
this->nan = false;
}
Integer Integer::add(const Integer &input)
{
Integer tmp;
tmp.equals(value + input.toInt());
return tmp;
}
Integer Integer::sub(const Integer &input)
{
Integer tmp;
tmp.equals(value - input.toInt());
return tmp;
}
Integer Integer::mul(const Integer &input)
{
Integer tmp;
tmp.equals(value*input.toInt());
return tmp;
}
Integer Integer::div(const Integer &input)
{
Integer tmp;
tmp.equals(value / input.toInt());
return tmp;
}
Integer Integer::add(int input)
{
Integer tmp(input);
return add(tmp);
}
Integer Integer::sub(int input)
{
Integer tmp(input);
return sub(tmp);
}
Integer Integer::mul(int input)
{
Integer tmp(input);
return mul(tmp);
}
Integer Integer::div(int input)
{
Integer tmp(input);
return div(tmp);
}
int Integer::toInt()const
{
return value;
}
Integer Integer::operator +(const Integer &input)
{
return add(input);
}
Integer Integer::operator -(const Integer &input)
{
return sub(input);
}
Integer Integer::operator *(const Integer &input)
{
return mul(input);
}
Integer Integer::operator /(const Integer &input)
{
return div(input);
}
Integer &Integer::operator =(const Integer &input)
{
this->equals(input.value);
return *this;
}
Integer &Integer::operator =(int &input)
{
this->equals(input);
return *this;
}
bool Integer::operator ==(const Integer &input)
{
if (value == input.toInt())
return true;
return false;
}
bool Integer::operator ==(int input)
{
return value == input;
}
bool Integer::operator !=(const Integer &input)
{
if (value != input.toInt())
return true;
return false;
}
string Integer::toString()
{
string converted;
//stringstream ss;
std::stringstream ss(this->value);
converted = ss.str();
return converted;
}
void Integer::equals(string str)
{
Nan(str);
if (nan == false)
{
value = atoi(str.c_str());
}
else
{
value = 0;
}
}
void Integer::Nan(string str)
{
int count = 0;
for (unsigned int i = 0; i < str.length(); i++)
{
if (isdigit(str[i]))
{
}
else
{
this->nan = true;
return;
}
}
this->nan = false;
}
bool Integer::isNan()
{
return this->nan;
}
Integer Integer::operator=(string str)
{
equals(str);
return *this;
}
Integer::Integer(string str)
{
this->create(0);
if (!this->isNan())
this->equals(str);
};
}
Double.h
#ifndef DOUBLE_H
#define DOUBLE_H
#include
#include "integer.h"
using namespace std;
namespace Diane
{
class Double
{
private:
double *data;
void create(double d);
void NaN(string s);
bool nan;
public:
Double();
Double(double d);
Double(const Double&d);
Double(const Integer&i);
Double(string s);
void equals(double d);
void equals(string s);
void equals(const Double&d);
Double add(const Double &d);
Double sub(const Double &d);
Double mul(const Double &d);
Double div(const Double &d);
Double add(double d);
Double sub(double d);
Double mul(double d);
Double div(double d);
double toDbl() const;
Double operator + (const Double &d);
Double operator - (const Double &d);
Double operator * (const Double &d);
Double operator / (const Double &d);
string toString() const;
Double operator =(double d);
Double operator =(const Double &d);
Double operator = (std::string s);
bool operator == (const Double &d);
bool operator == (double d);
bool operator !=(const Double &d);
bool operator !=(double d);
bool isNan();
};
}
#endif
Double.cpp
#include
#include"double.h"
#include
using namespace std;
namespace Diane
{
Double::Double()
{
this->create(0.0);
}
Double::Double(double d)
{
this->create(d);
}
Double::Double(const Double &d)
{
this->create(d.toDbl());
}
Double::Double(const Integer &i)
{
this->create((double)i.toInt());
}
Double::Double(string s)
{
this->create(0.0);
if (!this->isNan())
this->equals(s);
}
void Double::create(double val)
{
data = new double;
if (data == NULL)
{
this->nan = true;
}
else
{
this->nan = false;
this->equals(val);
}
}
void Double::equals(double d)
{
*data = d;
this->nan = false;
}
void Double::equals(string s)
{
double tmp = std::stod(s);
*data = tmp;
/*
this->NaN(s);
if (!this->isNan())
*data = atof(s.c_str());
else
*data = 0.0;
*/
}
Double Double::add(const Double &d)
{
Double tmp;
*tmp.data = *data + *d.data;
return tmp;
}
Double Double::add(double d)
{
Double tmp;
tmp.equals(this->toDbl() + d);
return tmp;
}
Double Double::sub(const Double &d)
{
Double tmp;
*tmp.data = *data - *d.data;
return tmp;
}
Double Double::sub(double d)
{
Double tmp;
tmp.equals(this->toDbl() - d);
return tmp;
}
Double Double::mul(const Double &d)
{
Double tmp;
*tmp.data = *data * *d.data;
return tmp;
}
Double Double::mul(double d)
{
Double tmp;
tmp.equals(this->toDbl()* d);
return tmp;
}
Double Double::div(const Double &d)
{
Double tmp;
*tmp.data = *data / *d.data;
return tmp;
}
Double Double::div(double d)
{
Double tmp;
tmp.equals(this->toDbl() / d);
return tmp;
}
double Double::toDbl() const
{
return *data;
}
Double Double::operator = (const Double &d)
{
this->equals(d.toDbl());
return *this;
}
Double Double::operator =(double d)
{
this->equals(d);
return *this;
}
Double Double::operator = (string s)
{
this->equals(s);
return *this;
}
bool Double::operator == (const Double &d)
{
return *this->data == *d.data;
}
bool Double::operator == (double d)
{
return *this->data == d;
}
bool Double::operator != (const Double &d)
{
return *this->data != *d.data;
}
bool Double::operator != (double d)
{
return *this->data != d;
}
string Double::toString() const
{
return to_string(*data);
}
bool Double::isNan()
{
return this->nan;
}
void Double::NaN(string s)
{
int pos;
pos = s.find(' . ', 0);
if (pos != string::npos)
{
pos = s.find(" . ", pos + 1);
if (pos != string::npos)
{
this->nan = true;
return;
}
}
string::iterator p;
for (p = s.begin(); p < s.end(); p++)
{
if (!isdigit(*p) && *p != ' . ')
{
this->nan = true;
return;
}
}
this->nan = false;
return;
};
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
