1.设计并编写Songer(歌手)类,使用静态数据成员和静态成员函数实现统计当前歌手的数量,使用友元函数实现任意两个歌手之间的Pk。
Songer.h
#ifndef SHIYAN2_SONGER_H
#define SHIYAN2_SONGER_H
#include <iostream>
#include <cstring>
using namespace std;
class Songer {
private:
char name[10];
int fans_count;
static int count;
public:
Songer();
Songer(char name[10],int fans_count);
Songer(Songer &s);
void setFan(int fans_count);
~Songer();
void static ShowCount();
friend void Pk(Songer &s1,Songer &s2);
};
#endif //SHIYAN2_SONGER_H
Songer.cpp
#include "Songer.h"
int Songer::count=0;
Songer::Songer() {
count++;
}
Songer::Songer(char *n, int fans)
{
strcpy(name,n);
fans_count=fans;
count++;
}
Songer::Songer(Songer &s)
{
strcpy(name,s.name);
fans_count=s.fans_count;
count++;
}
void Songer::setFan(int fans_count)
{
Songer::fans_count=fans_count;
}
Songer::~Songer()
{
cout<<"测试结束"<<endl;
}
void Songer::ShowCount()
{
cout<<"当前歌手人数"<<count<<endl;
}
void Pk(Songer &s1,Songer &s2)
{
if(s1.fans_count==s2.fans_count)
{
cout<<s1.name<<"和"<<s2.name<<"粉丝数相同"<<endl;
}else if(s1.fans_count>s2.fans_count)
{
cout<<s1.name<<"战胜"<<s2.name<<endl;
}else
{
cout<<s2.name<<"战胜"<<s1.name<<endl;
}
}
main.cpp
#include <iostream>
using namespace std;
#include "Songer.h"
int main() {
Songer songer1((char*)"张三",100);
Songer songer2((char*)"李四",120);
Songer songer3((char*)"王五",90);
cout<<"修改粉丝数"<<endl;
int x;
cin>>x;
songer1.setFan(x);
cin>>x;
songer2.setFan(x);
cin>>x;
songer3.setFan(x);
cout<<"歌手数"<<endl;
Songer::ShowCount();
cout<<"PK"<<endl;
Pk(songer1,songer2);
Pk(songer2,songer3);
Pk(songer1,songer3);
}