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<<"\nPlease enter the name:";
	cin.getline(fillname,LEN);
	cin.sync();
	cout<<"\nPlease enter the handicap:";
	cin>>handicap;
	cout<<"\nEnter end"<<endl;
}
void Golf::show()
{
	cout<<"\nshow 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.royalchen.com/

author:royalchen

Email:royalchen@royalchen.com

———————————————————————————————————————————————————

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注