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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <iostream>
#include <string.h>
const size_t j=2;
const size_t k=4;
int GoodSum, QuantitySum;
std::string fruitNameList[j] = {"Apple", "Banana"}, vegNameList[j] = {"Broccoli", "Lettuce"};
int priceFruitList[j] = {10, 10},quantityFruitList[j] = {7, 8}, priceVegList[j]= {60, 50}, quantityVegList[j]= {12, 10};
class Fruit
{
public:
std::string fruitName;
int fruitPrice;
int fruitQuantity;
//Constructor
Fruit(std::string newFruitName = "", int newFruitPrice = 0, int newFruitQuantity = 0)
{
fruitName = std::move(newFruitName);
fruitPrice = newFruitPrice;
fruitQuantity = newFruitQuantity;
//std::cout << "Constructor Called." << std:: endl; // To check if its working
}
//Deconstructor
~Fruit ()
{
//std::cout << "Destructor Called." << std::endl; // To check if its working
}
//Copy Constructor
Fruit (const Fruit ©Fruit)
{
//std::cout << "Copy Constructor Called." << std::endl; // To check if its working
fruitName = copyFruit.fruitName;
fruitPrice = copyFruit.fruitPrice;
fruitQuantity = copyFruit.fruitQuantity;
}
//Copy Assignment Operator
Fruit &operator = (const Fruit ©)
{
//std::cout << "Copy Assignment Operator was called." << std::endl;
return *this;
}
//Display Attributes
void printFruitDetails ()
{
if (fruitName.size()>=8)
{
std::cout <<"\t"<< this->fruitName << "\t " << this->fruitPrice << "\t " << this->fruitQuantity << std::endl;
}
else
{
std::cout <<"\t"<< this->fruitName << "\t\t " << this->fruitPrice << "\t " << this->fruitQuantity << std::endl;
}
}
};
class Vegetable
{
public:
std::string vegName;
int vegPrice;
int vegQuantity;
//Constructor
Vegetable (std::string newVegName = "Broccoli", int newVegPrice = 60, int newVegQuantity = 12)
{
vegName = std::move(newVegName);
vegPrice = newVegPrice;
vegQuantity = newVegQuantity;
//std::cout<< "Constructor Called." << std::endl; // To check if its working
}
//Deconstructor
~Vegetable ()
{
//std::cout << "Destructor Called." << std::endl; // To check if its working
}
//Copy Constructor
Vegetable (const Vegetable ©Vegetable)
{
//std::cout << "Copy Constructor Called." << std::endl;
vegName = copyVegetable.vegName;
vegPrice = copyVegetable.vegPrice;
vegQuantity = copyVegetable.vegQuantity;
}
// Copy Assignment Operator
Vegetable &operator = (const Vegetable ©)
{
//std::cout << "Copy Assignment Operator was called." << std::endl;
return *this;
}
//Display Attributes
void printVegDetails()
{
if (vegName.size()>=8)
{
std::cout <<"\t"<< this->vegName << "\t " << this->vegPrice << "\t\t " << this->vegQuantity << std::endl;
}
else
{
std::cout <<"\t"<< this->vegName << "\t\t " << this->vegPrice << "\t " << this->vegQuantity << std::endl;
}
}
};
class Grocery : public Vegetable, public Fruit
{
public:
std::string GoodName;
int GoodPrice, GoodQuantity;
Grocery (std::string GoodNamey="", int GoodPricey=0, int GoodQuantityy=0)
{
GoodName=std::move(GoodNamey);
GoodPrice=GoodPricey;
GoodQuantity=GoodQuantityy;
}
~Grocery(){};
Grocery (const Grocery ©Good)
{
GoodName = copyGood.GoodName;
GoodPrice = copyGood.GoodPrice;
GoodQuantity = copyGood.GoodQuantity;
}
void printGroceryDetails()
{
if (GoodName.size()>=8)
{
std::cout <<"\t"<< this->GoodName << "\t " << this->GoodPrice << "\t\t " << this->GoodQuantity << std::endl;
}
else
{
std::cout <<"\t"<< this->GoodName << "\t\t " << this->GoodPrice << "\t\t " << this->GoodQuantity << std::endl;
}
}
};
Fruit *fruitlist_fin[j];
Vegetable *veglist_fin[j];
// Function to sum
void TotalSum(int x, int y);
void StoreData(int i);
int main ()
{
// driver program
std::cout <<"\t"<< "Good(s)" << "\t\t " << "Price(php)" << "\t " << "Quantity" << std::endl;
for(int i =0; i < j; i++) //Store to Fruit and vegetable List
{
StoreData(i);
}
Grocery *GroceryList_fin[k];
for(int i = 0; i < 2; i++)
{
GroceryList_fin[i]=new Grocery (veglist_fin[i]->vegName,veglist_fin[i]->vegPrice, veglist_fin[i]->vegQuantity);
GroceryList_fin[i+2]=new Grocery (fruitlist_fin[i]->fruitName,fruitlist_fin[i]->fruitPrice, fruitlist_fin[i]->fruitQuantity);
}
for(int i = 0; i < k; i++)
{
GroceryList_fin[i]->printGroceryDetails();
TotalSum(GroceryList_fin[i]->GoodPrice,GroceryList_fin[i]->GoodQuantity);
}
std::cout<<"\n\tTotal:\t\t "<<GoodSum<<"\t\t "<<QuantitySum<<"\n\n";
delete[] GroceryList_fin;
//GroceryList_fin[0] = NULL;
for(int i = 0; i < k; i++)
{
GroceryList_fin[i]->printGroceryDetails();
}
std::cout<<"\n\tTotal:\t\t "<<GoodSum<<"\t\t "<<QuantitySum<<"\n\n";
}
void TotalSum(int x, int y) {
GoodSum=GoodSum+(x*y);
QuantitySum=QuantitySum+y;
}
void StoreData(int i) {
fruitlist_fin[i] = new Fruit (fruitNameList[i], priceFruitList[i], quantityFruitList[i]);
veglist_fin[i] = new Vegetable (vegNameList[i], priceVegList[i], quantityVegList[i]);
}
|