working on school project that uses one class object constructor to call another to make an array of objects and store data.. tried a forloop that displays correctly from inside the loop but not outside..
here's the header..
#ifndef PROG4CLASSESANDFUNCTS_H
#define PROG4CLASSESANDFUNCTS_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <math.h>
#include <windows.h>
#include <string>
#include <cstdlib>
usingnamespace std;
//********************************************************************
//CLASSES
//********************************************************************
//************************************************************
//
//************************************************************
//********************************************************************
//Class InvBin
//********************************************************************
class InvBin
{
private:
string description; // item name
int qty; // Quantity of items
// in this bin
public:
InvBin (string d , int q );//2-parameter constructor
//with default values
// It will also have the following public member functions. They
//will be used by the BinManager class, not the client program.
void setDescription( string d );
string getDescription();
void setQty ( int q );
int getQty();
};
//********************************************************************
//Class BinManager
//********************************************************************
class BinManager
{
private:
InvBin bin[30]; //array of InvBin objects
int numBins; // number of bins
//currently in use
public:
BinManager(); // default constructor
BinManager(int size, string d[], int q[]);
// the class will also have the following public member functions:
string getDescription(int index); //returns name of one item
int getQuantity(int index) ; // returns qty of one item
string DisplayAllBins() ; //returns string having one line
//for each item
bool addParts(int binIndex, int q); //these returns true if the
bool removeParts(int binIndex, int q); //action was done and false
// if it could not be done-
//see validation information
};
#endif
//********************************************************************
//Class BinManager
//********************************************************************
BinManager::BinManager() // default constructor
{ numBins = 0;}
BinManager::BinManager(int size, string d[ ], int q[ ])
{
// 3-Parameter constructor:: Recieves number of
//bins in use and parallelarrays of item names and
//quantities. Uses this info. to store values in the
//elements of the bin array. Remember, these elements
//are InvBin objects.
int index;
cout<<"constructor"<<endl;
for( index=0 ; index < size ; index++ )
{
InvBin bin[index](d[index], q[index]);
cout<<fixed<<left<<index +1<<" ";
cout<<setw(20)<<bin[index].getDescription();
cout<<" "<<bin[index].getQty()<<endl;
}
Sleep(2000);
}
and here's the BinManager member function for displaying all bins..
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string BinManager::DisplayAllBins()
{
int i;
cout<< "Current Inventory: "<<endl;
cout<<"still not working"<<endl;
for (i=0; i<9;i++)
{
cout<<fixed<<left<<i<<" "<<setw(20);
cout<<bin[i].getDescription();
cout<<" "<<bin[i].getQty()<<endl;
}
Sleep(4000);
return 0 ;
};
as you can see by the comments..
1 2 3 4 5
// 3-Parameter constructor:: Recieves number of
//bins in use and parallelarrays of item names and
//quantities. Uses this info. to store values in the
//elements of the bin array. Remember, these elements
//are InvBin objects.
this last part was taken straight from the book, as was the header file.. so I CAN'T use a vector, nor can I use pointers, since we haven't covered that yet..(yes I'm aware of them but haven't practiced with them yet).. i know that because it's a forloop that local data isn't stored once it ends, so how do i get it to save? i get the feeling i'm doing something stupid that i should SEE is wrong but i don't see it... HELP!
That's too bad that you can't use vectors. Still the problem is the same as yesterday. In BinManager's constructor you have: InvBin bin[index](d[index], q[index]);
This doesn't call the constructor for the class-member bin. It creates a local version of bin which is destroyed at the end of each for loop iteration.
Since Bin is created in the declaration of BinManager, I don't think you can apply another constructor to it (a default constructor is called which doesn't do anything). Use a function instead:
To declare:
1 2 3 4 5 6 7 8 9 10 11 12 13
class InvBin
{
private:
string description;
int qty;
public:
void init(string d , int q ); // void init() replaces the constructor
void setDescription( string d );
string getDescription();
void setQty ( int q );
int getQty();
};
Then to call:
1 2 3 4 5 6 7 8 9 10 11 12 13
BinManager::BinManager(int size, string d[ ], int q[ ])
{
int index;
cout<<"constructor"<<endl;
for( index=0 ; index < size ; index++ )
{
bin[index].init(d[index], q[index]); // We call the init function instead of a constructor.
cout<<fixed<<left<<index +1<<" ";
cout<<setw(20)<<bin[index].getDescription();
cout<<" "<<bin[index].getQty()<<endl;
}
Sleep(2000);
}