code tags, CODE TAGS, use them :o
http://www.cplusplus.com/articles/jEywvCM9/
May I know after I create a class and object and array like this
Book book[5]
how can I accept the input from user but not 1 times all input?
like inside the I am doing some question like asking the information about the book
1 2 3
|
for(i=0;i<5;i++){
cout<<"Enter Title : ";
}
|
Can I use the book[nob].setTitle(tit);?
is it possible to put like this way?
-----------------------------------------
1 2 3 4 5 6 7 8 9
|
string aut[4];
int noa=0;
do{
cout<<"Enter Author name : ";
getline(cin,aut[noa]);
cout<<"Is there still got any others Author?(yes/no)";
cin>>cont;
noa++;
} while(cont!="no"&&noa<4);
|
|
yes, that looks fine
after I save all the author name from the user,how can I pass the array to my class?
book[nob].setAuthor(aut[]);
Is this work?let say if the numer of author is 3
if I want to pass the array to set the,Is this possible to write like this? |
not exactly.
You will have to loop through all books and set the author.
You can just do it in the loop above like this
string aut; // not an array anymore
int noa=0;
do{
cout<<"Enter Author name : ";
getline(cin,author);
cout<<"Is there still got any others Author?(yes/no)";
cin>>cont;
book[noa].setAuthor(author);
noa++;
} while(cont!="no"&&noa<4);
when I want to return the the author
Can I to write like this way
1 2 3
|
string* Book::getAuthor(){
return author;
}
|
|
You could but it's bad practice
you can just return a copy of the author string, otherwise he could be modified from outside
Just remove the * beside string* to do so
1 2 3
|
string Book::getAuthor(){
return author;
}
|
------------------
about the constructor
I had check a lot of the post about constructor
it is like create a situation that you expect will happen,and you are going to initialize it like
1 2 3 4 5 6
|
Book::Book(string &tit,string &aut,double price){
title=tit;
author=aut;
pRice=price;
}
Book abook("Doremon","Datuk",22.20);
|
How if I dont want to to initialize it?
is it necessary to be exist?
Hope somebody could help me with this,Thanks |
If you don't want to initialize it you can write a "default constructor"
1 2 3 4 5
|
Book::Book(){
title = "";
author = "";
pRice = 0;
}
|
Also: you should change those "string &" to "const string &" because you don't modify the strings.
Without them you say "give me a piece of paper with the name on it, but be careful, i might write on them, you get back the new version" (you change the string)
With these const string& basically say "give me a piece of paper with the name on it and I'll not write on it, you'll get it back after I'm finished" (you don't change the string, it is constant)
1 2 3 4 5
|
Book::Book(const string &tit,const string &aut,double price){
title=tit;
author=aut;
pRice=price;
}
|