Re: Accessor Functions (getter) for C String (Character Array) Members
On 23 Jun., 16:08, CoryG89 <cory...@gmail.com> wrote:
Ok, so I am working on this for school. I am building a model
business application in the console using C++.
The application is modeled as a database/cashier program. I have a
class called BookData that is supposed to hold a number of character
arrays and a few other variables.
Since this is for your homework (and it's OK if
you say so), I can only give some hints such that
the educational value of your lessons don't get
lost.
Here are my private members:
char _isbn[14];
char _title[51];
char _author[31];
char _publisher[31];
char _date[11];
int _quantity;
double _cost;
double _price;
Was this the pre-condition given by your teacher or
was this your own idea?
I am supposed to have public accessor and mutator functions that 'get'
and set' each private member of the class. I was surprised to learn
this, but apparently you cannot return an array from a function in C++
Normally I know I would write an accessor function for the last three
similar to something like this:
double BookData::getCost()
{
return _cost;
}
In C++ an important concept is the notion of
const-correctness. You should be sure that
your members reflect this concept. How could
you improve above declaration?
I am not sure how I should go about doing this for the character
arrays. This is for a C++ course and I believe I am supposed to keep
the c strings and not use strings from the standard libraries. I
cannot remember but I think that you are supposed to use pointers to
do this somehow. I tried but the syntax seems clumsy and I am getting
errors. Any tips would be most appreciated.
It would be helpful, if you showed you attempts
to see where the actual problem is. Hint: In C and
C++ it often helps to invent a typedef to simplify
the complexity of a declaration.
This is supposed to work by booting up and filling a dynamically
allocated array of BookData class objects from a delimited data file.
I need these 'setter' / 'getter' functions for accessing and
manipulating these values to add other functionality. I am going to be
working on this for the rest of tonight and all tomorrow. So anyone
that can give me any advice on this subject would be most helpful.
As you observed, you cannot directly return
a built-in array by function. You have
different choices here: There is one choice,
which works via a "by-value" approach, but since
C arrays aren't completely self-descriptive,
you need a second parameter that provides
the missing information. Think about ways
of doing this. The alternative approach
is a "by-reference" approach, which would
return a reference to the member data. How
would you declare a typedef for a reference
to e.g. char[14]?
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]