I'm re-learning how to create classes and use inheritance from this one site, LearnCPP.com, I am trying to figure something out in my book class. I want to overload the Book constructor to only hold text so I can enter in really long strings of text. I'll fix this later by using a vector to hold the strings and each element be a page, but for now I'm wondering if overloading inherited constructors can be done?
Another question, This is a basic inheritance example I made, taking from the LearnCPP site, adding my own stuff and classes to it, hopefully I did everything right, I'm just wondering as it is right now, is it constructed well, following good programming form?
Last question. If I made a Weapon class for swords, it would inherit from item, since swords are items, but sword would inherit from weapons and not items so how would that look??? It has to inherit from weapon but it's still an item so im a bit confused as to how its going to be a weapon AND an item at the same time without having to use multiple inheritance, which is what i want to avoid.
Code:#include <iostream>#include <vector>
class Item
{
public:
Item(const std::string& name, float cost, float value, float weight)
: name(name), cost(cost), value(value), weight(weight) {}
std::string DisplayName() const { return name; }
float DisplayCost() const { return cost; }
float DisplayValue() const { return value; }
float DisplayWeight() const { return weight; }
private:
std::string name{ "Generic Item" };
float cost{ 2 };
float value{ 1 };
float weight{ 1 };
};
class Book : public Item
{
public:
Book(const std::string& name, float cost, float value, float weight, const std::string& text, const std::string& title, const std::string& author)
: Item{ name, cost, value, weight }, text(text), title(title), author(author){}
//Book(const std::string& text): text(text){} //How do you do this??
std::string Read() const { return text; }
std::string DisplayTitle() const { return title; }
std::string DisplayAuthor() const { return author; }
private:
std::string text{ "Default" };
std::string title{ "Title" };
std::string author{ "Author Name" };
};
class Sword : public Item
{
public:
Sword(const std::string& name, float cost, float value, float weight, float attackPower, float durability)
: Item{ name, cost, value, weight }, attackPower(attackPower), durability(durability){}
float DisplayAttackPower() const { return attackPower; }
float DisplayDurability() const { return durability; }
friend std::ostream& operator<<(std::ostream& os, Sword& sword)
{
os << "Name: " << sword.DisplayName() << std::endl;
os << "Cost: " << sword.DisplayCost() << std::endl;
os << "Value: " << sword.DisplayValue() << std::endl;
os << "Weight: " << sword.DisplayWeight() << std::endl;
os << "Attack Power: " << sword.DisplayAttackPower() << std::endl;
os << "Durability: " << sword.DisplayDurability() << std::endl;
return os;
}
private:
float attackPower{ 5 };
float durability{ 1 };
};
int main()
{
Sword Greatsword{"Greatsword", 1000, 800, 15, 34, 100};
Sword BastardSword{"Bastard Sword", 1500, 1200, 20, 50, 100};
std::cout << Greatsword << std::endl;
std::cout << BastardSword << std::endl;
Book AtlasOfDragons
(
"Book",
50,
30,
3,
"Herein is recorded the list of known dragons, both living and dead, including those slain by the Dragonguard since the time of its founding, as well as those slain in earlier ages, where they can be identified. Unfortunately, only a few of the dragons slain by our Akaviri predecessors during the Crusade were recorded and thus this list is sadly incomplete.",
"Atlas of Dragons",
"Brother Mathnan"
);
std::cout << AtlasOfDragons.DisplayTitle() << std::endl;
std::cout << "By " << AtlasOfDragons.DisplayAuthor() << '\n' << std::endl;
std::cout << AtlasOfDragons.Read() << std::endl;
return 0;
}