Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //assortment.h
- #pragma once
- #include "product.h"
- class Assortment
- {
- private:
- int arraySize;
- int numberOfElements;
- Product *myArray;
- void initialize( int from )
- {
- for ( size_t i = from; i < arraySize; i++ )
- {
- myArray[i] = Product( );
- }
- }
- void expand( )
- {
- arraySize = arraySize * 2;
- Product *tempArray = new Product[arraySize];
- for ( size_t i = 0; i < numberOfElements; i++ )
- {
- tempArray[i] = myArray[i];
- }
- delete[]myArray;
- myArray = tempArray;
- initialize( numberOfElements );
- }
- public:
- Assortment( )
- {
- arraySize = 10;
- numberOfElements = 0;
- myArray = new Product[arraySize];
- }
- ~Assortment( )
- {
- delete[]myArray;
- }
- void add( Product newProduct );
- void printAssortment( );
- int getNumberOfElements( );
- //Product getAt( int index );
- };
- //assortment.cpp
- #include "stdafx.h"
- #include "assortment.h"
- #include <iostream>
- void Assortment::add(Product newProduct)
- {
- if (numberOfElements >= arraySize)
- {
- expand();
- }
- myArray[numberOfElements++] = newProduct;
- }
- void Assortment::printAssortment( )
- {
- for ( int i = 0; i < numberOfElements; i++ )
- {
- myArray[i].print();
- std::cout << std::endl;
- }
- }
- int Assortment::getNumberOfElements( )
- {
- return numberOfElements;
- }
- /*
- Product getAt( int index )
- {
- if (index < 0 || index >= numberOfElements)
- {
- throw("Out of bounds exception!");
- }
- return myArray[index];
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement