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:
#include<iostream>
#include<math.h>
usingnamespace std;
class Numbers{
private:
char *type;
double value;
public:
Numbers(double n=0);
~Numbers();
Numbers(const Numbers &b);
void Compare(const Numbers &b);
virtualvoid Print() ;
virtualdouble Return();
protected:
virtualvoid 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;
elseif (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
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.
#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;