Help with classes?

Pages: 12
Having some trouble with my first attempt at classes.
Write a C++ definition for an abstract data type describing a book store inventory. Each boo has the following attributes:
-book title(string)
--book author (string)
book price(floating point)
count of books on hand (integer)
The member functions are as follows:
- A constructor that is used to initialize all eight elements of the array.
- A function that displays in a readable tabular form the contents of the entire book collection
-A modify function that, once called, prompts the user first for a book numb (1-9)and then for a code (T,A,P or C) indicating which attribute(title, author, Price or count ) of the indicated book is to be changed;finally,the function should provide a prompt for the new value of the attribute.

Write the declaration for an object named inventory of the type described by the class.

Ok so I think I did my header file right, but am having a little trouble with the implementation on the .cpp file. I wrote my questions in the .cpp file. Thanks.


Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
using namespace std;
#ifndef BSTORE_H
#define BSTORE_H

class bstore
{
public:
	bstore();
	bstore(int);
	void title(string);
	void author(string);
	void price(float);
	void countofbooks(int);
	void tabular();
	void modify();
private:
	string name;
	float money;
	int count;
};
#endif 



cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "hw.h"
#include <string>
#include <iostream>
using namespace std;

bstore::bstore(){
}
void bstore::title(string)
{
	cout<<"Title of book: ";
	cin>> name;
}
void bstore::author(string)
{ 
	cout<<"Name of Author: "<<endl;
	cin>> name;
}
void bstore::price(float)
{ 
	cout<<"Cost of Book: "<<endl;
	cin>> money;
}
void bstore::countofbooks(int)
{ 
	cout<<"# of book: "<<endl;
	cin>> count;
}
//what does it mean by a constructor it initialize all 8 elements? Is that just an array to set up all 8 books? books[7]? How would I setup such an array.

// dont know what to do for the void and tabular functions. For the tabular I think I could construct an array and store each
// book title in it and then display it.

//i think I can figure out modify once my array is setup. 
did you learn how to use classes within classes? that would be the easiest method..

Not yet. My professor just barely taught us classes and then gave us this hw. I have been looking through my book for a good example, but nothing is similar. If classes within classes is easiest enough to learn I will do that. I just want to get this done :(. Can you teach me how? Or point me in the right direction of the way I am doing it? Thanks.
you can create 3 arrays, i guess... one with the name, author, and and price..

assign the same book to the id of the book..

1
2
3
name_of_book[0] = "something";
author[0] = "some name for book something";
price[0] = 19.59;


rinse and repeat for each

if you want to do a class with a nested class, its more complicated for someone just learning classes, it has to do with pointers and such and can cause a headache to learn from my descriptions, if you are interested in learning:

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr061.htm

you essentially create a class that holds all the info, and they are stored in an array of nodes (aka. the sub class)

after reading this:

Write the declaration for an object named inventory of the type described by the class.

seems like you are doing nested classes.. missed that the first time
Last edited on
how to use classes within classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
using namespace std;

class Book
{
	public
		string book_title;
		string author;
		float price;
		int count;
};

class BookStore
{
	public
		BookStore();
		~BookStore();
		//other methods

	private
		Book book_array[6];
};
correct i prefer doing nested..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
using namespace std;

class BookStore
{
	public:
		BookStore();
		~BookStore();
		//other methods

	private:
        class Book
       {
	public:
		string book_title;
		string author;
		float price;
		int count;
         };
         Book book_array[6];
};
Last edited on
But you loose functionality like this. In my case BookStore class user can give object of type Book to object of type BookStore and get it back. And also for me my version are more understandable. Because class BookStore in your case is to much over loaded.
whats over loaded about my code?
First, you're not being asked to create an abstract data type at all. Second, the requirements are crap. (8 element array? book 1-9?)

Your header file isn't correct. bstore would be a container of books. (BookInventory would be a better name, imo.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

struct BookInfo
{
	std::string title ;
	std::string author ;
	float price ;
	unsigned nOnHand ;
};

class BookInventory
{
public:
	//- A constructor that is used to initialize all eight elements of the array.
	//  note:  Very fragile.  books must contain 8 elements.
	BookInventory( const BookInfo books[] ) ;

	// A function that displays in a readable tabular form the contents of the entire book collection
	// Use:  printOn(std::cout)
	void printOn(std::ostream& os) const ;

	// -A modify function that, once called, prompts the user first for a book numb (1-8 which maps to 0-7)and then for
	// a code (T,A,P or C) indicating which attribute(title, author, Price or count ) of the indicated
	// book is to be changed;finally,the function should provide a prompt for the new value of the attribute.
	void interactiveModify() ;  // horrible idea!

private:
	BookInfo _inventory[8] ;
};


Then you'd need to implement the methods, of course.

edit: Didn't see where you'd asked
what does it mean by a constructor it initialize all 8 elements? Is that just an array to set up all 8 books? books[7]? How would I setup such an array.


Here's a simple test driver which should work when you have the methods implemented and hopefully answers that question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	BookInfo books[8] =
	{
		{"TitleA", "AuthorA", 5.59f, 10},
		{"TitleB", "AuthorB", 4.99f,  2},
		// ...
	};

	BookInventory inventory(books) ;
	inventory.printOn(std::cout) ;

	inventory.interactiveModify() ;
	inventory.printOn(std::cout) ;
}
Last edited on
whats over loaded about my code?

By over loaded I mean it contains to much info. Class must be as abstract as possible.
Cire I tried running the code you provided, but it didn't work I got errors on all of the variables in the .cpp. Not sure what to do. Why is it a horrible idea to interactive modify (not sure what it even does)?
I presume you're talking about the main function. If you had the other code in an .h file, you would have to include it. And, of course, it won't link properly until you define the methods.

I thought the first part was for the .h and the sample test driver was for the .cpp file?
You have it correct. What I was saying is that the .cpp file would need to #include the .h file.
I did use #include the .h file and got errors.

1>hw.obj : error LNK2019: unresolved external symbol "public: void __thiscall BookInventory::interactiveModify(void)" (?interactiveModify@BookInventory@@QAEXXZ) referenced in function _main
1>hw.obj : error LNK2019: unresolved external symbol "public: void __thiscall BookInventory::printOn(class std::basic_ostream<char,struct std::char_traits<char> > &)const " (?printOn@BookInventory@@QBEXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main
1>hw.obj : error LNK2019: unresolved external symbol "public: __thiscall BookInventory::BookInventory(struct BookInfo const * const)" (??0BookInventory@@QAE@QBUBookInfo@@@Z) referenced in function _main
1>I:\CSP\Lab9\hw\Debug\hw.exe : fatal error LNK1120: 3 unresolved externals

Here is what my cpp looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "hw.h"

int main()
{
	BookInfo books[8] =
	{
		{"TitleA", "AuthorA", 5.59f, 10},
		{"TitleB", "AuthorB", 4.99f,  2},
		// ...
	};

	BookInventory inventory(books) ;
	inventory.printOn(std::cout) ;

	inventory.interactiveModify() ;
	inventory.printOn(std::cout) ;
}

bleh fixed it ^^

posted wrong thing initially
Last edited on
And, of course, it won't link properly until you define the methods.
I am sorry. How do I do this?
It's what your instructor asked you to do.

Surely you can make some sort of attempt.
I have made many attempts. He never taught the material. I guess I will keep trying. Thanks for the start.
If you're having problems, post the code you're having problems with. I'm sure most would be happy to help, but nobody wants to do your homework for you.
Pages: 12