OK, I've got explicit now, also passing multiple arguments to the constructor and also I've tried out deque.
So to summarise the solution.
I have two classes
cs_carerates and
cs_adults. I use a vector (named
crv) to hold multiple instances of objects of the class cs_carereates and a deque (named
cad) to hold multiple instances of objects of the class cs_adults.
The constructor for cs_carerates takes one argument and is not explicit so you can use either crv.push_back(60) or crv.push_back(cs_carerates(60)); to construct the vector instance.
The constructor for cs_adults requires 3 arguments, therefore (I believe explicit is implied) only the latter method of using push_back, with a call to the construction function as the argument can be used e.g. cad.push_back(cs_adults("Fred",50000,1));
Thanks Bazzy for pointing out what I missed from anilpanicker, sorry to anilpaniker for missing your input.
So here's the code for the solution :-
The cs_carerates class definition :-
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
|
// Constructor which takes the level of care in nights
// and then
cs_carerates::cs_carerates(int carenights) {
levelofcare = carenights;
// Set the carerate (i.e. actual percentage of care)
float cn = ((float)carenights / 365) * 100;
if (cn < 50) {
carerate = (int)floor(cn);
}
else {
carerate = (int)ceil(cn);
}
// set the cost rate
if (carerate <= 13) { costrate=0; }
if (carerate >= 14 && carerate <= 34) { costrate = 24; }
if (carerate >= 35 && carerate <= 47) { costrate = 25 + ((carerate - 35) * 2); }
if (carerate >= 48 && carerate <= 52) { costrate = 50; }
if (carerate >= 53 && carerate <= 65) { costrate = 51 + ((carerate - 53) * 2); }
if (carerate >= 66 && carerate <= 86) { costrate = 76; }
if (carerate >= 87) { costrate = 100; }
}
//destructor (does nothing)
cs_carerates::~cs_carerates() {
}
int cs_carerates::getcarerate() {
return carerate;
}
int cs_carerates::getcostrate() {
return costrate;
}
|
The cs_adult class definition :-
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
|
// Class to hold basic adult information
class cs_adult {
public:
cs_adult(string adults_name, int adults_ati, int adults_aid);
~cs_adult();
string getaname();
int getati();
int getaid();
private:
string aname;
int aid;
int ati;
};
// Constructor
cs_adult::cs_adult(string adults_name, int adults_ati, int adults_aid) {
aname = adults_name;
ati = adults_ati;
aid = adults_aid;
}
// Destructor (does nothing)
cs_adult::~cs_adult() {
}
// Getaname (returns adults name as string)
string cs_adult::getaname() {
return aname;
}
// getati (returns adutls ati (Adjusted Taxable Income))
int cs_adult::getati() {
return ati;
}
// getaid ( returns adults id ( unique identifier))
int cs_adult::getaid() {
return aid;
}
|
The relevant 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
|
#include <vector>
#include <deque>
.....
vector<cs_carerates> crv; // define vector to hold an unknown # of cs_carerates instances
for (int i = 0;i < 40;i++) { // loop to create 40 instances
crv.push_back(i * 5); // create the current instance with an implicit call to the
// constructor
// output the values to check that all is OK.
cout << "Nights Care=" << i * 5;
cout << " Care Rate=" << crv[i].getcarerate();
cout << " Cost Rate=" << crv[i].getcostrate() << endl;
}
deque<cs_adult> cad; // define deque to hold unknown # of cs_adult instances
for (int i=0; i < 10; i++) { // loop to create 10 instances
cad.push_back(cs_adult("Fred",50000+(i*10),i)); // create current instance with explicit call
// to the multi-argument constructor
// output values to check that all is OK.
cout << "Adult is=" << cad[i].getaname();
cout << " ATI=" << cad[i].getati() << endl;
}
|