如何使用重载打印 derived class 的一些特定属性 <<
How to print some specific atributes of derived class using overload <<
我有一个名为 Animals 的基础 Class 和 2 个派生的 class Dog 和 Cat
class Animal{
protected:
std::string name;
std::string color;
public:
std::string getName() {
return name;
}
std::string getColor() {
return color;
}
...
class Cat : public Animal {
private :
int lives;
public :
int getLives() {
return lives;
}
...
class Dog : public Animal {
private :
std::string gender;
public:
std::string getGender(){
return gender;
}
...
我有 shared_ptr
的 vec
std::vector<std::shared_ptr<Animal>> animals
我在向量中添加了一些猫和狗,我试图从向量中打印出每只动物的所有特征,使用运算符>>(这是一项作业,我们必须使用它)我做到了这个
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::shared_ptr<T>>& v)
{
for (int i = 0; i < v.size(); ++i) {
os << v[i]->getName();
os << "-";
os << v[i]->getColor();
if (i != v.size() - 1)
os << ", ";
os<<"\n";
}
return os;
}
但这样我只能打印名称和颜色或动物(这些属性在基础中class)
我的问题是:
我如何打印所有属性,猫的生命和狗的性别???
试试这个:
class Animal {
protected:
std::string name;
std::string color;
public:
virtual void print(std::ostream& os){
os << "Name:" << name <<" Color:"<< color;
}
void setName(string n) { name = n; }
void setColor(string c) { color = c; }
std::string getName() {
return name;
}
std::string getColor() {
return color;
}
};
class Cat : public Animal {
private:
int lives;
public:
void setLives(int n) { lives = n; }
void print(std::ostream& os) {
Animal::print(os);
os <<" Lives:"<<lives;
}
int getLives() {
return lives;
}
};
class Dog : public Animal {
private:
std::string gender;
public:
void setGender(string g) { gender = g; }
void print(std::ostream& os) {
Animal::print(os);
os << " Gender:" << gender;
}
std::string getGender() {
return gender;
}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::shared_ptr<T>>& v)
{
for (int i = 0; i < v.size(); ++i) {
v[i]->print(os);
os << "\n";
}
return os;
}
int main()
{
shared_ptr<Dog> d1 = make_shared<Dog>();
d1->setName("Dog1"); d1->setColor("White"); d1->setGender("Male");
shared_ptr<Cat> c1 = make_shared<Cat>();
c1->setName("Cat1"); c1->setColor("Brown"); c1->setLives(1);
vector<shared_ptr<Animal>> vec;
vec.push_back(d1);
vec.push_back(c1);
cout << vec;
return 0;
}
我有一个名为 Animals 的基础 Class 和 2 个派生的 class Dog 和 Cat
class Animal{
protected:
std::string name;
std::string color;
public:
std::string getName() {
return name;
}
std::string getColor() {
return color;
}
...
class Cat : public Animal {
private :
int lives;
public :
int getLives() {
return lives;
}
...
class Dog : public Animal {
private :
std::string gender;
public:
std::string getGender(){
return gender;
}
...
我有 shared_ptr
的 vecstd::vector<std::shared_ptr<Animal>> animals
我在向量中添加了一些猫和狗,我试图从向量中打印出每只动物的所有特征,使用运算符>>(这是一项作业,我们必须使用它)我做到了这个
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::shared_ptr<T>>& v)
{
for (int i = 0; i < v.size(); ++i) {
os << v[i]->getName();
os << "-";
os << v[i]->getColor();
if (i != v.size() - 1)
os << ", ";
os<<"\n";
}
return os;
}
但这样我只能打印名称和颜色或动物(这些属性在基础中class) 我的问题是: 我如何打印所有属性,猫的生命和狗的性别???
试试这个:
class Animal {
protected:
std::string name;
std::string color;
public:
virtual void print(std::ostream& os){
os << "Name:" << name <<" Color:"<< color;
}
void setName(string n) { name = n; }
void setColor(string c) { color = c; }
std::string getName() {
return name;
}
std::string getColor() {
return color;
}
};
class Cat : public Animal {
private:
int lives;
public:
void setLives(int n) { lives = n; }
void print(std::ostream& os) {
Animal::print(os);
os <<" Lives:"<<lives;
}
int getLives() {
return lives;
}
};
class Dog : public Animal {
private:
std::string gender;
public:
void setGender(string g) { gender = g; }
void print(std::ostream& os) {
Animal::print(os);
os << " Gender:" << gender;
}
std::string getGender() {
return gender;
}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<std::shared_ptr<T>>& v)
{
for (int i = 0; i < v.size(); ++i) {
v[i]->print(os);
os << "\n";
}
return os;
}
int main()
{
shared_ptr<Dog> d1 = make_shared<Dog>();
d1->setName("Dog1"); d1->setColor("White"); d1->setGender("Male");
shared_ptr<Cat> c1 = make_shared<Cat>();
c1->setName("Cat1"); c1->setColor("Brown"); c1->setLives(1);
vector<shared_ptr<Animal>> vec;
vec.push_back(d1);
vec.push_back(c1);
cout << vec;
return 0;
}