Help with a "Has a" relationship

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Help with a "Has a" relationship

Postby brb00136 » Wed Dec 21, 2011 11:56 pm

Hi all, I have started this project but cannot figure out how to populate the loadfunction member function to populate the arrays and pass the information along... Can someone help?

Code: Select all
#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");
}
brb00136
 
Posts: 3
Joined: Thu Dec 01, 2011 3:48 pm

Return to Homeworks

Who is online

Users browsing this forum: No registered users and 1 guest

cron