Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. //assortment.h
  2.  
  3.  
  4. #pragma once
  5. #include "product.h"
  6.  
  7. class Assortment
  8. {
  9. private:
  10. int arraySize;
  11. int numberOfElements;
  12. Product *myArray;
  13.  
  14. void initialize( int from )
  15. {
  16. for ( size_t i = from; i < arraySize; i++ )
  17. {
  18. myArray[i] = Product( );
  19. }
  20. }
  21.  
  22. void expand( )
  23. {
  24. arraySize = arraySize * 2;
  25.  
  26. Product *tempArray = new Product[arraySize];
  27.  
  28. for ( size_t i = 0; i < numberOfElements; i++ )
  29. {
  30. tempArray[i] = myArray[i];
  31. }
  32.  
  33. delete[]myArray;
  34.  
  35. myArray = tempArray;
  36.  
  37. initialize( numberOfElements );
  38. }
  39.  
  40.  
  41.  
  42. public:
  43. Assortment( )
  44. {
  45. arraySize = 10;
  46. numberOfElements = 0;
  47. myArray = new Product[arraySize];
  48. }
  49.  
  50. ~Assortment( )
  51. {
  52. delete[]myArray;
  53. }
  54.  
  55. void add( Product newProduct );
  56.  
  57. void printAssortment( );
  58.  
  59. int getNumberOfElements( );
  60.  
  61. //Product getAt( int index );
  62. };
  63.  
  64.  
  65.  
  66. //assortment.cpp
  67.  
  68. #include "stdafx.h"
  69. #include "assortment.h"
  70.  
  71. #include <iostream>
  72.  
  73.  
  74. void Assortment::add(Product newProduct)
  75. {
  76. if (numberOfElements >= arraySize)
  77. {
  78. expand();
  79. }
  80.  
  81. myArray[numberOfElements++] = newProduct;
  82. }
  83.  
  84.  
  85. void Assortment::printAssortment( )
  86. {
  87. for ( int i = 0; i < numberOfElements; i++ )
  88. {
  89. myArray[i].print();
  90. std::cout << std::endl;
  91. }
  92. }
  93.  
  94. int Assortment::getNumberOfElements( )
  95. {
  96. return numberOfElements;
  97. }
  98.  
  99.  
  100. /*
  101. Product getAt( int index )
  102. {
  103. if (index < 0 || index >= numberOfElements)
  104. {
  105. throw("Out of bounds exception!");
  106. }
  107.  
  108. return myArray[index];
  109. }
  110. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement