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
|
#include <iostream>
#include <string>
#include <utility>
#include <vector>
class Fruit {
private:
std::string fruitName;
int fruitPrice {};
int fruitQuantity {};
public:
Fruit(std::string newFruitName = "", int newFruitPrice = 0, int newFruitQuantity = 0) :
fruitName(std::move(newFruitName)), fruitPrice(newFruitPrice), fruitQuantity(newFruitQuantity) {}
~Fruit() {}
Fruit(const Fruit& copyFruit) : Fruit(copyFruit.fruitName, copyFruit.fruitPrice, copyFruit.fruitQuantity) {}
Fruit(Fruit&& copyFruit) noexcept : Fruit(std::move(copyFruit.fruitName), copyFruit.fruitPrice, copyFruit.fruitQuantity) {}
Fruit& operator=(Fruit f) noexcept {
std::swap(f.fruitName, fruitName);
std::swap(f.fruitPrice, fruitPrice);
std::swap(f.fruitQuantity, fruitQuantity);
return *this;
}
void printFruitDetails() const {
std::cout << "\t" << this->fruitName << "\t\t " << this->fruitPrice << "\t " << this->fruitQuantity << std::endl;
}
};
class Vegetable {
private:
std::string vegName;
int vegPrice {};
int vegQuantity {};
public:
Vegetable(std::string newVegName = "Broccoli", int newVegPrice = 60, int newVegQuantity = 12) :
vegName(std::move(newVegName)), vegPrice(newVegPrice), vegQuantity(newVegQuantity) {}
~Vegetable() {}
Vegetable(const Vegetable& copyVegetable) : Vegetable(copyVegetable.vegName, copyVegetable.vegPrice, copyVegetable.vegQuantity) {}
Vegetable(Vegetable&& copyVegetable) noexcept : Vegetable(std::move(copyVegetable.vegName), copyVegetable.vegPrice, copyVegetable.vegQuantity) {}
Vegetable& operator=(Vegetable copy) noexcept {
std::swap(copy.vegName, vegName);
std::swap(copy.vegPrice, vegPrice);
std::swap(copy.vegQuantity, vegQuantity);
return *this;
}
void printVegDetails() const {
std::cout << "\t" << this->vegName << "\t\t " << this->vegPrice << "\t " << this->vegQuantity << std::endl;
}
};
/*
class Grocery : public Vegetable, public Fruit {
private:
int GrocList_count {};
std::string GoodName;
int GoodPrice {}, GoodQuantity {};
public:
Grocery(std::string GoodNamey = "", int GoodPricey = 0, int GoodQuantityy = 0) {
GoodName = std::move(GoodNamey);
GoodPrice = GoodPricey;
GoodQuantity = GoodQuantityy;
}
~Grocery() {};
Grocery(const Grocery& copyGood) {
GoodName = copyGood.GoodName;
GoodPrice = copyGood.GoodPrice;
GoodQuantity = copyGood.GoodQuantity;
}
void printGroceryDetails() {
std::cout << "\t" << this->GoodName << "\t\t " << this->GoodPrice << "\t " << this->GoodQuantity << std::endl;
}
};
*/
int main() {
const size_t j { 2 };
//const size_t k { 4 };
std::cout << "\t" << "Good(s)" << "\t\t " << "Price" << "\t " << "Quantity\n";
const std::string fruitNameList[j] { "Apple", "Banana" };
const int priceFruitList[j] { 10, 10 };
const int quantityFruitList[j] { 7, 8 };
std::vector<Fruit> fruitlist_fin;
for (int i {}; i < j; ++i)
fruitlist_fin.emplace_back(fruitNameList[i], priceFruitList[i], quantityFruitList[i]);
for (const auto& f : fruitlist_fin)
f.printFruitDetails();
const std::string vegNameList[j] { "Beans", "Lettuce" };
const int priceVegList[j] { 60, 50 };
const int quantityVegList[j] { 12, 10 };
std::vector<Vegetable> veglist_fin;
for (int i {}; i < j; ++i)
veglist_fin.emplace_back(vegNameList[i], priceVegList[i], quantityVegList[i]);
for (const auto& v : veglist_fin)
v.printVegDetails();
/*
Grocery GroceryList_fin[k] {};
for (int i = 0; i < j; i++)
veglist_fin[i] = GroceryList_fin[i];
for (int i = 0; i < j; i++)
fruitlist_fin[i] = GroceryList_fin[i + 3];
for (int i = 0; i < k; i++)
GroceryList_fin[i].printGroceryDetails();
*/
}
| |