Book Info Code

I'm having an error where the title and author of the book are supposed to be defined but they aren't and I'm not sure why. the Title and Author should be defined as "Tony Gaddis" and "C++ Early Objects" respectively but instead they are defined as the default text.

Output:
Author: Error: Author not defined
Title: Error: No Title Defined
Year Published: 2013
Price: 57.8
In Stock: 500
Press any key to continue . . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
	string author;
public:
	Person()
	{
		author = "Error: Author not defined";
	}
	Person(string a)
	{
		author = a;
	}
	string getAuthor()
	{
		return author;
	}
};

class BookAuthor
{
private:
	string title;
public:
	BookAuthor()
	{
		title = "Error: No Title Defined";
	}
	BookAuthor(string a)
	{
		title = a;
	}
	string getTitle()
	{
		return title;
	}
};

class Book: public BookAuthor, public Person
{
private:
	int year;
	double price;
	int stock;
public:
	Book()
	{
		price = 0;
	}
	Book(double cost)
	{
		price = cost;
	}
	Book(string a, string b, int c, double d, int e)
	{
		Person Author(a);
		BookAuthor Title(b);
		year = c;
		price = d;
		stock = e;
	};
	Book operator+(const Book &right)
	{
		double totalprice = price + right.price;
		Book temp(totalprice);
		return temp;
	}
	void Display()
	{
		cout << "Author: " << getAuthor() << endl;
		cout << "Title: " << getTitle() << endl;
		cout << "Year Published: " << year << endl;
		cout << "Price: " << price << endl;
		cout << "In Stock: " << stock << endl;
	}
};



int main()
{
	Book Cplusbk("Tony Gaddis", "C++ Early Objects", 2013, 57.8, 500);
	//Authors name, title, year published, price, and number of books in stock
	Cplusbk.Display(); //Displays the Authors Name, title, year published, and number of books in stock
	Book bk1(50.0), bk2(80.0), bk3;

	bk3 = bk1 + bk2;

	system("pause");
	return 0;
}
In your constructor for the 'Book' object you are creating instances of those classes, these die at the end of the constructors scope. What you want here is not to inherit from the other classes, but to add instances of those other classes to the 'Book' object. Think about it, a book has an author as an attribute, a book is not a sub-class of an author.
Topic archived. No new replies allowed.