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

golf.h

#ifndef _GOLF_H_
#define GOLF_H_

class Golf{
	const static int LEN=40;
private:
	char fillname[LEN];
	int handicap;
public:
	Golf(const char *name,int hc);
	Golf();
	void sethandicap(int hc){handicap=hc;};
	void show();
};






#endif

golf.cpp

#include <iostream>
#include "golf.h"
#include <cctype>
using namespace std;

Golf::Golf(const char *name,int hc)
{
	strcpy(fillname,name);
	//fillname=name;
	handicap=hc;
	cout<<"one golf success!"<<endl;
}
Golf::Golf()
{
	cout<<"
Please enter the name:";
	cin.getline(fillname,LEN);
	cin.sync();
	cout<<"
Please enter the handicap:";
	cin>>handicap;
	cout<<"
Enter end"<<endl;
}
void Golf::show()
{
	cout<<"
show on!"<<endl;
	cout<<"name:"<<fillname<<"   hc:"<<handicap<<endl;
	cout<<"show end!"<<endl;
}

main103.cpp

#include <iostream>
#include "golf.h"
using namespace std;
void main103()
{

	Golf g1;//不要写成了Golf::Golf 使用文件不用域运算符
	g1.show();
	Golf g2("myname1",100);
	g2.show();
	cin.get();





}


原文地址:https://www.cnblogs.com/qq84435/p/3664822.html