【C++ Primer Plus习题】10.3

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

这里是引用

解答:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值