代码如下:
#include <iostream>
#include <string>
using namespace std;
class player
{
public:
virtual void attack() = 0;
virtual void defense() = 0;
};
class forwards : public player
{
private:
string name;
public:
forwards(string forward_name) : name(forward_name) {}
void attack()
{
cout<<"前锋 进攻"<<name<<"\n";
}
void defense()
{
cout<<"前锋 防守"<<name<<"\n";
}
};
class center : public player
{
private:
string name;
public:
center(string center_name) : name(center_name) {}
void attack()
{
cout<<"中锋 进攻"<<name<<"\n";
}
void defense()
{
cout<<"中锋 防守"<<name<<"\n";
}
};
class foreigncenter
{
//private:
//string name;
public:
//foreigncenter(string foreigncenter_name):name(foreigncenter_name){}
void jingong()
{
cout<<"外籍中锋 进攻"<<"\n";
}
void fangshou()
{
cout<<"外籍中锋 防守"<<"\n";
}
};
class translator : public player
{
private:
foreigncenter wjzf;
public:
void attack()
{
wjzf.jingong();
}
void defense()
{
wjzf.fangshou();
}
};
int main()
{
player *b = new forwards("詹姆斯");
b->attack();
b->defense();
player *c = new center("邓肯");
c->attack();
c->defense();
player *d = new translator();
d->attack();
d->defense();
return 0;
}
编译&运行:
ngnsvr9 [** NONE **]/home/xionghailong/demo/adapter $ g++ main.cpp -o main
ngnsvr9 [** NONE **]/home/xionghailong/demo/adapter $ ./main
前锋 进攻詹姆斯
前锋 防守詹姆斯
中锋 进攻邓肯
中锋 防守邓肯
外籍中锋 进攻
外籍中锋 防守