立方体类设计案例

作者:墨启飞

环境:vs2019

时间:2021.10.7

#pragma once
class Cube {
private:
	int m_Length;  //长
	int m_Width;   //宽
	int m_Height;  //高

public:
	void setLength(int  length);
	int getLength();

	void setWidth(int width);
	int getWidth();

	void setHeight(int height);
	int getHeight();

	//获得面积的方法
	int getArea();

	//获得体积的方法
	int getVolume();

	bool cubCompare(Cube& c);
};
#include"Cube.h"

void Cube::setLength(int length) {
	m_Length = length;
}

int Cube::getLength() {
	return m_Length;
}

void Cube::setWidth(int width) {
	m_Width = width;
}

int Cube::getWidth() {
	return m_Width;
}

void Cube::setHeight(int height) {
	m_Height = height;
}

int Cube::getHeight() {
	return m_Height;
}

int Cube::getArea() {
	return (m_Length * m_Height + m_Length * m_Width + m_Height * m_Width) * 2;
}

int Cube::getVolume() {
	return m_Length * m_Height * m_Width;
}

bool Cube::cubCompare(Cube& c) {
	if (m_Length == c.m_Length && m_Width == c.m_Width && m_Height == c.m_Height) {
		return true;
	}
	else {
		return false;
	}
}
#include<iostream>
#include"Cube.h"
using namespace std;
/*
	设计立方体类(Cube),求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否想得。

	立方体类;
		成员变量:长(length) 宽(widht) 高(height)
		成员方法:获得面积的方法(getArea) 获得体积的方法(getVolume)私有成员的公共方法 判断两个立方体是否相等的方法

*/

bool cubCompare(Cube& c1, Cube& c2) {
	if (c1.getLength() == c2.getLength() && c1.getWidth() == c2.getWidth() && c1.getHeight() == c2.getHeight()) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	//创建对象
	Cube c1;

	c1.setLength(10);
	c1.setWidth(10);
	c1.setHeight(5);

	cout << "面积:" << c1.getArea() << endl;
	cout << "体积:" << c1.getVolume() << endl;

	Cube c2;
	c2.setLength(10);
	c2.setWidth(10);
	c2.setHeight(5);

	bool flag = c1.cubCompare(c2);

	cout << flag << endl;

	flag = cubCompare(c1, c2);

	cout << flag << endl;
	return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨启飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值