/*
* book.cpp
*
*Simple program for storing array of book titles and published dates
*
* Created on: Oct 9, 2010
* Author: Alpha
*/
#include <iostream>
#include <string>
usingnamespace std;
//declaration section
class book
{
private:
string bookTitle;
int publishedDate;
public:
void booktitles (string);
void date (int dd, int mm, int yyyy);
void printbook ();
};
void book::booktitles(string bookName)
{
bookTitle = bookName;
}
void book::date(int dd, int mm, int yyyy)
{
publishedDate = dd/mm/yyyy;
}
void book::printbook()
{
cout << bookTitle << "published in " << publishedDate << endl;
}
int main ()
{
book publishedTitle;
string titles;
cout << "Please enter the book title, press 99 to print previous records or press q to quit ";
getline (cin, titles);
publishedTitle.booktitles (titles);
cout << "Please enter published date in DD/MM/YYYY format ";
return 0;
}
I having some problem understanding the part on passing a value back to a private class. Had did a sample as above. Do highlight my mistake. Thanks for your time.
Thanks for pointing out this issue, will make the changes.
The area I had problem with is actually this portion.
I was expecting the cout to be "You have entered "booktitle" as your book title.
However, the output on screen was "You have entered as your book title ".
1 2 3 4 5 6 7 8 9 10 11
int main ()
{
book publishedTitle;
string titles;
cout << "Please enter the book title, press 99 to print previous records or press q to quit ";
getline (cin, titles);
publishedTitle.booktitles (titles);
cout << "Please enter published date in DD/MM/YYYY format ";
return 0;
}
1 2 3 4 5 6
void book::booktitles(string bookName)
{
book publishedTitle;
bookTitle = bookName;
cout << "You have entered " << publishedTitle.bookTitle << " as your book title " << endl;
}
Please enter the book title, press 99 to print previous records or press q to qu
it Foundation
Please enter published date in DD/MM/YYYY format
Process returned 0 (0x0) execution time : 5.097 s
Press any key to continue.
// The above is what happened when I ran the program and entered a book title.(I ran the beginning post's code AND replaced it after with your newer code, both gave the same output and I didn't get a chance to enter a date)
void book::booktitles(string bookName)
{
book publishedTitle;
bookTitle = bookName;
cout << "You have entered " << publishedTitle.bookTitle << " as your book title " << endl;
}
Since booktitles is a member of your 'book' class, it can change and read 'bookTitle' directly without any other object. It's implied it is referring to this->bookTitle.
That is what is happening on line 4. You are correct assigning 'bookName' to this book's 'bookTitle'.
The problem is, you're creating another book (publishedTitle, created on line 3) and printing that other book's title (which you never assign) instead of this book's title. That's why nothing is being printed.
Basically what you're doing is similar to this:
1 2 3 4 5 6 7
void print5()
{
int a; // some other number
int b = 5; // the number we want to print
cout << a; // printing the wrong number
}
// I'd like to thank Disch for posting that last code to simplify the problem. 'Tis helpful to me and I'm sure it's going to be somewhere in my brain if I ever come across a similar problem in the future!
void book::booktitles(string bookName)
{
bookTitle = bookName;
book publishedTitle;
cout << "You have entered " << bookTitle << " as your book title" << endl;
}
1 2 3 4 5 6 7 8 9 10 11
int main ()
{
book publishedTitle;
string titles;
cout << "Please enter the book title, press 99 to print previous records or press 00 to quit ";
getline (cin, titles);
publishedTitle.booktitles (titles);
cout << "Please enter published date in DD/MM/YYYY format ";
return 0;
}