心累,还是没弄明白虚继承和虚函数
```cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Citrus
{
public:
string name;
int weight;
Citrus(){}
Citrus(string n,int w):name(n),weight(w){}
void show()
{
cout<<name<<" "<<weight<<"kg, is citrus fruit."<<endl;
}
};
class Mandarin : virtual public Citrus
{
public:
Mandarin(){};
Mandarin(string n,int w):Citrus(n,w){};
void show()
{
cout<<name<<" "<<weight<<"kg, is mandarin fruit."<<endl;
}
};
class Pomelo :virtual public Citrus
{
public:
Pomelo(){};
Pomelo(string n,int w):Citrus(n,w){};
void show()
{
cout<<name<<" "<<weight<<"kg, is pomelo."<<endl;
}
};
class Citron : virtual public Citrus
{
public:
Citron() {}
Citron(string n, double w) : Citrus(n, w) {}
void show()
{
cout << name << " " << weight << "kg, is citron.\n";
}
};
class Orange : virtual public Mandarin, virtual public Pomelo
{
public:
Orange() {}
Orange(string n, double w) : Citrus(n, w) {}
void show()
{
cout << name << " " << weight << "kg, is orange.\n";
}
};
class Lime : virtual public Pomelo,virtual public Citron
{
public:
Lime(){};
Lime(string n,int w):Citrus(n, w){};
void show()
{
cout<<name<<" "<<weight<<"kg, is lime."<<endl;
}
};
class Tangerine :virtual public Mandarin,virtual public Orange
{
public:
Tangerine(){};
Tangerine(string n,int w):Citrus(n,w){};
void show()
{
cout<<name<<" "<<weight<<"kg, is tangerine."<<endl;
}
};
class Grapefruit :virtual public Pomelo,virtual public Orange
{
public:
Grapefruit(){};
Grapefruit(string n,int w):Citrus(n,w){};
void show()
{
cout<<name<<" "<<weight<<"kg, is grapefruit."<<endl;
}
};
class Lemon :virtual public Orange,virtual public Lime
{
public:
Lemon(){};
Lemon(string n,int w): Citrus(n, w){};
void show()
{
cout<<name<<" "<<weight<<"kg, is lemon."<<endl;
}
};
int main()
{
Citrus *c;
Mandarin *ma;
Pomelo *po;
Citron *ci;
Orange *og;
Lime *li;
Tangerine *ta;
Grapefruit *gr;
Lemon *le;
string name;
double weight;
while(cin >> name >> weight)
{
if(name == "Tangerine")
{
Tangerine tangerine(name, weight);
c = ma = og = ta = &tangerine;
c->show();
ma->show();
og->show();
ta->show();
}
if(name == "Grapefruit")
{
Grapefruit grapefruit(name, weight);
c = po = gr = &grapefruit;
c->show();
po->show();
gr->show();
}
if(name == "Lemon")
{
Lemon lemon(name, weight);
ma = og = &lemon;
ci = li = &lemon;
po = le = &lemon;
ma->show();
po->show();
ci->show();
og->show();
li->show();
le->show();
}
}
}