您的位置:首页 > 编程语言 > C语言/C++

C++primer plus第六版课后编程练习答案10.3

2015-11-30 15:37 513 查看
头文件
#ifndef GOLF_H_
#define GOLF_H_

static const int len = 40;

class golf
{

char fullname[len];
int handicap;
public:
golf(){}
golf(const char *name,int hc);
int setgolf(golf &g);
void sethandicap(int hc);
void showgolf();
};

#endif
#include<iostream>
#include "golf.h"

using namespace std;

golf::golf(const char *name,int hc)
{
strcpy(fullname,name);
handicap=hc;
}

int golf::setgolf(golf &g)
{
*this=g;//c++中类是有默认的赋值操作符的,默认的赋值操作符是实现按位拷贝。就是把A里面的内容按照数据位复制给B里面的内容
return 1;
}

void golf::sethandicap(int hc)
{
handicap=hc;
}

void golf::showgolf()
{
cout<<"fullname:"<<fullname<<endl;
cout<<"handicap:"<<handicap<<endl;
}

#include<iostream>
#include "golf.h"

using namespace std;

void main()
{
golf ann("Ann Birdfree",24);
ann.showgolf();
golf andy;
andy.setgolf(ann);
andy.showgolf();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: