C++ - 打印派生类对象的矢量<base*> 元素
C++ - Printing vector<base*> elements for derived class objects
我有一个名为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_H
Here 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();
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 构造函数采用 Base&不被调用
- 将sharet_ptr<Derived>转换为shared_ptr<Base>
- 将 BASE 派生类存储在同一容器中
- 避免矢量中的对象切片<Base><shared_ptr>
- 从 std<Derived>::shared_ptr 的向量返回 std::shared_ptr<Base>
- 如何从unique_ptr返回unique_ptr的引用<Derived><Base>?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 如何将unique_ptr<derived>*转换为<base>unique_ptr*?
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?
- 从 Base 引用对象调用派生类的成员
- 两个成员,在Base中默认初始化,可能在Derived中非默认初始化
- 从"<Base>std::unique_ptr"创建"ClassType<std::unique_ptr>"时出现编译错误<Derived>
- C++ - 打印派生类对象的矢量<base*> 元素
- 为什么在此示例中从unique_ptr自动向上转换<derived>到unique_ptr<base>失败?
- 如何显式调用运算符<<