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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Compiler directives
#include <iostream> // for cin, cout, endl
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// provide at least 4 attributes of different types
class Fruit {
public:
Fruit() {}
//Fruit(const std::string& z, int qty, double wgt, float prc, bool isp) :
//name(z), quantity(qty), weight(wgt), price(prc), is_purchased(isp) {}
Fruit(const std::string& z, int qty, double wgt, float prc, bool isp) :
name(z), is_purchased(isp) {
setQuantity(qty), setWeight(wgt), setPrice(prc);
}
void setName(const std::string& x) { name = x; }
auto getName() const { return name; }
void setQuantity(int q) { if (q >= 0) quantity = q; }
auto getQuantity() const { return quantity; }
void setWeight(double w) { if (w > 0) weight = w; }
auto getWeight() const { return weight; }
void setPrice(float p) { if (p >= 0) price = p; }
auto getPrice() const { return price; }
void setIs_purchased(bool i) { is_purchased = i; }
auto getIs_purchased() const { return is_purchased; }
private:
string name;
int quantity {};
double weight {};
float price {};
bool is_purchased {};
};
struct Fruit_t{
int quantity{};
double weight{};
float price{};
bool is_purchased{};
};
// provide 2 constructors (default with no parameters and 2nd with parameters), setters, getters.
/*
default constructor: sets all the members to their default values – if in doubt, use the
default initializers for the values, such as: int amount{}; it is a “default fruit” whatever that is.
constructor #2 The new fruit's name, amount, weight, etc. are arguments. Calls the individual
setters (below) to validate and set the
variable.
set_name Accept a string argument and copies it into the name variable
set_amount Accepts an int argument and copies it into amount variable, but ONLY if >= 0
set_weight Accept a float argument and copies it into the weight variable if valid
set_price Accepts a double argument and copies it into the price, if reasonable
Retrieval of data is done with getters:
Member Function/Method Description
get_name Returns the value in name
get_amount Returns the value of rooms
get_weight Returns the value of square_feet
get_prie Returns the list_price of the house
cout a string to output a full description:
name: orange, amount:3, weight:7.5, price: $25
*/
/*
Provide two constructors: one for a “default” object, and one for an object where all the initial
values are provided. Provide setters that validate new values and changes data members if OK.
Provide getters. One of the getters should be a show / display / print / cout getter that shows
all information about the object. Another getter should show some combination of data members,
such as: get_price_per_sqft, which performs a calculation and then produces a result:
get_price_per_sqft() returns list_price / square_feet.
After the class is working, create at least 5 different, separate instances of the class,
such as: fruit1, fruit2, ... fruit5. Create 3 instances with all valid data and 2 instances
with some (attempted) invalid data. .
(apple, orange, pear, grapes, avocado)
Setters should NOT change existing valid member values to invalid values. If given
invalid data in a setter, reject it. Do not change the value.
Modify the program to ask: "How many objects do you want?". Loop as many times specified
by the user (5 is enough for testing) to create that many objects. You can use 5 individual
variables, or you can use an array or vector. Submit one .cpp file. Do not create the class
in a separate file.
*/
int main() {
Fruit apple("apple", 4, 4.56, 4, true),
orange("orange", 7, 2.65, 5, false),
banana("banana", 4, 2.34, 2, true),
avocado("avocado", 150, -3.45, -150000, true),
grapes("grapes", -3, -1000, -10000, false);
/*
Constructors and setters should ensure that invalid values are not accepted for attributes.
For example, some values cannot be negative; a list price would not be negative, etc.
Other validation may apply, such as the number of rooms could be only 1 to 20, perhaps.
If given invalid data in a constructor, modify it to some reasonable default value. An easy,
quick, safe default is to use a default number, like 0. Your choice. Constructors do not
interact with the user. Don’t ask the user to correct invalid data inside the constructor.
The constructor has to work independently of whether there is a user sitting at a
keyboard or not. Constructors, setters and getters need to work under many circumstances –
as when there is NO user sitting at a screen with a keyboard.
Setters should NOT change existing valid member values to invalid values.
If given invalid data in a setter, reject it. Do not change the value.
To get the values for the data, you can “hard code” the data – using literals,
or you can ask the user for data using a get_input function. Allow the user
to enter negative values so invalid values can be presented to the class
constructors and setters. Data validation is a key reason for having
constructors and setters.
The testing code should test each member function: constructors,
setters and getters. Your driver code should create at least 5
instances of your class and display the contents of each instance.
Demonstrate using the class by writing test code that exercises
all the methods. It is important to test all the code you write.
*/
cout << "This program creates at least 5 separate, co-existing instances of a\n"
"custom designed object. Information about each object is displayed.\n"
"Each constructor, setter and getter is tested at least once.\n";
cout <<"\nname:" <<apple.getName() <<" price:"<< apple.getPrice()
<< " weight:"<< apple.getWeight() << " quantity:" << apple.getQuantity()
<<" was it purchased?:"<< boolalpha << apple.getIs_purchased() << '\n';
cout << "name:" << orange.getName() << " price:" << orange.getPrice()
<< " weight:"<< orange.getWeight() << " quantity:" << orange.getQuantity()
<< " was it purchased?:"<< boolalpha << orange.getIs_purchased() << '\n';
cout << "name:" << banana.getName() << " price:" << banana.getPrice()
<< " weight:"<< banana.getWeight() << " quantity:" << banana.getQuantity()
<< " was it purchased?:"<< boolalpha << banana.getIs_purchased() << '\n';
cout << "name:" << avocado.getName() << " price:" << avocado.getPrice()
<< " weight:"<< avocado.getWeight() << " quantity:" << avocado.getQuantity()
<< " was it purchased?:"<< boolalpha << avocado.getIs_purchased() << '\n';
cout << "name:" << grapes.getName() << " price:" << grapes.getPrice()
<< " weight:"<< grapes.getWeight() << " quantity:" << grapes.getQuantity()
<< " was it purchased?:"<< boolalpha << grapes.getIs_purchased() << '\n';
cout << "\nThis ends the class design, implementation, test program. Goodbye!\n";
return EXIT_SUCCESS;
} // end of main
|