大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include <string>
#include "golf.h"
using namespace std;
#define SIZE 5
int main()
{
Golf ann("AnnBirdfree", 24);
Golf andy;
ann.showgolf();
andy.showgolf();
return 0;
}
golf.h
#pragma once
const int Len = 40;
class Golf
{
public:
Golf();
Golf(const char* name, int hc);
~Golf();
void handicap(int hc);
void showgolf()const;
private:
char fullname[Len];
int hc;
};
golf.cpp
#include "golf.h"
#include <iostream>
#include <string>
using namespace std;
Golf::Golf()
{
char name[Len];
int handicp=0;
cout << "请输入名字:";
cin.getline(name, Len);
cout << "请输入等级:";
cin >> handicp;
strcpy_s(this->fullname, name);
this->hc = handicp;
}
Golf::Golf(const char* name, int hc)
{
strcpy_s(this->fullname, name);
this->hc = hc;
}
Golf::~Golf()
{
}
void Golf::handicap(int hc)
{
this->hc = hc;
}
void Golf::showgolf()const
{
cout <<"名字为:" << this->fullname << endl;
cout <<"等级为:" << this->hc << endl;
}
运行结果:
考查点:
- 构造函数
2024年9月4日14:59:17