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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
// Implementation file (.cpp) for class CDX
#include "CDX.h"
#include <iostream>
using namespace std;
CDX::CDX() //Constructor
{
Sig = "No Sig";
Type = "A";
Price = 0.00;
};
void CDX::LoadInfo() //Prompts, validates and sets data members
{
cout << "Enter CD Sig: ";
getline(cin, Sig);
int check=0;
do
{ cout << "1-A\n2-B\n3-C\n4-D\n5-E\n6-F" << endl;
cout << "Enter the number that corresponds with the CD's Type: ";
cin >> check;
} while ((check <1)||(check>6));
switch(check)
{
case 1: Type = "A";
break;
case 2: Type = "B";
break;
case 3: Type = "C";
break;
case 4: Type = "D";
break;
case 5: Type = "E";
break;
case 6: Type = "F";
break;
default:
cout << "How in the world did you get here?!";
}
float newPrice;
cout << "Input CD Price: ";
cin >> newPrice;
if ((newPrice > 0.0))
{
Price = newPrice;
}
else
{
cout << "Invalid amount entered - Price not changed.\n";
}
};
float CDX::ReturnPrice() //return Price
{
return Price;
};
void CDX::DisplayInfo() //Displays data members
{
cout << "CD Sig: " << Sig << endl;
cout << "CD Type: " << Type << endl;
cout << "CD Price: " << Price << endl;
};
void CDX::ChangeData() //Changes existing data members
{
char response;
cout << "Current CD Sig: " << Sig << endl;
cout << "Would you like to change the Sig? Enter Y or N: ";
cin >> response;
if ((response == 'Y')||(response == 'y'))
{
cout << "Please enter new CD Sig: " << endl;
getline(cin, Sig);
}
else
{
cout << "Sig left unchanged." << endl;
}
cout << "Current CD Type:" << Type << endl;
cout << "Would you like to change the type? Enter Y or N: ";
cin >> response;
if ((response == 'Y')||(response == 'y'))
{
int check=0;
do
{
cout << "1-A\n2-B\n3-C\n4-D\n5-E\n6-F" << endl;
cout << "Enter the number that corresponds with the CD's Type: ";
cin >> check;
} while ((check<1)||(check>6));
switch(check)
{
case 1: Type = "A";
break;
case 2: Type = "B";
break;
case 3: Type = "C";
break;
case 4: Type = "D";
break;
case 5: Type = "E";
break;
case 6: Type = "F";
break;
default:
cout << "You shouldn't be here.";
}
}
else
{
cout << "CD Type unchanged." << endl;
}
cout << "Current CD Price: " << Price << endl;
cout << "Would you like to change the Price? Y or N: " << endl;
cin >> response;
float cdPrice;
if ((response == 'Y')||(response == 'y'))
{
cout << "Enter new Price: " << endl;
cin >> cdPrice;
if ((cdPrice > 0.00f))
{
Price = cdPrice;
}
else
{
cout << "Invalid amount entered - Price left unchanged." << endl;
}
}
else
{
cout << "Price left unchanged." << endl;
}
};
//Implementations for free functions
#include "CDXFun.h"
#include <iostream>
using namespace std;
void ShowCDX(CDX *pCD1, CDX *pCD2, CDX *pCD3)
{
if (pCD1 = 0)
{
pCD1 -> DisplayInfo();
}
else
{
cout << "No information found." << endl;
}
if (pCD2 = 0)
{
pCD2 -> DisplayInfo();
}
else
{
cout << "No information found." << endl;
}
if (pCD3 = 0)
{
pCD3 -> DisplayInfo();
}
else
{
cout << "No information found." << endl;
}
};
void ShowPrice(CDROM &CD1, CDROM &CD2, CDROM &CD3)
{
float totalPrice;
totalPrice = CD1.ReturnPrice()+CD2.ReturnPrice()+CD3.ReturnPrice();
cout << "Total Price: " << totalPrice << endl;
cout << "Average Price: " << (totalPrice/3) << endl;
};
void MemoryReturn(CDX *pCD1, CDX *pCD2,CDX *pCD3)
{
delete pCD1;
delete pCD2;
delete pCD3;
};
//Main
#include "CDXFun.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main ()
{
CDX *pCD1, *pCD2, *pCD3;
pCD1 = new CDX;
if ( 0 == pCD1)
{
cout << "Error: Dynamic Memory Allocation failed. Exiting.";
exit(1);
}
pCD2 = new CDX;
if ( 0 == pCD2 )
{
delete pCD1;
cout << "Dynamic Memory Allocation failed. Exiting.";
exit(1);
}
pCD3 = new CDX;
if ( 0 == pCD3 )
{
delete pCD1;
delete pCD2;
cout << "Dynamic Memory Allocation failed. Exiting.";
exit(1);
}
pCD1->LoadInfo();
pCD2->LoadInfo();
pCD3->LoadInfo();
void DisplayCDS(CDX *pCD1, CDX *pCD2, CDX *pCD3);
void ShowPrices(CDX &CD1, CDX &CD2, CDX &CD3);
pCD1->ChangeData();
pCD2->ChangeData();
void DisplayCDS(CDX *pCD1, CDX *pCD2, CDX *pCD3);
void ShowPrices(CDX &CD1, CDX &CD2, CDX &CD3);
void MemoryReturn(CDX *pCD1, CDX *pCD2, CDX *pCD3);
return 0;
};
|