头文件
#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();
}