evetsleep

ConvertMBtoByte

Nov 16th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class ConvertByte
  7. {
  8. public:
  9.     //Defining the default constructor
  10.     ConvertByte();
  11.  
  12.     //Prompts the user to enter a value in MB.
  13.     void read();
  14.  
  15.     //Mutator which converts the value entered from MB
  16.     //into bytes.
  17.     void convert_to_byte();
  18.  
  19.     //Accessor which outputs the value that is stored
  20.     //in byte_result.
  21.     void print();
  22.  
  23.     //Destructor used to remove an object that was
  24.     //created.
  25.     ~ConvertByte();
  26.  
  27. private:
  28.     unsigned long int mb_private;
  29.     unsigned long int byte_result;
  30.     unsigned long int MB_in_bytes;
  31. };
  32.  
  33. //Default constructor which also sets the default
  34. //value for MB_in_bytes (now many bytes make up
  35. // a megabyte.
  36. ConvertByte::ConvertByte()
  37. {
  38.     MB_in_bytes = 1048576;
  39. }
  40.  
  41. //Prompts the user for the MB to convert and
  42. //stores the input in the private interface
  43. //variable mb_private.
  44. void ConvertByte::read()
  45. {
  46.     cout << "What number, in megabytes, would you like to convert to bytes? ";
  47.     cin >> mb_private;
  48. }
  49.  
  50. //Mutator which takes the value stored in mb_private
  51. //and converts into bytes and stores that result in
  52. //the private interface variable byte_result.
  53. void ConvertByte::convert_to_byte()
  54. {
  55.     byte_result = mb_private * MB_in_bytes;
  56. }
  57.  
  58. //Sends the result of converting the megabyte number entered
  59. //to stout.
  60. void ConvertByte::print()
  61. {
  62.     cout << mb_private << " MB is equal to " << byte_result << " bytes.";
  63. }
  64.  
  65. //Destrutor used to remove the object.
  66. ConvertByte::~ConvertByte(){}
  67.  
  68. int main ()
  69. {
  70.     //Create byteObject
  71.     ConvertByte byteObject;
  72.  
  73.     /*
  74.     Read in the number of megabytes to convert
  75.     then convert it to bytes and then print
  76.     the result to screen.
  77.     */
  78.     byteObject.read();
  79.     byteObject.convert_to_byte();
  80.     byteObject.print();
  81.  
  82.     //Use a destructor to destroy the object.
  83.     byteObject.~ConvertByte();
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment