第10章第3题

golf.h

#ifndef GOLF_H_
#define GOLF_H_
class golf
{
enum { Len = 40 };
private:
char fullname[Len];
int handicap;
public:
golf(const char* nm = "\0" , int cp = 0);
~golf();
int setGolf(const char* nm , int p);
void cap(int ac);
void show()const;
};
#endif

golf.cxx

#include<iostream>
#include<cstring>
#include"golf.h"
golf::golf(const char* nm , int cp)
{
strcpy(fullname,nm);
handicap = cp;
}
golf::~golf()
{
}
int golf::setGolf(const char* nm , int p)
{
if(*nm == '\0')
return 0;
else
{
strcpy(fullname,nm);
handicap = p;
}
return 1;
}
void golf::cap(int ac)
{
handicap = ac;
}
void golf::show()const
{
std::cout<<"姓名: "<<fullname<<"\n";
std::cout<<"成绩: "<<handicap<<"\n";
}

main.cxx

#include<iostream>
#include"golf.h"
int main()
{
golf tod[10];
char names[40];
int hc = 0;
int nu = 0; //记录输入了几个成员
for(int i = 0 ; i < 10 ; ++i)
{
std::cout<<"请输入姓名:";
std::cin.getline(names,40);
std::cout<<"请输入成绩:";
std::cin>>hc;
std::cin.get();
int bl = (tod+i)->setGolf(names,hc);
if(bl == 0)
break;
++nu;
}
for(int i = 0 ; i < nu ; ++i)
(tod+i)->show();
int newCap = 0;
for(int i = 0 ; i < nu ; ++i)
{
std::cout<<"请输入第"<<i+1<<"位先生的新成绩:";
std::cin>>newCap;
if(std::cin)
(tod+i)->cap(newCap); //如果输入不合法就部更改成绩
}
for(int i = 0 ; i < nu ; ++i)
(tod+i)->show();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值