Creating array of pointers to poin to diffrent objects

Hi guys.I have to make an array of 2018 pointers that points to objects type Number of which 1009 point to Rational numbers,and 1009 to complex numbers.I have two classes ComplexNumbers and RationalNumbers that are derived classes.
This is my code:
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




#include<iostream>
#include<math.h>
using namespace std;
class Numbers{
private:
	char *type;
	double value;
public:
	Numbers(double n=0);
	~Numbers();
	Numbers(const Numbers &b);
	void Compare(const Numbers &b);
	virtual void Print() ;
	virtual double Return();


protected:
	virtual void Set(double x);

};
class RationalNumbers:public Numbers
{
private:double c;
public:
	RationalNumbers(double x, double y);
	

};
class ComplexNumbers:public Numbers
{
private:
	double c;
public:
ComplexNumbers(double x, double y);
		

};
Numbers::Numbers(double n)
{
	value = n;
}
Numbers::~Numbers()
{
	delete[] type;
}
Numbers::Numbers(const Numbers &b)
{
	value = b.value;
	type = b.type;

}
void Numbers::Compare(const Numbers &b)
{
	if (this->value > b.value)
		cout << "First number is greater" << endl;
	else
		if (this->value == b.value)
			cout << "Numbers are equal" << endl;
		else
			cout << "Second number is greater" << endl;

}

void Numbers::Print()
{
	cout << "Type" << type << "and value of the number" << valeu<< endl;

}

double Numbers::Return()
{
	return value;
}

void Numbers::Set(double x)
{
	this->value = x;
}
RationalNumbers::RationalNumbers(double x, double y)
{
	
	c = x / y;

}
ComplexNumbers::ComplexNumbers(double x, double y)
{

	c = sqrt(x*x + y * y);

}

I also need to read numbers from file or randomly.Do i need to find somewhere 2018 numbers and put them in .txt file and read them or there is another solution?
In main i tried only with this
 
Numbers **p;

Thank you for your replies.
Last edited on
Do i need to find somewhere 2018 numbers and put them in .txt file and read them or there is another solution?
You can test your program with a lesser amount of numbers. Just use constants for the amount.
If this is an assignment then can you post the full text? To me, it doesn't make sense to have either RationalNumbers or ComplexNumbers derive from Numbers, at least not the way you've written them.

A rational number has an integer numerator and denominator. Since Numbers contains a double instead of an int, that doesn't seem appropriate.

A complex number has real and imaginary parts. You could derive from Numbers to get one part but then the other part would be a member, and that seems awkward.

Numbers::Return() is virtual. What should it return for a complex number? Just the real part? And Set() is virtual but it only takes one parameter. How to do you use it to set a complex number?

All of these questions hint that the design isn't right. I have some ideas on how to clean it up, but without seeing the assignment, it's hard to know if I'm going in the right direction.
Thank you for your replies.I have changed values from double to int.My assignment was to make a class Numbers and two derived classes RationalNumbers(where numbers are a/b,i have changed to intiger) and ComplexNumbers(a +jb where i need to return it's moduo sqrt(a*a+b*b).In the main I created a array of pointers where 1009 point to RationalNumbers and 1009 to ComplexNumbers.My teacher has written this code for me.
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
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include"Numbers.h"
double fRand(double fMin, double fMax);
void main()
{
	
	Numbers **number= new Numbers*[2018];
	for (int i = 0; i < 1009; i++)
	{
RationalNumbers *number1 = new RationalNumbers (rand() % 1000+1, rand() % 1000+1);
		ComplexNumbers *number2 = new ComplexNumbers(fRand(1,1000), fRand(1, 1000));//Randomly generating numbers
		number[2 * i] = number1;
		number[2 * i + 1] =number2;
	}//know i have to sort array of numbers in descending order
	for ( int i = 0; i < 2018; i++)//This is my code for sorting an array
		for (int j = 1; j < 2018; j++)//I know it is not a best option
			if (number[j-1]->Return() < number[j]->Return())
			
			{
				Numbers *pom = number[j-1];
				number[j-1] = number[j];
				number[j] = pom;
			}
	





	
	system("pause");
}
double fRand(double fMin, double fMax)
{
	double f = (double)rand() / RAND_MAX;
	return fMin + f * (fMax - fMin);
}


Is there another way to do this
1
2
3
4
5
6
7
Numbers **number= new Numbers*[2018];
	for (int i = 0; i < 1009; i++)
	{
RationalNumbers *number1 = new RationalNumbers (rand() % 1000+1, rand() % 1000+1);
	ComplexNumbers *number2 = new ComplexNumbers(fRand(1,1000), fRand(1, 1000));
		number[2 * i] = number1;
		number[2 * i + 1] =number2;
Last edited on
Topic archived. No new replies allowed.