Working with classes?

F

fakeprogress

For a homework assignment in my Data Structures/C++ class, I have to
create the interface and implementation for a class called Book, create
objects within the class, and process transactions that manipulate (and
report on) members of the class.

Interface consists of:
- 5 private variables
char author[20];
char title[25];
char code[4];
int ncopies; // number of copies owned
int onloan; // number of copies currently on loan

- 5 public methods + 2 other methods
char *getAuthor; // returns (a pointer to) the author's name
char *getTitle; // returns (a pointer to) the title
char *getCode; // returns (a pointer to) the code of the book
int getNcopies; // returns the number of copies owned by the
library
int getOnLoan; // returns the number of copies currently on
loan

void Borrow( int ); // fixed number of copies are borrowed
void nReturn( int ); // returns fixed number of copies

- 2 required constructors + 1 optional constructor
Book( auth, tit, cd, ncop ) // creates an object of type Book
with the info specified & assumes number of copies on loan is 0
Book( auth, tit, cd, ncop, nonloan ) // creates an object of
type Book with the info specified

Book( ) // allows you to declare objects of type Book without
initializing any values


* There is an initial file of information (called initbooks) that
contains the information for the initial library. This information is
stored on a file (and is not to be keyed in individually). Write a
function ReadLibrary that will read the input file and create a book
for each line of information read. As each data line is read, you
should allocate a new record (possibly from within an available array)
and initialize the data.

Each data line contains an author, title, code, and number of copies.
Some data lines contain information regarding the number of copies
currently on loan. You should call the appropriate constructor and
intialize the book to the type of data input.


* There is a file of transactions (called trans) containing
transactions to be processed. Each transaction contains the following:
transaction type: b (borrow) or r (return)
account number: book code
amount: number of copies to be borrowed/returned
The appropriate object (book) corresponding to the book code should be
located. The balance should be updated by the amount input. The update
should be an increase (in the case of a deposit) or a decrease (in the
case of a withdrawal).

Write a function ProcessTransactions that will read the file trans and
process the transactions (and create a log).

* Write a function printFull. This function will print the inventory of
books held, including the author, title, code, number of copies in
holding, and number of copies available. The sequence of this report
should be in the sequence of the actual array at the time that it is
printed.

* Write a main function that will initialize the system and declare
whatever variables are needed. You should create an array of objects of
type Book (max size = 25). The main function should call the following
functions:
ReadLibrary
printFull
ProcessTransactions
printFull

--------------------

Now. I am NOT asking anyone to do this assignment for me. I needed to
type all that out so that one would know exactly what I'm talking about
and be of greater assistance :)

My question is with the methods I am supposed to write. For example, is
char *getAuthor simply as easy as
char Book :: getAuthor( ) {
return *author;
}
(is that even correct?)
....or am I missing a major point here? o_O


My professor's lesson on classes & objects leaves much to be desired.
Turning to the textbook was no help, either, because he's actually one
of the author's of the book... and he pretty much teaches directly from
the text.

I apologize if my question is silly... but, really, I'm struggling and
don't know where else to turn.

Thank you so much for whatever input you can provide :D

--Allie
 
A

Alf P. Steinbach

* (e-mail address removed):
For a homework assignment in my Data Structures/C++ class, I have to
create the interface and implementation for a class called Book, create
objects within the class, and process transactions that manipulate (and
report on) members of the class.

Interface consists of:
- 5 private variables
char author[20];
char title[25];
char code[4];
int ncopies; // number of copies owned
int onloan; // number of copies currently on loan

Tell your professor about the existence of std::string.

- 5 public methods + 2 other methods
char *getAuthor; // returns (a pointer to) the author's name
char *getTitle; // returns (a pointer to) the title
char *getCode; // returns (a pointer to) the code of the book

It's Very Bad Form to expose internal variables via references or
pointers to non-const.

Also, the prefix "get" has negative value in C++.

It does have some positive value in languages like Java and C# that
support dynnamic introspection, combined with a commonly accepted
convention for identifying getters and setters.

Third, there should presumably be some arguments?

int getNcopies; // returns the number of copies owned by the
library
int getOnLoan; // returns the number of copies currently on
loan

void Borrow( int ); // fixed number of copies are borrowed
void nReturn( int ); // returns fixed number of copies

- 2 required constructors + 1 optional constructor
Book( auth, tit, cd, ncop ) // creates an object of type Book
with the info specified & assumes number of copies on loan is 0
Book( auth, tit, cd, ncop, nonloan ) // creates an object of
type Book with the info specified

Book( ) // allows you to declare objects of type Book without
initializing any values

Ouch, ditch that professor, if that's /actually/ the specification.

An uninitialized object, of a class with non-trivial constructor(s),
should never exist.


[snip]
Now. I am NOT asking anyone to do this assignment for me. I needed to
type all that out so that one would know exactly what I'm talking about
and be of greater assistance :)

My question is with the methods I am supposed to write. For example, is
char *getAuthor simply as easy as
char Book :: getAuthor( ) {
return *author;
}
(is that even correct?)
...or am I missing a major point here? o_O

Yes, the return type.

My professor's lesson on classes & objects leaves much to be desired.
Turning to the textbook was no help, either, because he's actually one
of the author's of the book... and he pretty much teaches directly from
the text.

What's the book title?

Have you looked it up in ACCU's book reviews?
 
F

fakeprogress

I mutilate Alf P. Steinbach's response:
Tell your professor about the existence of std::string.
What is that?
Also, the prefix "get" has negative value in C++.
What do you mean by "negative value"?
Third, there should presumably be some arguments?
I was wondering the same thing... but I copied the assignment exactly
as I saw it on the handout.
What's the book title?
The book is "Data Structures Using C and C++"
(http://www.amazon.com/gp/product/0130369977).
Have you looked it up in ACCU's book reviews?
It's not listed in ACCU's book reviews o_O

Thanks!
 
O

osmium

Alf P. Steinbach said:
Ouch, ditch that professor, if that's /actually/ the specification.

An uninitialized object, of a class with non-trivial constructor(s),
should never exist.

I agree, this is truly a hideous course. I suspect the instructor doesn't
know about vector either, so he needs a default constructor to populate an
array.

[snip]
My question is with the methods I am supposed to write. For example, is
char *getAuthor simply as easy as
char Book :: getAuthor( ) {
return *author;
}
(is that even correct?)
...or am I missing a major point here? o_O

You want something like:

char* Book::get_author() { return author;}

author is already a pointer due to a kind of a nasty pointer/array
semi-equivalence. And besides that, you copied wrong from your earlier
text.
 
O

osmium

The book is "Data Structures Using C and C++"
(http://www.amazon.com/gp/product/0130369977).

15 reviews with 3 1/2 stars average. The average was boosted by one guy
making two posts (both five stars) and one guy commenting on the great
buyer/seller relationship, also five stars. I think Amazon is not quite
perfected.

If you just read the reviews and ignore the stars I can't imagine buying
that book. Of course, I think data structures should be taught in
pseudocode anyway, not the language du jour.
 
N

n2xssvv g02gfr12930

For a homework assignment in my Data Structures/C++ class, I have to
create the interface and implementation for a class called Book, create
objects within the class, and process transactions that manipulate (and
report on) members of the class.

Interface consists of:
- 5 private variables
char author[20];
char title[25];
char code[4];
int ncopies; // number of copies owned
int onloan; // number of copies currently on loan

- 5 public methods + 2 other methods
char *getAuthor; // returns (a pointer to) the author's name
char *getTitle; // returns (a pointer to) the title
char *getCode; // returns (a pointer to) the code of the book
int getNcopies; // returns the number of copies owned by the
library
int getOnLoan; // returns the number of copies currently on
loan

void Borrow( int ); // fixed number of copies are borrowed
void nReturn( int ); // returns fixed number of copies

- 2 required constructors + 1 optional constructor
Book( auth, tit, cd, ncop ) // creates an object of type Book
with the info specified & assumes number of copies on loan is 0
Book( auth, tit, cd, ncop, nonloan ) // creates an object of
type Book with the info specified

Book( ) // allows you to declare objects of type Book without
initializing any values


* There is an initial file of information (called initbooks) that
contains the information for the initial library. This information is
stored on a file (and is not to be keyed in individually). Write a
function ReadLibrary that will read the input file and create a book
for each line of information read. As each data line is read, you
should allocate a new record (possibly from within an available array)
and initialize the data.

Each data line contains an author, title, code, and number of copies.
Some data lines contain information regarding the number of copies
currently on loan. You should call the appropriate constructor and
intialize the book to the type of data input.


* There is a file of transactions (called trans) containing
transactions to be processed. Each transaction contains the following:
transaction type: b (borrow) or r (return)
account number: book code
amount: number of copies to be borrowed/returned
The appropriate object (book) corresponding to the book code should be
located. The balance should be updated by the amount input. The update
should be an increase (in the case of a deposit) or a decrease (in the
case of a withdrawal).

Write a function ProcessTransactions that will read the file trans and
process the transactions (and create a log).

* Write a function printFull. This function will print the inventory of
books held, including the author, title, code, number of copies in
holding, and number of copies available. The sequence of this report
should be in the sequence of the actual array at the time that it is
printed.

* Write a main function that will initialize the system and declare
whatever variables are needed. You should create an array of objects of
type Book (max size = 25). The main function should call the following
functions:
ReadLibrary
printFull
ProcessTransactions
printFull

--------------------

Now. I am NOT asking anyone to do this assignment for me. I needed to
type all that out so that one would know exactly what I'm talking about
and be of greater assistance :)

My question is with the methods I am supposed to write. For example, is
char *getAuthor simply as easy as
char Book :: getAuthor( ) {
return *author;
}
(is that even correct?)
...or am I missing a major point here? o_O


My professor's lesson on classes & objects leaves much to be desired.
Turning to the textbook was no help, either, because he's actually one
of the author's of the book... and he pretty much teaches directly from
the text.

I apologize if my question is silly... but, really, I'm struggling and
don't know where else to turn.

Thank you so much for whatever input you can provide :D

--Allie

Having years of C++ experience I don't see anything particularly
difficult here. That is from the C++ programming point of view,
unfortunately C++ skills alone are no longer sufficient for gainful
employment in software development. If that's what you're intending
you'll need at least a 2-1+ in Computer Science, or maybe less if gained
from Oxbridge or other highly rated University, and preferably be under 30.
I'm over 40 with a 2-2 from a lesser rated University, lots of C/C++
STL experience, but not using any associated technology like
COM/MFC/embedded/Linux/UNIX, and have been unemployed for the last 3
years. It would appear I'm well past my sell by date and will no doubt
have to find employment other than IT. Perhaps not the class you were
expecting, but you have been warned!

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top