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
|
#include <iostream>
#include <string>
class RetailItem
{
private:
std::string description;
int unitsOnHand;
double price;
public:
// if we dont supply parameters
RetailItem() {
description = "<Empty>";
unitsOnHand = 0;
price = 0;
}
// when we do.
RetailItem(std::string desc, int qty, double cost) {
description = desc;
unitsOnHand = qty;
price = cost;
}
// Getters
std::string getDescription() { return description; }
int getUnits() { return unitsOnHand; }
double getPrice() { return price; }
// Setters
void SetDescription(std::string desc) { description = desc; }
void SetUnits(int qty) { unitsOnHand = qty; }
void SetPrice(double cost) { price = cost; }
};
int main()
{
// creating this way creates three objects with
// default data, you then need to use the setters
// to set the data.
RetailItem Items[3];
// like this...
Items[0].SetDescription("Jacket");
Items[0].SetUnits(12);
Items[0].SetPrice(59.95);
// items[1].. items[2] etc.
// or we can use the constructor to assign data.
RetailItem Item1("Jacket", 12, 59.95);
// ok so lets read the data we assigned using setters
for (int i = 0; i <= 2; i++)
std::cout << "Item: " << Items[i].getDescription() << ", Qty: " <<
Items[i].getUnits() << ", Price: " << Items[i].getPrice() << std::endl;
// display data we entered from the constructor
std::cout << std::endl << "Item: " << Item1.getDescription() << ", Qty: " <<
Item1.getUnits() << ", Price: " << Item1.getPrice() << std::endl;
return 0;
}
|
Item: Jacket, Qty: 12, Price: 59.95
Item: <Empty>, Qty: 0, Price: 0
Item: <Empty>, Qty: 0, Price: 0
Item: Jacket, Qty: 12, Price: 59.95
|