What I'm really writing is a basic test inventory program. i just tried to dynamically cast but its not right
Inventory.h:
Code:
#include "InventoryItem.h"
#include "String.h"
using namespace std;
class Inventory
{
private:
int N;
InventoryItem* pI[1000];
public:
Inventory(): N(0){};
~Inventory(){
for(int i = 0; i < N; i++)
{delete pI[i];}
}
void createBook(String t, float c, int oH, String aN, String d){
pI[N] = new Book(t, c, oH, aN, d);
pI[N] = dynamic_cast<Book*>(pI[N]);
N++;
}
void createTape(String t, float c, int oH, float pT){
pI[N] = new Tape(t, c, oH, pT);
pI[N] = dynamic_cast<Tape*>(pI[N]);
N++;
}
void createCd(String t, float c, int oH, int nT, float pT){
pI[N] = new Cd(t, c, oH, nT, pT);
pI[N] = dynamic_cast<Cd*>(pI[N]);
N++;
}
void addItem();
void receiveOrder();
void report();
void sell();
void sellPrice();
int getN(){return N;}
};
Small part of Inventory.cpp
Code:
void Inventory::sell()
{
int temp;
cout << "Enter in the item number you wish to sell: ";
cin >> temp;
if ( temp > N ) {cout << "Invalid Item number";}
else
{
int quantity;
cout << "How many do you wish to sell: ";
cin >> quantity;
if (quantity > pI[temp]->onHand) // any sort of call like pI[temp]->onHand will not work ***compiler error here***
{cout<<"Unable to sell: " << quantity - onHand << " too many to sell!"<<endl;}
else
{
float temp = pI[temp]->sellPrice() * quantity * 1.0725F;
pI[temp]->onHand -= quantity; //
cout << quantity << " Have been sold for " << temp << endl;
}//end 2nd else
}//end 1st else
}
InventoryItem.h :
Code:
#ifndef InventoryItem_H
#define InventoryItem_H
#include "String.h"
using namespace std;
class InventoryItem
{
public:
//virtual void receiveOrder() = 0;
virtual float sellPrice() = 0;
};
class Book : public InventoryItem
{
private:
String title;
float cost;
int onHand;
String authorName;
String description;
public:
Book() : title(""), cost(0.0), onHand(0.0), authorName(""), description(""){}
Book(String t, float c, int oH, String aN, String d) :
title(t), cost(c), onHand(oH), authorName(aN), description(d){}
float sellPrice();
};
class Tape : public InventoryItem
{
private:
String title;
float cost;
int onHand;
float playTime;
public:
Tape() : title(""), cost(0.0), onHand(0.0), playTime(0.0){}
Tape(String t, float c, int oH, float pT) :
title(t), cost(c), onHand(oH), playTime(pT){}
float sellPrice();
};
class Cd : public InventoryItem
{
private:
String title;
float cost;
int onHand;
int numTrack;
float playTime;
public:
Cd() : title(""), cost(0.0), onHand(0.0), numTrack(0), playTime(0.0){}
Cd(String t, float c, int oH, int nT, float pT) :
title(t), cost(c), onHand(oH), numTrack(nT), playTime(pT){}
float sellPrice();
};
#endif
The dynamic cast lines didnt affect the compiler errors im getting
Originally Posted by
MSVC++
error C2039: 'onHand' : is not a member of 'InventoryItem'