Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Level 2 - Assignment 11: Inheritance Create a class called Number that is derived from string. You will use this as a base class

C++ Level 2 - Assignment 11: 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.

Required

You must have your name, ID, Date, Assignment number, and a brief description of what the file is in comments at the top of each page. Failure to do this will result in 10 points being deducted off the top of your grade.

Integer.h

#include

using namespace std;

#ifndef INTEGER

#define INTEGER

class Integer

{

private:

int intData;

bool nan;

void isNan(std::string s);

public:

//Constructors:

Integer();

Integer(const Integer &i);

Integer(int i);

Integer(const std::string str);

int toInt() const;

void equals(int i);

void equals(std::string s);

Integer add(const Integer &i);

Integer sub(const Integer &i);

Integer mul(const Integer &i);

Integer div(const Integer &i);

//Primitive Constructors

Integer add(int &i);

Integer sub(int &i);

Integer mul(int &i);

Integer div(int &i);

// Overloaded Operators

Integer operator + (const Integer &i);

Integer operator - (const Integer &i);

Integer operator * (const Integer &i);

Integer operator / (const Integer &i);

Integer &operator = (const Integer &i);

Integer &operator = (int i);

Integer &operator = (std::string s);

bool operator == (const Integer &i);

bool operator == (int i);

std::string toString();

bool isNan();

};

#endif

Integer.cpp

#include

#include

#include

#include "Integer.h"

Integer::Integer()

:intData(0), nan(false)

{

}

Integer::Integer(const Integer &i)

: nan(false)

{

equals(i.intData);

}

Integer::Integer(int i)

: intData(i), nan(false)

{

}

Integer::Integer(const std::string str)

{

this->equals(str);

}

int Integer::toInt() const

{

return this->intData;

}

void Integer::equals(int i)

{

intData = i;

}

void Integer::equals(std::string s)

{

this->isNan(s);

if (this->nan)

this->intData = 0;

else

this->intData = stod(s);

}

Integer Integer::add(const Integer &i)

{

Integer ans(this->intData + i.toInt());

return ans;

}

Integer Integer::sub(const Integer &i)

{

Integer ans(this->intData - i.toInt());

return ans;

}

Integer Integer::mul(const Integer &i)

{

Integer ans(this->intData - i.toInt());

return ans;

}

Integer Integer::div(const Integer &i)

{

Integer ans(this->intData - i.toInt());

return ans;

}

// Primitives

Integer Integer::add(int &i)

{

Integer ans;

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

return ans;

}

Integer Integer::sub(int &i)

{

Integer ans;

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

return ans;

}

Integer Integer::mul(int &i)

{

Integer ans;

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

return ans;

}

Integer Integer::div(int &i)

{

Integer ans;

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

return ans;

}

//Operator Overloads

Integer Integer::operator + (const Integer &i)

{

return this->add(i);

}

Integer Integer::operator - (const Integer &i)

{

return this->sub(i);

}

Integer Integer::operator * (const Integer &i)

{

return this->mul(i);

}

Integer Integer::operator / (const Integer &i)

{

return this->div(i);

}

Integer & Integer::operator=(const Integer & i)

{

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

return *this;

}

Integer & Integer::operator = (int i)

{

this->equals(i);

return *this;

}

Integer & Integer::operator = (std::string s)

{

this->equals(s);

return *this;

}

bool Integer::operator==(const Integer & i)

{

return this->intData == i.intData;

}

bool Integer::operator == (int i)

{

return this->intData = i;

}

std::string Integer::toString()

{

std::stringstream ss;

ss << this->intData;

return ss.str();

}

void Integer::isNan(std::string s)

{

int pos;

this->nan = false;

pos = s.find(".", 0);

if (pos != string::npos)

{

pos = s.find(".", +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;

}

}

return;

}

bool Integer::isNan()

{

return nan;

}

Double.h

#include "Integer.h"

#include

#ifndef DOUBLE

#define DOUBLE

class Double

{

private:

double doubleData;

bool nan;

void isNan(std::string s);

public:

//Constructors:

Double();

Double(const Double &d);

Double(double d);

Double(const Integer &i);

Double(const std::string str);

double toDouble() const;

void equals(std::string s);

void equals(double d);

Double add(const Double &d);

Double sub(const Double &d);

Double mul(const Double &d);

Double div(const Double &d);

//Primitive Functions

Double add(double &d);

Double sub(double &d);

Double mul(double &d);

Double div(double &d);

//Operator Overloads

Double operator + (const Double &d);

Double operator - (const Double &d);

Double operator * (const Double &d);

Double operator / (const Double &d);

Double &operator = (const Double &d);

Double &operator = (double d);

Double &operator = (std::string s);

bool operator == (const Double &d);

bool operator == (double d);

std::string toString();

bool isNan();

};

#endif

Double.cpp

#include"Double.h"

#include"Integer.h"

#include

#include

#include

Double::Double()

:doubleData(0.0), nan(false)

{

}

Double::Double(const Double &d)

: nan(false)

{

equals(d.doubleData);

}

Double::Double(double d)

: doubleData(d), nan(false)

{

}

Double::Double(const Integer &i)

: nan(false)

{

equals((double)i.toInt());

}

Double::Double(const std::string str)

{

this->equals(str);

}

double Double::toDouble() const

{

return this->doubleData;

}

void Double::equals(std::string s)

{

this->isNan(s);

if (this->nan)

this->doubleData = 0.0;

else

this->doubleData = stod(s);

}

void Double::equals(double d)

{

doubleData = d;

}

Double Double::add(const Double &d)

{

Double ans(doubleData + d.toDouble());

return ans;

}

Double Double::sub(const Double &d)

{

Double ans(doubleData - d.toDouble());

return ans;

}

Double Double::mul(const Double &d)

{

Double ans(doubleData * d.toDouble());

return ans;

}

Double Double::div(const Double &d)

{

Double ans(doubleData / d.toDouble());

return ans;

}

//Primitive Function Definitions:

Double Double::add(double &d)

{

Double ans;

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

return ans;

}

Double Double::sub(double &d)

{

Double ans;

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

return ans;

}

Double Double::mul(double &d)

{

Double ans;

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

return ans;

}

Double Double::div(double &d)

{

Double ans;

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

return ans;

}

// Operator Overloads:

Double Double::operator + (const Double &d)

{

return this->add(d);

}

Double Double::operator -(const Double &d)

{

return this->sub(d);

}

Double Double::operator * (const Double &d)

{

return this->mul(d);

}

Double Double::operator / (const Double &d)

{

return this->div(d);

}

Double & Double::operator = (const Double & d)

{

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

return *this;

}

Double & Double::operator = (double d)

{

this->equals(d);

return *this;

}

Double & Double::operator = (std::string s)

{

this->equals(s);

return *this;

}

bool Double::operator == (const Double &d)

{

return this->doubleData == d.doubleData;

}

bool Double::operator == (double d)

{

return this->doubleData == d;

}

std::string Double::toString()

{

std::stringstream ss;

ss << this->doubleData;

return ss.str();

}

void Double::isNan(std::string s)

{

int pos;

this->nan = false;

pos = s.find(".", 0);

if (pos != string::npos)

{

pos = s.find(".", +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;

}

}

return;

}

bool Double::isNan()

{

return nan;

}

Menu.h

#ifndef MENU

#define MENU

#include

#include

#include

#include

#include

using std::vector;

struct menuItem

{

std::string descript;

void(*func)();

};

class Menu

{

private:

vector mI;

void run();

public:

Menu();

void addMenu(std::string Descript, void(*f)());

void runMenu();

void waitKey();

};

#endif

Menu.cpp

#include "Menu.h"

using namespace std;

Menu::Menu()

{

//Default constructor.

}

void Menu::addMenu(std::string Descript, void(*f)())

{

menuItem m;

m.descript = Descript;

m.func = f;

mI.push_back(m); // Do more research on this.

}

void Menu::run()

{

menuItem tmp;

for (;;) // Forever loop

{

for (int i = 0; i < (int)mI.size(); i++)

{

tmp = mI.at(i);

cout << tmp.descript << endl;

}

run();

}

}

void Menu::runMenu()

{

int select;

cin >> select;

if (select <= (int)mI.size())

{

mI[select - 1].func();

}

}

void Menu::waitKey()

{

cout << "Press any key to continue..." << endl;

getchar();

cin.ignore();

}

main.cpp

#include

#include

#include

#include

#include

#include "Double.h"

#include "Integer.h"

#include "Menu.h"

using namespace std;

void intAdd();

void intSub();

void intMul();

void intDiv();

void doubleAdd();

void doubleSub();

void doubleMul();

void doubleDiv();

void Exit();

string toString(int num);

string toString(float num);

int writeToFile(vector dNumbers);

int writeToFile(vector iNumbers);

Menu m;

int main()

{

vector iNumbers;

vector dNumbers;

char data[100];

fstream ifile;

ifile.open("numbers.txt");

if (!ifile)

{

cout << "Cannot open output file. ";

return 1;

}

while (!ifile.eof())

{

ifile.getline(data, 100);

if (strstr(data, "."))

{

dNumbers.push_back(new float(atof(data)));

}

else

{

iNumbers.push_back(new int(atoi(data)));

}

}

writeToFile(iNumbers);

writeToFile(dNumbers);

ifile.close();

system("pause");

cout << "Welcome to the Double and Integer Menu. Select one from the menu: " << endl;

cout << " 1. Add Integer. 2. Subract Integer. 3. Multiply Integer. 4. Divide Integer. 5. Add Double. 6. Subtract Double 7. Multiply Double. 8. Divide Double 9. Exit" << endl;

m.addMenu("1. Add Integer ", intAdd);

m.addMenu("2. Subtract Integer", intSub);

m.addMenu("3. Multiply Integer ", intMul);

m.addMenu("4. Divide Integer ", intDiv);

m.addMenu("5. Add Double ", doubleAdd);

m.addMenu("6. Subtract Double ", doubleSub);

m.addMenu("7. Multiply Double ", doubleMul);

m.addMenu("8. Divide Double ", doubleDiv);

m.addMenu("9. Exit ", Exit);

m.runMenu();

return 0;

}

/******************

Integer Menu Items:

*******************/

void intAdd()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x + *y;

delete x, y;

cout << "Sum = " << z.toInt() << endl;

m.waitKey();

}

void intSub()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x - *y;

delete x, y;

cout << "Difference = " << z.toInt() << endl;

m.waitKey();

}

void intMul()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x * *y;

delete x, y;

cout << "Product = " << z.toInt() << endl;

m.waitKey();

}

void intDiv()

{

int i, i2;

cout << "Enter two Integers: " << endl;

cin >> i >> i2;

Integer *x = new Integer(i);

Integer *y = new Integer(i2);

Integer z = *x / *y;

delete x, y;

cout << "Quotient = " << z.toInt() << endl;

m.waitKey();

}

/*****************

Double Menu Items:

******************/

void doubleAdd()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x + *y;

delete x, y;

cout << "Sum = " << z.toDouble() << endl;

m.waitKey();

}

void doubleSub()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x - *y;

delete x, y;

cout << "Difference = " << z.toDouble() << endl;

m.waitKey();

}

void doubleMul()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x * *y;

delete x, y;

cout << "Product = " << z.toDouble() << endl;

m.waitKey();

}

void doubleDiv()

{

double d, d2;

cout << "Enter two Doubles: " << endl;

cin >> d >> d2;

Double *x = new Double(d);

Double *y = new Double(d2);

Double z = *x / *y;

delete x, y;

cout << "Quotient = " << z.toDouble() << endl;

m.waitKey();

}

void Exit()

{

cout << "Come again! " << endl;

exit(0);

}

/**************

File IO Items:

***************/

string toString(int num)

{

stringstream ss;

ss << num;

return ss.str();

}

string toString(float num)

{

stringstream ss;

ss << num;

return ss.str();

}

int writeToFile(vector dNumbers)

{

ofstream outFile;

outFile.open("double.txt");

if (!outFile)

{

cout << "Cannot open output file. " << endl;

return 1;

}

for (vector ::const_iterator iter = dNumbers.begin(); iter != dNumbers.end(); iter++)

{

outFile << toString(**iter) << endl;

}

outFile.close();

return 0;

}

int writeToFile(vector iNumbers)

{

ofstream outFile2;

outFile2.open("integer.txt");

if (!outFile2)

{

cout << "Cannot open output file. " << endl;

return 1;

}

for (vector ::const_iterator iter = iNumbers.begin(); iter != iNumbers.end(); iter++)

{

outFile2 << toString(**iter) << endl;

}

outFile2.close();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions