栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++测试

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++测试

类和封装

正方体的体积和面积以及判断两个正方体是否相同。

#include
#include
using namespace std;
class Cube
{
public:
	void setL(int l)
	{
		C_L = l;
	}
	int getL()
	{
		return C_L;
	}
	void setW(int w)
	{
		C_W = w;
	}
	int getW()
	{
		return C_W;
	}
	void setH(int h)
	{
		C_H = h;
	}
	int getH()
	{
		return C_H;
	}
	int calculatrS()
	{
		return 2 * C_L * C_W + 2 * C_L * C_H + 2 * C_W * C_H;
	}
	int calculatrV()
	{
		return C_L * C_W * C_H;
	}
	bool issameByClass(Cube &c)
	{
		if (C_H== c.getH() && C_W == c.getW() && C_L == c.getL())
			return true;
		else
			return false;
	}
private:
	int C_L;
	int C_W;
	int C_H;
};
bool issame(Cube& c1, Cube& c2)
{
	if (c1.getH() == c2.getH() && c1.getW() == c2.getW() && c1.getL() == c2.getL())
		return true;
	else
		return false;
}
int main()
{	
	Cube c1;
	c1.setL(10);
	c1.setW(10);
	c1.setH(10);
	cout <<"c1的表面积为:" << c1.calculatrS() << endl;
	cout << "c1的体积为:" << c1.calculatrV() << endl;
	Cube c2;
	c2.setL(10);
	c2.setW(10);
	c2.setH(10);
	bool ret1 = issame(c1, c2);
	if (ret1)
		cout << "c1和c2相等" << endl;
	else
		cout << "c1和c2不相等"  << endl;
	bool ret2 = c1. issameByClass(c2);
	if (ret2)
		cout << "利用成员函数,c1和c2相等" << endl;
	else
		cout << "利用成员函数,c1和c2不相等" << endl;
	system("pause");
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号