Thread: Help With Inherited Classes Needed

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Help With Inherited Classes Needed

    Hello,

    Let me first start off by saying that yes this is a home work problem. I am not looking for an answer so much as a direction to explore. The problem I am having with my code is this: The second inherited class has one and only one of its functions not working. That function is the get_book_title function but only with the Tape class. I have commented out the first inherited class, the Book class, and it works. When I have the two inherited classes together is when it does not work. Would someone please look over my code, found below, and please offer a suggestion?

    Thank you,
    Elliott

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Publication
    {
    	public:
    
    		Publication():book_title("default"){}
    		~Publication() {}
    
    		void get_book_title ()
    		{
    			cout << "Please enter the title of the publication: ";
    			getline ( cin , book_title );
    		};
    
    		string put_book_title ()
    		{
    			return book_title;
    		};
    
    		void get_book_price ()
    		{
    			cout << "Please enter the price of the publication: ";
    			cin  >> book_price;
    		};
    
    		double put_book_price ()
    		{
    			return book_price;
    		};
    
    	protected:
    
    		string book_title;
    		double book_price;
    };
    
    class Book : public Publication
    {
    	public:
    
    		Book():page_count(0){}
    		~Book() {}
    
    
    		void get_page_count ()
    		{
    			cout << "Please enter the number of pages in the book: ";
    			cin  >> page_count;
    		};
    
    		int put_page_count ()
    		{
    			return page_count;
    		}
    
    	private:
    
    		int page_count;
    };
    
    class Tape : public Publication
    {
    	public:
    
    		Tape():playing_time(0){}
    		~Tape(){}
    
    		void get_playing_time ()
    		{
    			cout << "Please enter the playing time in minutes: ";
    			cin  >> playing_time;
    		}
    
    		int put_playing_time ()
    		{
    			return playing_time;
    		}
    
    	private:
    
    		int playing_time;
    };
    
    int main ()
    {
    
    	Book test_book;
    	Tape test_tape;
    
    	cout << "Test of class Book: " << endl
    		 << "This class should contain the following data: " << endl 
    		 << "From the base class Publication:" << endl
    		 << "     book_title" << endl
    		 << "     book_price" << endl
    		 << "From the derived class Book:" << endl
    		 << "     page_count" << endl << endl;
    
    	test_book.get_book_title ();
    	test_book.get_book_price ();
    	test_book.get_page_count ();
    
    	cout << endl 
    		 << "Title:       " << test_book.put_book_title () << endl
    		 << "Price:     $ " << test_book.put_book_price () << endl
    		 << "Total Pages: " << test_book.put_page_count () << endl;
    
    	
    	cout << endl << endl << endl;
    
    	cout << "Test of class Tape: " << endl
    		 << "This class should contain the following data: " << endl 
    		 << "From the base class Publication:" << endl
    		 << "     book_title" << endl
    		 << "     book_price" << endl
    		 << "From the derived class Tape:" << endl
    		 << "     playing_time" << endl << endl;
    
    	
    	test_tape.get_book_price ();
    	test_tape.get_playing_time ();
    	test_tape.get_book_title ();
    	
    	cout << test_tape.put_book_price () << endl;
    	cout << test_tape.put_playing_time () << endl;
    	cout << test_tape.put_book_title () << endl;
    
    	return 0;
    
    }
    
    //OUTPUT:
    /*
    
    Test of class Book:
    This class should contain the following data:
    From the base class Publication:
         book_title
         book_price
    From the derived class Book:
         page_count
    
    Please enter the title of the publication: Moby Dick
    Please enter the price of the publication: 12.99
    Please enter the number of pages in the book: 123
    
    Title:       Moby Dick
    Price:     $ 12.99
    Total Pages: 123
    
    
    
    Test of class Tape:
    This class should contain the following data:
    From the base class Publication:
         book_title
         book_price
    From the derived class Tape:
         playing_time
    
    Please enter the price of the publication: 23.33
    Please enter the playing time in minutes: 456
    Please enter the title of the publication: 23.33
    456
    
    Press any key to continue
    
    */

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Rule #1 in programming is: initialize your variables. In Publication's default constructor you don't initialize all the member variables. Constructors are for initializing ALL the member variables.

    2) Whenever you switch between cin>> and getline() in a program, you are going to run into problems that you have to take care of. A line like this:

    cin >>num

    waits for the user to enter some input. Then the user can type something in and hit Return. However, when the user hits Return, an invisible \n or "newline" character is entered after the input ('\n' is considered a single character). So, if the user types in 10 and hits Return, the input actually looks like this:

    10\n

    Now, let's examine how that input is handled. The >> operator is programmed to skip all leading whitespace(e.g. spaces, tabs, and newlines), then read in data, and finally stop reading in data when the first white space character is encountered. And, very importantly, the terminating whitespace character is not read in. So, with this input:

    10\n

    1) cin>> skips any leading whitespace(there's no whitespace in front of 10)
    2) cin>> reads in 10, and
    3) cin>> stops reading when it encounters the whitespace character \n
    4) cin>> does not read in the \n, and leaves the \n in the input stream.

    Sometime thereafter, if these lines are executed:

    cout << "\nEnter your name: ";
    getline(cin, name);

    then that is an instruction to read in more input. Specifically, getline() grabs all the input it can including whitespace until it hits the end of the line, which is indicated by a newline character '\n'. Very importantly, unlike the >>operator, getline() reads in the \n character at the end of the line. In your case, on the second round of queries there is still a \n left in the input stream, and as far as getline() is concerned that is good input, so it doesn't need to wait for any user input, and therefore getline() does it's thing and reads in the \n.

    The lesson is: if you are going to be switching between the >>operator and getline(), you need to remove the '\n' character that is inserted at the end of the input when the user hits Return, and which the >>operator leaves in the input stream. You can remove a trailing whitespace character like this:

    cin>>data;
    cin.ignore(1);

    cin.ignore() will remove the designated number of characters from the stream.

    Note that cin.ignore() is not necessary when you do something like this:

    cin>>data;

    and then the user enters:

    10\n

    and then you do something like this:

    cin>>data2;

    Since the >> operator skips leading whitespace, it skips over the \n remaining in the stream and searches for some input, and when it can't find any, it stops and waits for the user to enter something.
    Last edited by 7stud; 12-11-2005 at 12:29 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Your base class should remove all sense of book from the public interface, so
    test_tape.get_book_price
    would read
    test_tape.get_price

    2. You've fallen into the classic trap of mixing cin and getline. cin by itself is like scanf from C, it only uses as many characters as it needs. Typically, this means leaving a newline, which is exactly what getline needs.

    Following all your cin with an ignore, like so, produced good results for me
    Code:
          cin  >> page_count;
          cin.ignore(100,'\n');
    Oh, and your publication constructor should initialise all the member variables.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Thank You

    Thank you very much, both of you. It is now a little more clear and I have made the proper corrections. I was a littlenervous about posting after I read the topic on home work. I hope it was not out of line. Once again, thank you very much.

    Elliott

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I hope it was not out of line.
    It was a perfectly good question IMO.
    We work on the principle that effort is rewarded. You put the effort in, posted your code and asked a specific question. Where people know the answer, it doesn't take much time to answer, and the answer will be read (and appreciated).

    The homework policy is for leeches who show up without having done anything and expect a whole answer on a plate. Answering such questions takes much longer, and all that will happen is it will get passed onto the tutor without being understood (or even read perhaps), and then on into the bin. A total waste of effort and a drain from answering good questions like yours.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes advice needed
    By DanFraser in forum C# Programming
    Replies: 5
    Last Post: 12-05-2007, 05:30 AM
  2. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  3. inherited classes and stl container
    By rahulsk1947 in forum C++ Programming
    Replies: 9
    Last Post: 05-05-2007, 02:27 PM
  4. High Schoolers - Techy Classes?
    By mart_man00 in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 05-09-2003, 02:14 PM
  5. Replies: 2
    Last Post: 01-18-2003, 01:32 AM