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(); }