作者:墨启飞
环境: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;
}