Problems with Inheritance.

What would be the changes I need to make to my code in order to make sure it can actually add my numbers together? It asks me to do the following:

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.


I believe I've done that, but I am unsure how to continue on. Here's my code:

Last edited on
Double.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  #ifndef DOUBLE
#define DOUBLE
#include "Integer.h"
#include "Numbers.h"
#include <iostream>
#include <string>
namespace Norman
{
	class Double : public Numbers
	{
	private:
	
		bool nan;
		void isNan(std::string s);
	public:
		bool isNan() { return this->nan; }
		void equals(double d);
		void equals(std::string s);
		Double add(const Double &d);
		Double sub(const Double &d);
		Double mul(const Double &d);
		Double div(const Double &d);
		double toDouble() const;
		friend std::istream &operator>>(std::istream &is, Double& m);
		bool recursiveNaN(std::string s, int i, int flag);

		// Overloaded Functions
		Double add(double d);
		Double sub(double d);
		Double mul(double d);
		Double div(double d);

		// Constructors
		Double();
		Double(double d);
		Double(const Double &d);
		Double(const Integer &i);
		Double(const std::string str);

		//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);
		bool operator != (const Double &d);
		bool operator != (double d);

		std::string toString();


	};
}
#endif

Integer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef INTEGER
#define INTEGER
#include "Numbers.h"
#include <string>
#include <iostream>

namespace Norman
{
	class Integer : public Numbers
	{
	private:
		bool nan;
		void isNaN(std::string s);
	public:
		bool isNaN() { return this->nan; }
		void equals(int i);
		void equals(std::string s);
		const string& Data() const { return data; }
		friend std::istream &operator>>(std::istream &is, Integer& m);
		Integer add(const Integer &i);
		Integer sub(const Integer &i);
		Integer mul(const Integer &i);
		Integer div(const Integer &i);
		int toInt() const;
		bool recursiveNaN(std::string s, int i, int flag);

		// Overloaded Functions
		Integer add(int i);
		Integer sub(int i);
		Integer mul(int i);
		Integer div(int i);

		// Constructors
		Integer();
		Integer(int i);
		Integer(const Integer &i);
		Integer(const std::string str);

		//Overloaded Operators
		Integer operator + (const Integer &i);
		Integer operator - (const Integer &i);
		Integer operator * (const Integer &i);
		Integer operator / (const Integer &i);
		Integer &operator = (std::string s);
		Integer &operator = (const Integer&i);
		Integer &operator = (int i);
		bool operator == (const Integer &i);
		bool operator == (int i);
		bool operator != (const Integer &i);
		bool operator != (int i);

		std::string toString();

	};
}

#endif 


Menu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef MENU
#define MENU

#include <iostream>
#include <string>
#include <conio.h>
#include <vector>
namespace Norman
{
	const int MAX_COUNT = 20;

	//struct
	struct menuItem
	{
		void(*func)();
		std::string description;
	};

	class Menu
	{
	private: //data section
		std::vector<menuItem> mi;
		int count;
		void runSelection();
		static Menu *pInstance;

	public:
		//constructors
		Menu();

		//mutators
		void addMenu(std::string description, void(*f)(void));

		//accessors
		void runMenu();
		void waitKey();
		static Menu *Instance();
	};
}
#endif

[/code]
Numbers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef NUMBERS
#define NUMBERS

#include <iostream>
#include <string>

using std::string;

class Numbers : public string
{
public:
	string data;
	Numbers(){data = "0";}
	Numbers(string str){data = str;}
};

#endif  

Double.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>
#include <sstream>
#include "Double.h"
#include "Integer.h"

namespace Norman
{

	using namespace std;

	// Constructors
	Double::Double()
		:nan(false)
	{
		this->equals(0);
	}
	Double::Double(double d)
		: nan(false)
	{
		this->equals(d);
	}
	Double::Double(const Double &d)
		: nan(false)
	{
		this->equals(d.toDouble());
	}
	Double::Double(const Integer &i)
		: nan(false)
	{
		this->equals(static_cast<double>(i.toInt()));
	}
	//Overloaded Functions
	Double Double::add(double d)
	{
		return Double(this->toDouble() + d);
	}
	Double Double::sub(double d)
	{
		return Double(this->toDouble() - d);
	}
	Double Double::mul(double d)
	{
		return Double(this->toDouble() * d);
	}
	Double Double::div(double d)
	{
		return Double(this->toDouble() / d);
	}
	void Double::equals(double d)
	{
		this->data = d;
		this->nan = false;
	}
	void Double::equals(std::string s)
	{
		this->isNan(s);
		if (!isNan())
		{
			this->assign(s);
		}
	}
	double Double::toDouble() const
	{
		return stoi(data);
	}

	Double Double::add(const Double &d)
	{
		Double tmp;
		tmp.equals(this->toDouble() + d.toDouble());
		return tmp;
	}

	Double Double::sub(const Double &d)
	{
		Double tmp;
		tmp.equals(this->toDouble() - d.toDouble());
		return tmp;
	}

	Double Double::mul(const Double &d)
	{
		Double tmp;
		tmp.equals(this->toDouble() * d.toDouble());
		return tmp;
	}

	Double Double::div(const Double &d)
	{
		Double tmp;
		tmp.equals(this->toDouble() / d.toDouble());
		return tmp;
	}
	istream& operator>>(istream &is, Double &m)
	{
		is >> m.data;
		return is;
	}
	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 = (string s)
	{
		this->equals(s);
		return *this;
	}
	bool Double::operator == (const Double &d)
	{
		return this->toDouble() == d.toDouble();
	}
	bool Double::operator == (double d)
	{
		return this->toDouble() == d;
	}
	bool Double::operator != (const Double &d)
	{
		return this->toDouble() != d.toDouble();
	}
	bool Double::operator != (double d)
	{
		return this->toDouble() != d;
	}
	std::string Double::toString()
	{
		std::stringstream ss;
		ss << this->data;
		return ss.str();
	}

	bool Double::recursiveNaN(string s, int i = 0, int flag = 0)
	{
		if (s.length() == 0)
			return false;
		if (i == s.length())
			return true;
		if (s[i]>47 && s[i]<58)
			return recursiveNaN(s, i + 1, flag);
		else if (s[i] == '.')
		{
			if (flag == 0)
			{
				flag = 1;
				return recursiveNaN(s, i + 1, flag);
			}
			else
				return false;
		}
		else
			return false;
	}

	void Double::isNan(std::string s)
	{
		if (recursiveNaN(s))
			this->nan = false;
		else
			this->nan = true;

		return;
	}


}

Integer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <sstream>
#include <string>
#include "Integer.h"

namespace Norman
{
	using namespace std;

	//Constructors

	Integer::Integer()
		:nan(false)
	{
		this->equals(0);
	}

	Integer::Integer(int i)
		: nan(false)
	{
		this->equals(i);
		this->nan = false;
	}

	Integer::Integer(const Integer &i)
		:nan(false)
	{
		this->equals(i.toInt());
	}

	//Overloaded Functions

	Integer Integer::add(int i)
	{
		return Integer(this->toInt() + i);
	}

	Integer Integer::sub(int i)
	{
		return Integer(this->toInt() - i);
	}

	Integer Integer::mul(int i)
	{
		return Integer(this->toInt() * i);
	}

	Integer Integer::div(int i)
	{
		return Integer(this->toInt() / i);
	}
	void Integer::equals(int i)
	{
		this->data = i;
	}
	void Integer::equals(std::string s)
	{
		this->isNaN(s);
		if (!isNaN())
		{
			this->assign(s);
		}
	}

	int Integer::toInt() const
	{
		return stoi(data);
	}

	Integer Integer::add(const Integer &i)
	{
		Integer tmp;
		tmp.equals(this->toInt() + i.toInt());
		return tmp;
	}

	Integer Integer::sub(const Integer &i)
	{
		Integer tmp;
		tmp.equals(this->toInt() - i.toInt());
		return tmp;
	}

	Integer Integer::mul(const Integer &i)
	{
		Integer tmp;
		tmp.equals(this->toInt() * i.toInt());
		return tmp;
	}

	Integer Integer::div(const Integer &i)
	{
		Integer tmp;
		tmp.equals(this->toInt() / i.toInt());
		return tmp;
	}
	istream& operator>>(istream& is, Integer& m)
	{
		is >> m.data;
		return is;
	}
	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 = (string s)
	{
		this->equals(s);
		return *this;
	}
	bool Integer::operator == (const Integer &i)
	{
		return this->toInt() == i.toInt();
	}
	bool Integer::operator == (int i)
	{
		return this->toInt() == i;
	}
	bool Integer::operator != (const Integer &i)
	{
		return this->toInt() != i.toInt();
	}
	bool Integer::operator != (int i)
	{
		return this->toInt() != i;
	}
	std::string Integer::toString()
	{
		std::stringstream ss;
		ss << this->data;
		return ss.str();
	}
	bool Integer::recursiveNaN(string s, int i = 0, int flag = 0)
	{
		if (s.length() == 0)
			return false;
		if (i == s.length())
			return true;
		if (s[i]>47 && s[i]<58)
			return recursiveNaN(s, i + 1, flag);
		else if (s[i] == '.')
		{
			if (flag == 0)
			{
				flag = 1;
				return recursiveNaN(s, i + 1, flag);
			}
			else
				return false;
		}
		else
			return false;
	}

	void Integer::isNaN(std::string s)
	{
		if (recursiveNaN(s))
			this->nan = false;
		else
			this->nan = true;

		return;
	}
}
main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <sstream>
#include <vector>
#include "Double.h"
#include "Integer.h"
#include "Menu.h"
using namespace std;

using Norman::Double;
using Norman::Integer;
using Norman::Menu;

void doubleCheck();
void doubleAdd();
void doubleSub();
void doubleMul();
void doubleDiv();
void intCheck();
void intAdd();
void intSub();
void intMul();
void intDiv();
void Exit();
int main()
{
	Menu *m = Menu::Instance();
	m->addMenu("1. Add Doubles.", doubleAdd);
	m->addMenu("2. Sub Doubles.", doubleSub);
	m->addMenu("3. Mul Doubles.", doubleMul);
	m->addMenu("4. Div Doubles.", doubleDiv);
	m->addMenu("5. Add Integers.", intAdd);
	m->addMenu("6. Sub Integers.", intSub);
	m->addMenu("7. Mul Integers.", intMul);
	m->addMenu("8. Div Integers.", intDiv);
	m->addMenu("9. Check doubles.", doubleCheck);
	m->addMenu("10. Check Integers.", intCheck);
	m->addMenu("11. Exit.", Exit);
	m->runMenu();

	return 0;
}
void doubleCheck()
{
	string s;
	Double d;
	cout << "Please enter a double." << endl;
	cin >> s;
	d.equals(s);
	if (d.isNan())
		cout << "Cannot assign a non-number to class Double" << endl;
	if (d.isNan() == false)
		cout << "Valid number set to " << d.toString() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void intCheck()
{
	string s;
	Integer i;
	cout << "Please enter an integer." << endl;
	cin >> s;
	i.equals(s);
	if (i.isNaN())
		cout << "Cannot assign a non-number to class Integer." << endl;
	if (i.isNaN() == false)
		cout << "Valid number set to " << i.toString() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void doubleAdd()
{
	Double d, d2, d3;
	cout << "Enter your first double." << endl;
	cin >> d;
	cout << "Enter your second double." << endl;
	cin >> d2;
	d3 = d + d2;
	cout << d3.toDouble() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void doubleSub()
{
	Double d, d2, d3;
	cout << "Enter your first double." << endl;
	cin >> d;
	cout << "Enter your second double." << endl;
	cin >> d2;
	d3 = d - d2;
	cout << d3.toDouble() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void doubleMul()
{
	Double d, d2, d3;
	cout << "Enter your first double." << endl;
	cin >> d;
	cout << "Enter your second double." << endl;
	cin >> d2;
	d3 = d * d2;
	cout << d3.toDouble() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void doubleDiv()
{
	Double d, d2, d3;
	cout << "Enter your first double." << endl;
	cin >> d;
	cout << "Enter your second double." << endl;
	cin >> d2;
	d3 = d / d2;
	cout << d3.toDouble() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void intAdd()
{
	Integer i, i2, i3;
	cout << "Enter your first Integer." << endl;
	cin >> i;
	cout << "Enter your second Integer." << endl;
	cin >> i2;
	i3 = i + i2;
	cout << i3.toInt() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void intSub()
{
	Integer i, i2, i3;
	cout << "Enter your first Integer." << endl;
	cin >> i;
	cout << "Enter your second Integer." << endl;
	cin >> i2;
	i3 = i - i2;
	cout << i3.toInt() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void intMul()
{
	Integer i, i2, i3;
	cout << "Enter your first Integer." << endl;
	cin >> i;
	cout << "Enter your second Integer." << endl;
	cin >> i2;
	i3 = i * i2;
	cout << i3.toInt() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void intDiv()
{
	Integer i, i2, i3;
	cout << "Enter your first Integer." << endl;
	cin >> i;
	cout << "Enter your second Integer." << endl;
	cin >> i2;
	i3 = i / i2;
	cout << i3.toInt() << endl;
	Menu *m = Menu::Instance();
	m->waitKey();
}
void Exit()
{
	cout << "Thanks for using the program." << endl;
	exit(0);

}

Menu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "Menu.h"
#include <iostream>
#include <vector>
#include <cstdlib>
namespace Norman
{
	Menu::Menu()
		: count(0), mi(MAX_COUNT) {};
	Menu *Menu::pInstance = NULL;
	Menu *Menu::Instance()
	{
		if (pInstance == NULL)
			pInstance = new Menu;
		return pInstance;
	}
	void Menu::runSelection()
	{
		int select;
		std::cin >> select;
		if (select <= this->count)
			this->mi[select - 1].func();

	}
	void Menu::addMenu(std::string description, void(*f)(void))
	{
		if (this->count < MAX_COUNT)
		{
			this->mi[count].func = f;
			this->mi[count].description = description;
			count++;
		}
	}
	void Menu::runMenu()
	{
		for (;;)
		{
			system("CLS");
			for (unsigned int pos = 0; pos < this->count; pos++)
				std::cout << this->mi[pos].description << std::endl;

			runSelection();
		}
	}
	void Menu::waitKey()
	{
		std::cout << "Press any key to continue" << std::endl;
		while (!_kbhit());

		std::fflush(stdin);
	}
}
Topic archived. No new replies allowed.