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
|
#include <iostream>
#include <string>
using namespace std;
class Supply
{
public:
Supply()
{
}
virtual ~Supply()
{
}
virtual void Print() = 0;
};
class Pen : public Supply
{
public:
Pen()
: Supply()
{
}
Pen(const string description, const int stockNumber, const int quantity, const int price, const int penColor, const int inkColor)
: m_Description(description)
, m_StockNumber(stockNumber)
, m_Quantity(quantity)
, m_Price(price)
, m_PenColor(penColor)
, m_InkColor(inkColor)
{
}
virtual ~Pen()
{
}
void Print()
{
cout << endl << m_Description << endl;
}
protected:
string m_Description;
int m_StockNumber;
int m_Quantity;
int m_Price;
int m_PenColor;
int m_InkColor;
};
class MemPen : public Pen
{
public:
MemPen()
: Pen()
{
}
MemPen(const string description, const int stockNumber, const int quantity, const int price, const int penColor, const int inkColor, const int memory)
: Pen(description, stockNumber, quantity, price, penColor, inkColor)
, m_Memory(memory)
{
}
virtual ~MemPen()
{
}
protected:
int m_Memory;
};
int main()
{
int array_size;
cout << "Please enter the size of the array: " << endl;
cin >> array_size;
if(array_size >= 8)
{
cout << "Invalid array size, please enter a valid integer size";
cin >> array_size;
}
Supply** DynamicArray = new Supply*[array_size];
//
//initialize all elements in the array to the null pointer
//
for (int i = 0; i <= array_size; i++)
{
DynamicArray[i] = NULL;
}
string description, des2;
int stockNumber, price, quantity, nPenColor, nInkColor, memory;
//
//store pointer to Pen object p1 in [0]
cout << endl;
cin.ignore();
cout << "Enter Name of pen: ";
getline(cin, description);
cout << description;
cin.clear();
cout << "Enter stock number: ";
cin >> stockNumber;
cout << "Enter quantity: ";
cin >> quantity;
cout << "Enter price: ";
cin >> price;
cout << "Enter pen color (1,2,3): ";
cin >> nPenColor;
cout << "Enter ink color (1,2,3): ";
cin >> nInkColor;
DynamicArray[3] = new Pen(description, stockNumber, quantity, price, nPenColor, nInkColor);
//store pointer to MemPen obect mp1 in [2]
//
cout << description;
cout << endl;
description = " ";
cout << "Test" << endl;
cout << des2;
cin.ignore();
cout << "Enter Name of pen: ";
getline(cin,description);
cout << description;
cin.ignore(1000,'\n');
// description.erase(std::remove(description.begin(),description.end(), '\n'), description.end());
cout << "Enter stock number: ";
cin >> stockNumber;
cout << "Enter quantity: ";
cin >> quantity;
cout << "Enter price: "; cin >> price;
cout << "Enter pen color (1,2,3): ";
cin >> nPenColor;
cout << "Enter ink color (1,2,3): ";
cin >> nInkColor;
cout << "Enter memory size in Mb: ";
cin >> memory;
//
//loop to print out ALL elements in the array that point to an object,
//that is, there is a valid address in the element of the array.
DynamicArray[array_size-1] = new MemPen(description, stockNumber, quantity, price, nPenColor, nInkColor, memory);
cout << "Supply List" <<endl;
cout << "===========" <<endl;
for (int x = 0; x < array_size; x++)
{
if (DynamicArray[x])
{
(DynamicArray[x])->Print();
}
}
// don't forget to release memory:
for (int x = 0; x < array_size; x++)
{
if (DynamicArray[x])
{
delete DynamicArray[x];
DynamicArray[x] = NULL;
}
}
delete *DynamicArray;
*DynamicArray = NULL;
}
|