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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <cassert>
#include <cstdlib>
using namespace std;
//DVD Header file
class DVD {
public:
DVD();
void Print();
void LoadInformation();
float getCost()
{ return cost;
}
std::string getName()
{ return name;
}
Time getTime()
{ return length;
}
private:
std::string name;
float cost;
Time length;
};
// Time class definition
class Time
{
public:
Time(int=0, int=0, int=0); // constructor
void setTime( int, int, int ); // set hour, minute and second
void printUniversal(); // print time in universal-time format
void printStandard(); // print time in standard-time format
std::string toString(); // Return the Time object as a string
private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
}; // end class Time
//DVD Implementation
DVD::DVD() : cost(0.0),
name("None")
{
}
//whats missing? Length- Time object
void DVD::LoadInformation() {
//needs a dvd pointer
//needs a for loop for inputing each -
// To do, populate the DVD from the user
}
//implemented print function for us this couts objects
void DVD::Print() {
std::cout << " Name : " << name << std::endl;
std::cout << " Cost : " << cost << std::endl;
std::cout << "Length : " << length.toString() << std::endl;
}
// Time constructor initializes each data member to zero.
// Ensures all Time objects start in a consistent state.
Time::Time(int h, int m, int s)
{
setTime(h, m, s);
} // end Time constructor
// set new Time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
void Time::setTime( int h, int m, int s )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour
minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute
second = ( s >= 0 && s < 60 ) ? s : 0; // validate second
} // end function setTime
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal()
{
cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) << minute << ":" << setw( 2 ) << second;
} // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":"
<< setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 )
<< second << ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
// Return time values as a string to the caller
std::string Time::toString()
{
char buffer[32];
sprintf(buffer, "%02d:%02d:%02d", hour, minute, second);
// constructs a string that looks like hours minutes seconds
std::string theReturnedStr(buffer);
return theReturnedStr;
}
//MAIN
const int NUMDVDS = 1;
// Free function to loop over the array of dvd pointers to
// print each DVD.
void DisplayDVDs(DVD* dvdArr[]) {
for (int i = 0; i < NUMDVDS; i++)
dvdArr[i]->Print();
}
// To Do, add function to GetDVDsFromUser
// Main function to begin all processing.
int main() {
// Dynamically create the first DVD object
DVD* dvd1 = new DVD;
if (NULL == dvd1)
{
std::cerr << "Unable to create dvd1, aborting!" << std::endl;
}
DVD* dvd2 = new DVD;
if (NULL == dvd2)
{
std::cerr << "Unable to create dvd2, aborting!" << std::endl;
}
DVD* dvd3 = new DVD;
if (NULL == dvd3)
{
std::cerr << "Unable to create dvd2, aborting!" << std::endl;
}
// Populate the array of pointers
DVD* dvdArr[NUMDVDS];
dvdArr[0] = dvd1;
dvdArr[1] = dvd2;
dvdArr[2] = dvd3;
//hide cins in load information of dvd class- and have it loop
// Use the array of pointers to pass to free functions
DisplayDVDs(dvdArr);
//once thats done call diplay dvds takes an array of pointers, for each dvd- print yourself!
// Delete each indivual DVD, reclaiming its memory
delete [] dvdArr;
//delete dvdArr[0];
//delete dvdArr[1];
//delete dvdArr[2];
system("pause");
}
| |