C++ 实验二 C++程序的结构

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);

}

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值