c++primer plus练习10,练习7反复练习类和this指针

7.Betelgeusean plorg 有这些特征。
数据:
●plorg 的名称不超过19个字符;
●plorg 有满意指数(CI),这是一个整数。
操作:
●新的 plorg 将有名称,其 CI值为50;
●plorg 的 CI可以修改;
●plorg 可以报告其名称和 CI:
●plorg 的默认名称为“Plorga”
请编写一个Plorg 类声明(包括数据成员和成员函数原型)来表示 plorg,并编写成员函数的函数定义。
然后编写一个小程序,以演示 Plorg 类的所有特性。

原文链接:https://blog.youkuaiyun.com/zhyjhacker/article/details/139219979

#ifndef LIST_H_
#define LIST_H_

#include<iostream>

class BetelGeusean
{
public:
	BetelGeusean(const char* name = "Plorg");
	void setCi(const unsigned  ci);
	void showPlorg(void)const;
private:
	static const int LEN = 20;
	char chname[LEN];
	unsigned  ciIdx;
};
#endif // !LIST_H_

函数的实现

#include<iostream>
#include"list.h"

BetelGeusean::BetelGeusean(const char* name)
{
	this->ciIdx = 50;
	strcpy_s(this->chname, strlen(name) + 1, name);
}

void 
BetelGeusean::setCi(const unsigned ci)
{
	this->ciIdx = ci;
}

void
BetelGeusean::showPlorg(void)const
{
	std::cout << this->chname << " " << this->ciIdx;
}

测试程序

#pragma region 练习7
////程序清单
//xxx.cpp -- xxx
#if 1
#include <iostream>
#include"list.h"
int main()
{
	//using namespace std;
	BetelGeusean p1;
	p1.showPlorg();
	std::cout << "\n";
	p1.setCi(14);
	p1.showPlorg();

	return 0;
}
#endif
#pragma endregion
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值