From: CoryG89 on 22 Jun 2010 23:08 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. 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; 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; } 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. 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. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 23 Jun 2010 20:03 CoryG89 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. > > 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; > > Leading underscores in names are reserved in C/C++ Consider naming your members "m_isbn" or "isbn_" or maybe just "isbn". > 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++ > No you cannot. You can only return a pointer. If you have been given this exercise they should already have tried to explain pointers and arrays and how they relate to each other in C/C++. > Normally I know I would write an accessor function for the last three > similar to something like this: > > > double BookData::getCost() > { > return _cost; > } > This function should probably be defined as double BookData::getCost() const; But then maybe that's not too important for a beginner. > > 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. > Well for practise it might make sense to forego std::string. For your array members you can use: void BookData::setTitle(const char* title) { assert(title); // Make sure we do not get passed a NULL pointer strncpy(m_title, title, 14); // attn: constants used - not good m_title[13] = '\0'; // Make sure always null terminated } const char* BookData::getTitle() { return m_title; } Now, that's not too clumsy, or is it? br, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 23 Jun 2010 20:01 CoryG89 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. > > 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; > > > 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; > } > > > 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. The syntax IS clumpsy and error prone - that's why C++ introduced std::string. To otherwise get access to an array, the C way is returning a pointer to the first character. > > 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. I'm not sure it is a good idea to have separate setters for all values, for example I would think you cannot change publisher without also changing the publishing date. And how often do you change the author's name for a published book? Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: Why is the return type of count_if() "signed" rather than "unsigned"? Next: const in C++ |