I am attempting to create an array of objects within another object (specifically an array of Check within Checkbook. I am not allowed to use a vector in place of a dynamic array to store the objects, which is maddening, but those are the rules that have been stipulated to me.
The issue i'm struggling with is that I need to provide several variables to the check constructor because it needs to construct an Money object within the check object. So i'm receiving an error claiming that Check* checkArray = new Check[]; has no default constructor.
I have since added a default constructor which is just Check::Check() {};, but how can I dynamically fill an array without arguments being initially passed to the constructor upon creation? I am very new to OOP and am struggling to manage classes within classes. Note: The money Class was predefined and implemented
The data for these relevants checks are stored in a .txt file in the form of int(check number) '\t' double(check amount) '\t' int(bool for cashed represented in 0 or 1) and I am temporarily storing the data in a struct DataTransferStruct and then storing the structs in a vector just to test, but I can't use the vector in my final implementation. Am I approaching this in a bad way?
Relevant Code Below:
class Money
{
private:
long all_cents;
public:
friend Money operator +(const Money& amount1, const Money& amount2); //Returns sum of the values of amount1 and amount2
friend Money operator -(const Money& amount1, const Money& amount2); //Returns amount1 minus amount2
friend Money operator -(const Money& amount); //Returns the negative of the value of amount
friend bool operator ==(const Money& amount1, const Money& amount2); //Returns true if amount1 and amount2 have the same value; false otherwise
friend bool operator <(const Money& amount1, const Money& amount2) { return (amount1.all_cents < amount2.all_cents); }; //Returns true if amount1 is less than amount2; false otherwise
Money(long dollars, int cents); // Initializes the object so its value represents an amount with the dollars and cents given by arguments. If the amount is
//negative, then both dollars and cents should be negative
Money::Money(long dollars) : all_cents(dollars * 100) {} //Initializes the object so its value represents $dollars.00
Money::Money() : all_cents(0) {}//Initializes the object so its value represnets $0.00
double get_value() const; //Returns the amount of money recorded in the data portion of hte calling object
};
Check Class
class Check
{
//Check dataTypes
private:
int checkNum;
Money checkAmount;
bool cashed;
public:
//Constructor
Check::Check(long dollar_Value, int cents_Value, int check_Num, int cashed_) : checkAmount(CreateMoneyClass(dollar_Value, cents_Value)) { checkNum = check_Num; if (cashed_ == 1)cashed = true; else cashed = false; };
Check::Check() {};
//Deconstructor
Check::~Check() {};
//Member functions
Money CreateMoneyClass(long dollar_Value, int cents_Value);
int GetCheckNum() const { return checkNum; };
double GetCheckAmount() const { return checkAmount.get_value(); };
bool CheckCashed() const { return cashed; };
};
Money Check::CreateMoneyClass(long dollar_Value, int cents_Value)
{
//Function that creates a Money object and returns to checkAmount within Check class
Money temp(dollar_Value,cents_Value);
return temp;
}
Just started CheckBook class
class CheckBook
{
//Checkbook contains an array of checks
private:
Check* checkArray = new Check[];
};
Method I was using to store information
NewFile_Open(newFile);
//take in and format each data line w/ struct and then construct check in dynamic growing array
while (newFile >> temp.checkID)
{
newFile >> temp.rawMoneyAmount;
newFile >> temp.checkCashed;
//set dollars = rawMoneyAmount which drops cents
temp.dollars = temp.rawMoneyAmount;
//Get cents as int
temp.cents = (int)((temp.rawMoneyAmount - temp.dollars) * 100);
}
std::vectoryourself then. You'll need to allocate appropriately sized and aligned raw storage, then use placement-new to construct your objects within as needed.std::vector" means: creating your own version of it.