У меня есть абстрактный класс с именем Pet
и три производных класса Cat
Dog
Goldfish
, Я пытаюсь сохранить их в векторе и распечатать имена животных. Но я получаю шестнадцатеричное число. Я не знаю, где я делаю неправильно.
// Here is pet.h
#ifndef PET_H
#define PET_H
#include<iostream>
using namespace std;
class Pet
{
public:
//constructor
Pet(string);
// virtual destructor
virtual ~Pet();
// pure virtual function
virtual string speak() = 0;
//getters setters
string getName();
//overloading comparative operators
bool operator< (Pet&);
friend ostream& operator<<(ostream&, Pet&);
protected:
string pet_name; //name of pet
};#endif // PET_HHere is pet.cpp
#include <iostream>
#include "Pet.hpp"
#include <string>
using namespace std;
//constructor
Pet::Pet(string name) : pet_name(name)
{}
//overloading comparator function
bool Pet::operator<(Pet& obj)
{
return ((pet_name.compare(obj.pet_name)) < 0);
}
//getter for name
string Pet::getName() { return pet_name; }
// destructor
Pet::~Pet() { /* dtor */ }ostream& operator<<(ostream& output, Pet& p) {
output << "I am pet";
return output;
}#ifndef CAT_H
#define CAT_H
#include "Pet.hpp"
class Cat: public Pet
{
public:
Cat(string);
virtual ~Cat();
string speak();
friend ostream& operator<<(ostream &, Cat&);
};
#endif // CAT_H#include "Cat.hpp"#include<string>
Cat::Cat(string name):Pet(name)
{
//ctor
}
string Cat::speak()
{
return ">>Meow Meow>>";
}
Cat::~Cat()
{
//dtor
}ostream& operator<<(ostream& output, Cat& p) {
output << "I am " << p.getName() << " " << p.speak() << endl;
return output;
}
Файл List.hpp
#ifndef LIST_H
#define LIST_H
#include<iostream>
#include <vector>
#include<algorithm>
using namespace std;
template<class T>
class List
{
public:
void add_item(T);
void sortList();
void print();
private:
vector<T> list;
};template<class T>
void List<T>::add_item(T item_list) {
list.push_back(item_list);
}
template<class T>
void List<T>::sortList() {
sort(list.begin(), list.end());
}template<class T>
void List<T>::print() {
std::vector<T>::iterator i;
for (i = list.begin(); i != list.end(); ++i) {
cout << *i << endl;
}
}#endif // LIST_H
И это основная функция
int main()
{
List<Pet*> pets;
// book items adding in the lists
pets.add_item(new Cat("Kitty"));
pets.add_item(new Cat("Tom"));// sorting lists
pets.sortList();
// printing lists
// ----- Here is the PROBLEM ------
pets.print(); // --> Here I am getting problem
// --> when this statement executes, I get the hexa decimal number// saving in files
return 0;
}
ftfy:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <memory>
class Pet
{
public:
Pet(std::string name) : name{ std::move(name) } {}
virtual ~Pet() = default;
std::string get_name() const { return name; }
virtual std::string speak() const = 0;
friend std::ostream& operator<<(std::ostream& output, Pet const &pet)
{
output << "I am pet named " << pet.name;
return output;
}
protected:
std::string name;
};
class Cat : public Pet
{
public:
Cat(std::string name) : Pet{ std::move(name) } {};
virtual ~Cat() = default;
virtual std::string speak() const override { return ">>Meow Meow>>"; }
friend std::ostream& operator<<(std::ostream &output, Cat const & cat)
{
output << "I am a cat named " << cat.name << ' ' << cat.speak();
return output;
}
};
class Dog : public Pet
{
public:
Dog(std::string name) : Pet{ std::move(name) } {};
virtual ~Dog() = default;
virtual std::string speak() const override { return ">>Woof Woof>>"; }
friend std::ostream& operator<<(std::ostream &output, Dog const & dog)
{
output << "I am a dog named " << dog.name << ' ' << dog.speak();
return output;
}
};
template<class T>
class List
{
public:
void add_item(T item_list) { list.push_back(item_list); }
void sortList() {
std::sort(list.begin(), list.end(),
[](T const &lhs, T const &rhs) -> bool { return lhs.get()->get_name() < rhs.get()->get_name(); });
}
void print() const
{
for (typename std::vector<T>::const_iterator i{ list.begin() }; i != list.end(); ++i) {
if (auto cat = dynamic_cast<Cat*>((*i).get()); cat)
std::cout << *cat;
else if (auto dog = dynamic_cast<Dog*>((*i).get()); dog)
std::cout << *dog;
else
std::cout << **i;
std::cout.put('\n');
}
}
private:
std::vector<T> list;
};
int main()
{
List<std::shared_ptr<Pet>> pets;
pets.add_item(std::make_shared<Cat>("Kitty"));
pets.add_item(std::make_shared<Cat>("Tom"));
pets.add_item(std::make_shared<Dog>("Suzy"));
pets.add_item(std::make_shared<Dog>("Hasso"));
pets.sortList();
pets.print();
}
Других решений пока нет …