Hello , I'm having trouble with a C++ assignment. We're supposed to create an inventory program for keeping track of books, CDs and DVDs. We are supposed to use an abstract class InventoryItem, and classes Book, CD, DVD. Also a class Inventory will contain all the functions needed to enter the items into the data base. I'm having trouble figuring out how to get the object of Book, CD or DVD assigned into the memory space using new. I've just done a little, but here is the code I have so far:

#include <iostream>
#include <string>

using namespace std;
/////////////////////////////////////////////////////////////////////
class InventoryItem
{
public:
	virtual ~InventoryItem(){}
	virtual int IamThis(char c, char *pname)=0;
	virtual int nonHand()=0;
	virtual float Report(ostream& os)=0;

};
////////////////////////////////////////////////////////////////////
class Book : public InventoryItem
{
private:
	string title;
	string author;
	string description;
	float cost;
	int nonhand;
	char Iamthis;
public:
	Book():title(""),author(""),description(""),cost(0.0),nonhand(0),Iamthis('b'){}
	Book(string tit,string aut,string desc,float cst,int num, char iam):nonhand(0){}
	virtual int IamThis(char c, char *pname){return 0;}
	virtual int nonHand(){return 0;}
	virtual float Report(ostream& os){return 0;}



};
////////////////////////////////////////////////////////////////////
class CD : public InventoryItem
{
private:
	string title;
	string artistName;
	float playTime;
	string musicType;
	float cost;
	int nonhand;
	char Iamthis;

};
/////////////////////////////////////////////////////////////////////
class DVD : public InventoryItem
{
private:
	string title;
	string starActor;
	string director;
	string producer;
	float runTime;
	float cost;
	int nonhand;



	
};
//////////////////////////////////////////////////////////////////////
class Inventory
{
private:
	InventoryItem *PI[200];
	int n;

public:
	Inventory():n(0){}
	~Inventory(){
		for (int i=0;i<n;i++){
			delete PI[i];
		}
	}
	void createBook(string tit, string aut,string desc,float cst,int num,char iam)
	{
		Book* bookptr;
		bookptr = new Book;
		cout<<"book is: ";
		

	}
	void createCD(){}
	void creatDVD(){}
	void Report(){}
	void computePrice(){}


};
/////////////////////////////////////////////////////////////////////
int main()
{
	Inventory Inv;
	Inv.createBook("Super Dog","J. Jones","Story about an Heroic Dog",12.99,0,'b');
	
	return 0;
}

Any ideas or help out there? Appreciate. Thanks.

Keep in mind that no matter which object you create, you must increment n so that your inventory is updated with the amount of objects created.

Basically whenever you add a book, CD or DVD you have to increment n. Just take a look at your destructor, it deletes pointers in the array up to n amount, where n cannot be or exceed 200.

Also to allocate memory to a pointer from an array of pointers you can use the syntax--

PI[n] = new [Whichever Inventory Item]( Parameters, if any)

--for each create/add method in Inventory.


Lastly you won't be able to allocate memory for an object of any type until you override the pure-virtual functions in the base class InventoryItem.

If you're inclined with Java or C#, you can think of the pure-virtual function as a marking of an interface or abstract class. If anything inherits from that class it cannot be instantiated until you override the pure-virtual methods.

Thanks for the previous response. I've done some more work on it. I'm a bit stumped though as to how to retrieve the data back from the database in order to print it out. I can't use PI[n].title for example to retrieve the title of a book in the database. I'm definitely confused on that issue if anyone can enlighten me. Appreciate it. Here's the latest code:

#include <iostream>
#include <string>

using namespace std;
/////////////////////////////////////////////////////////////////////
class InventoryItem
{
public:
	virtual ~InventoryItem(){}
	virtual int IamThis(char c, char *pname)=0;
	virtual int nonHand()=0;
	virtual float Report(ostream& os)=0;

};
////////////////////////////////////////////////////////////////////
class Book : public InventoryItem
{
private:
	string title;
	string author;
	string description;
	float cost;
	int nonhand;
	char Iamthis;
	int x;
public:
	Book():title(""),author(""),description(""),cost(0.0),nonhand(0),Iamthis('b'){}
	Book(string tit,string aut,string desc,float cst,int num, char iam):nonhand(0){}
	virtual int IamThis(char c, char *pname){return 0;}
	virtual int nonHand(){return 0;}
	virtual float Report(ostream& os){return 0;}
	

};
////////////////////////////////////////////////////////////////////
class CD : public InventoryItem
{
private:
	string title;
	string artistName;
	float playTime;
	string musicType;
	float cost;
	int nonhand;
	char Iamthis;

public:
	CD():title(""),artistName(""),playTime(0.0),musicType(""),cost(0.0),nonhand(0),Iamthis('c'){}
	CD(string tit,string artName,float time,string type,float cst,int num, char iam):nonhand(0){}
	virtual int IamThis(char c, char *pname){return 0;}
	virtual int nonHand(){return 0;}
	virtual float Report(ostream& os){return 0;}

};
/////////////////////////////////////////////////////////////////////
class DVD : public InventoryItem
{
private:
	string title;
	string starActor;
	string director;
	string producer;
	float runTime;
	float cost;
	int nonhand;
	char Iamthis;

public:
	DVD():title(""),starActor(""),director(""),producer(""),runTime(0.0),cost(0.0),nonhand(0),Iamthis('d'){}
	DVD(string tit,string star,string dir,string prod,float time,float cst,int num, char iam):nonhand(0){}
	virtual int IamThis(char c, char *pname){return 0;}
	virtual int nonHand(){return 0;}
	virtual float Report(ostream& os){return 0;}



	
};
//////////////////////////////////////////////////////////////////////
class Inventory
{
private:
	InventoryItem *PI[200];
	int n;
	int x;

public:
	Inventory():n(0){}
	~Inventory(){
		for (int i=0;i<n;i++){
			delete PI[i];
		}
	}
	void createBook(string tit, string aut,string desc,float cst,int num,char iam)
	{	
		PI[n++] = new Book(tit,aut,desc,cst,num,iam);
	}
	void createCD(string tit,string artName,float time,string type,float cst,int num,char iam)
	{
		PI[n++] = new CD(tit,artName,time,type,cst,num,iam);
	}
	void creatDVD(string tit,string star,string dir,string prod,float time,float cst,int num,char iam)
	{
		PI[n++] = new DVD(tit,star,dir,prod,time,cst,num,iam);
	}
	void Report(){}
	void computePrice(){}
	
	


};
/////////////////////////////////////////////////////////////////////
int main()
{
	Inventory Inv;
	Inv.createBook("Super Dog","J. Jones","Story about an Heroic Dog",12.99,0,'b');
	Inv.createCD("Legend","Bob Marley",56,"Reggae",19.99,0,'c');
	Inv.creatDVD("Die Another Day","Pierce Brosnan","Lee Tamahori","Anthony Waye",132,15.99,0,'d');
	
	return 0;
}

I think that you can use a pointer and acess member variables using the '->' Operator.

And instead of a single delete. in your destructor. I think we can use something like

delete [] Pi;

In the Inventory Class destructor.

When I try to use PI[n]->title, it won't let me access it because it's a private member. It only lets me access the public member functions. The problem is that the professor wants us to have the data private, so there must be another way. Ugh.

When I try to use PI[n]->title, it won't let me access it because it's a private member. It only lets me access the public member functions. The problem is that the professor wants us to have the data private, so there must be another way. Ugh.

You could simply make a method with a return type that is exactly the same type of title.--

__typeof__(title) getTitle(){return title;};

Then access it--

PI[n]->getTitle()

--but from what I know __typeof__(element) is not a standard modifier and is frowned upon by programmers unless you're creating a library, so just return the type of title. Since it's string you'd use--

string getTitle(){return title;};

Yes you would need to add it in into your class function.

I mean.

class NameandID
{
private:
int x;
string name;//....And all your private members.
public:
int getx()const//Const Operator so that the function doesnt modify number 'x'
{
return x;
};
string getname()const//Const Operator so that it wont modify the name.
{
return name;
};
};

//So after that.

//You can access members with something like this.

int main()
{
NameandID name1;
NameandID *ptrname;

string s=ptrname->getname();
}
This article has been dead for over six months. Start a new discussion instead.