class Animal{
public:
virtual void showme()=0;};
class dog:public Animal
{public:
int zhonglei;
char name;
char food;
double weight;
void showme()
{
cout<<"狗的种类:"<<a.zhonglei<<endl;
        cout<<"狗的名称:"<<a.name<<endl;
cout<<"狗喜爱的食物:"<<a.food<<endl;
cout<<"狗的体重:"<<a.weight<<endl;
}
类猫与类狗一样
如何写运算符<<的重载函数 ,可以一次输入狗和猫喜爱的食物和体重

解决方案 »

  1.   


    #include <iostream>
    using namespace std;
    class Animal{
    public:
    virtual void showme()=0;
    };class dog:public Animal
    {
    public:
    int zhonglei;
    char name;
    char food;
    double weight; void showme()
    {
    cout<<"狗的种类:"<<zhonglei<<endl;
    cout<<"狗的名称:"<<name<<endl;
    cout<<"狗喜爱的食物:"<<food<<endl;
    cout<<"狗的体重:"<<weight<<endl;
    } friend std::ostream& operator << (ostream& o , dog& d)
    {
    cout<<"狗的种类:"<<d.zhonglei<<endl;
    cout<<"狗的名称:"<<d.name<<endl;
    cout<<"狗喜爱的食物:"<<d.food<<endl;
    cout<<"狗的体重:"<<d.weight<<endl;
    return o;
    }};int main
    { dog a; cout << a;
    return 0;
    }
      

  2.   

    #include <iostream>
    using namespace std;
    class Animal{
    public:
    virtual void showme()=0;
    };class dog:public Animal
    {
    public:
    int zhonglei;
    char name;
    char food;
    double weight; void showme()
    {
    cout<<"狗的种类:"<<zhonglei<<endl;
    cout<<"狗的名称:"<<name<<endl;
    cout<<"狗喜爱的食物:"<<food<<endl;
    cout<<"狗的体重:"<<weight<<endl;
    } friend std::ostream& operator << (ostream& o , dog& d)
    {
    o<<"狗的种类:"<<d.zhonglei<<endl;
    o<<"狗的名称:"<<d.name<<endl;
    o<<"狗喜爱的食物:"<<d.food<<endl;
    o<<"狗的体重:"<<d.weight<<endl;
    return o;
    }};