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
|
int main()
{
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\n";
/*
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.*/
constexpr size_t maxObj {5};
size_t no_fruits {};
do {
cout << "How many objects do you want? (1 - " << maxObj << "): ";
cin >> no_fruits;
cin.clear();
} while ((!cin || (no_fruits < 1 || no_fruits > maxObj)) && (cout << "Invalid input! Please enter a number (1-5)..\n") && cin.ignore(1000, '\n'));
// cout<<"You created "<< counter<<" objects:\n# 1 of "
// << limit << " objects: "<< endl;
cout << "\nYou created " << no_fruits << " objects:" << endl<<endl;
cout <<"# 1 of "
<< no_fruits << " objects:\n";
//vector <type> name (size)
vector<Fruit> fruits(no_fruits);
for (size_t f = 0; f < no_fruits; ++f) { // will only loop once
// fruit 1 - set's method
Fruit fruit1;
fruit1.set_Name("Apple ");
fruit1.set_Quantity(4);
fruit1.set_Weight(4.56);
fruit1.set_Price(4);
fruit1.set_Is_purchased(true);
fruit1.print(); // <----
// fruit 2 - constructor method
cout<<"\n# 2 of " << no_fruits<< " objects: "<< endl;
Fruit fruit2("Orange", 7, 2.65, 5, true);
fruit2.print(); // <--
// fruit 3 - constructor method
cout<<"\n# 3 of "<< no_fruits<<" objects: "<< endl;
Fruit fruit3("Banana", 4, 2.34, 2, false);
fruit3.print(); // <--
// fruit 4 - constructor method
cout<<"\n# 4 of "<< no_fruits<<" objects: "<< endl;
Fruit fruit4("Avocado", 16, 2.59, -6, false);
fruit4.print(); // <--
// fruit 5 - constructor method
cout<<"\n# 5 of "<< no_fruits<<" objects: "<< endl;
Fruit fruit5("Grapes", -7, -2.65, 5, false);
fruit5.print(); // <--
cout
<< "\nThis ends the class design, implementation, test program. Goodbye!"<< endl;
return EXIT_SUCCESS;
} // end of main
|