I would like to know if it is possible to access and modify an element of an existing class object. If I have a constructor like this:
Book(string tit, string aut, string isbn, int day, int month, int year, bool out, Genre g)
and I create many objects of this class type and store them in vector<Book> v; when I access to the i-th vector element am I be able to modify filed bool out ?
Book foo { "Fu", "ba", "r", 1, 2, 3, false, fiction };
// I have now object foo. How do I now change its properties?
// For example, how to change whatever the property is that
// was initialized with false during construction?
Now, the answer to your question is:
yes, elements of a non-const vector can be modified.
That depends whether or not the variable which has its value assigned to that of bool out is public or private. By default in a class it is private. However this does sound like the sort of thing where instead of a public member, accessor functions should be used so you can do other things like record when the book was taken out etc.
class Book
{
public:
string f_title() { return title; }
string f_author() { return author; }
string f_isbn() { return isbn; }
Genre f_genre() { return g; }
int f_day();
int f_month();
int f_year();
bool f_out_read() { return out; }
Book();
Book(string tit, string aut, string isbn, int day, int month, int year, bool out, Genre g);
class Invalid_Day {};
class Invalid_Month {};
class Invalid_Year {};
class Invalid_Genre {};
class Invalid_Out {};
private:
string isbn, title, author;
int day, month, year;
Genre g;
bool out;
};
bool f_is_out(char& c);
Where my member bool out is actually private. I use two functions to read and modify its value. If I call the object Book back I should invoke again my two functions and then modify it again? Is that possible?
That does not modify an existing Book. The f_is_out() has nothing to do with book objects. It is a mere conversion from char to bool.
What you have is essentially:
1 2 3 4 5 6 7 8
Book n;
bool bar = true;
// construct temporary Book object
Book foo{ n.f_title(), n.f_author(), n.f_isbn(),
n.f_day(), n.f_month(), n.f_year(), bar, n.f_genre() };
n = foo; // copy assignment. Overwrites old value of n
Is that really the intended way to "modify" books?
How about,
1 2 3 4 5
Book n;
char c;
cout << "Is out? (Y/N): ";
is >> c;
n.set_out( f_is_out(c) );
Yes keskiverto! Thanks...
I am writing a program part of an exercise of Stroustrup's book about a little program that manages books in a library. Once I have created and stored available books, I have to prompt the user to request a book, then the program will check if it is available, if the user exists, and if the user does not owe fees (it is handled by another class "Patron") and then if constraints are satisfied I have to mark book as "out", then returning a false value a store it again. Then I think that your solution should work!
What I have posted is the helper function istream's flow where (formally) I define if a book is available or not to check it out, storing it in vector<Book>.
It is not simple for me because I am a newbie but I am finding some difficulties in the last part (this one) when I let the program manage all these routines.