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
*/