c++ - Default Constructor when creating an Array of Objects within an Object -
i attempting create array of objects within object (specifically array of check
within checkbook
. not allowed use vector in place of dynamic array store objects, maddening, rules have been stipulated me.
the issue i'm struggling need provide several variables check
constructor because needs construct money
object within check
object. so i'm receiving error claiming check* checkarray = new check[];
has no default constructor.
i have since added default constructor check::check() {};
, how can dynamically fill array without arguments being passed constructor upon creation? new oop , struggling manage classes within classes. note: money class predefined , implemented
the data these relevants checks stored in .txt file in form of int(check number) '\t' double(check amount) '\t' int(bool cashed represented in 0 or 1) , temporarily storing data in struct datatransferstruct
, storing structs in vector test, can't use vector in final implementation. approaching in bad way?
relevant code below:
class money { private: long all_cents; public: friend money operator +(const money& amount1, const money& amount2); //returns sum of values of amount1 , amount2 friend money operator -(const money& amount1, const money& amount2); //returns amount1 minus amount2 friend money operator -(const money& amount); //returns negative of value of amount friend bool operator ==(const money& amount1, const money& amount2); //returns true if amount1 , amount2 have same value; false otherwise friend bool operator <(const money& amount1, const money& amount2) { return (amount1.all_cents < amount2.all_cents); }; //returns true if amount1 less amount2; false otherwise money(long dollars, int cents); // initializes object value represents amount dollars , cents given arguments. if amount //negative, both dollars , cents should negative money::money(long dollars) : all_cents(dollars * 100) {} //initializes object value represents $dollars.00 money::money() : all_cents(0) {}//initializes object value represnets $0.00 double get_value() const; //returns amount of money recorded in 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 creates money object , returns checkamount within check class money temp(dollar_value,cents_value); return temp; }
just started checkbook class
class checkbook { //checkbook contains array of checks private: check* checkarray = new check[]; };
method using store information
newfile_open(newfile); //take in , format each data line w/ struct , construct check in dynamic growing array while (newfile >> temp.checkid) { newfile >> temp.rawmoneyamount; newfile >> temp.checkcashed; //set dollars = rawmoneyamount drops cents temp.dollars = temp.rawmoneyamount; //get cents int temp.cents = (int)((temp.rawmoneyamount - temp.dollars) * 100); }
you should not make array in class
class checkbook { //checkbook contains array of checks private: check* checkarray = new check[]; };
you should define in class checkbook constructor.
like
class checkbook { //checkbook contains array of checks private: check* checkarray; checkbook() { checkarray = new checkarray[size]; } };
Comments
Post a Comment