Loading... 先搞点简单的练练手! ## 立方体类: ```cpp #include<iostream> #include<string> using namespace std; class Cube { public://公有属性 获取与设置长宽高 行为 int get_w()//获取宽 { return m_w; } void set_w(int w)//设置宽 { m_w = w; } int get_h()//获取高 { return m_h; } void set_h(int h)//设置高 { m_h = h; } int get_l()//获取长 { return m_l; } void set_l(int l)//设置长 { m_l = l; } public://公有属性 获取面积、获取体积、比较面积、比较体积 行为 int get_measure()//获取面积 { return m_w * m_w + m_h * m_h + m_l * m_l; } int get_volume()//获取体积 { return m_w * m_h * m_l; } bool Comparative_area(Cube temp_measure) //当前面积 大于 传入面积 返回真, 反之 { if (get_measure() > temp_measure.get_measure()) { return true; } else { return false; } } bool Comparison_volume(Cube temp_volume)//当前体积 大于 传入体积 返回真, 反之 { if (get_volume() > temp_volume.get_volume()) { return true; } else { return false; } } private://私有属性 设置变量 int m_w = 0; int m_h = 0; int m_l = 0; }; int main() { Cube Box_1, Box_2; Box_1.set_l(10); //设置第一个长方体属性 Box_1.set_h(10); Box_1.set_w(10); Box_2.set_l(20); //设置第二个长方体属性 Box_2.set_h(20); Box_2.set_w(20); cout << "第一个长方体面积为:" << Box_1.get_measure() <<" 面积为:"<<Box_1.get_volume()<< " 长为:" << Box_1.get_l() << " 宽为:" << Box_1.get_w() << " 高为:" << Box_1.get_h() << endl << endl; cout << "第二个长方体面积为:" << Box_2.get_measure() << " 体积为" <<Box_2.get_volume()<<" 长为:" << Box_2.get_l() << " 宽为:" << Box_2.get_w() << " 高为:" << Box_2.get_h() << endl << endl; if (Box_1.Comparative_area(Box_2)) { cout << "第一个长方体面积比第二个长方体面积大" << Box_1.get_measure() - Box_2.get_measure() << endl << endl; } else { cout << "第一个长方体面积比第二个长方体面积小" << Box_2.get_measure() - Box_1.get_measure() << endl << endl; } if (Box_1.Comparison_volume(Box_2)) { cout << "第一个长方体面积比第二个长方体面积大" << Box_1.get_volume() - Box_2.get_volume() << endl << endl; } else { cout << "第一个长方体面积比第二个长方体面积小" << Box_2.get_volume() - Box_1.get_volume() << endl << endl; } system("pause"); return 0; } ``` ## 点和圆关系类: ```cpp #include<iostream> using namespace std; class spot//点的x, y坐标 { private: int m_x ; int m_y ; public: void set_x_y(int x, int y)//设置x,y坐标 { m_x = x; m_y = y; } int get_x()//获取x坐标 { return m_x; } int get_y()//获取y坐标 { return m_y; } }; class circular//圆的属性 { private: int m_r;//圆的半径 spot heart;//圆心坐标 public: void set_r(int r)//设置圆的半径 { m_r = r; } void set_x_y(int x, int y)//设置圆心x与y坐标 { heart.set_x_y(x, y); } int get_r()//获取圆的半径 { return m_r; } int get_x()//获取圆心x坐标 { return heart.get_x(); } int get_y()//获取圆心y坐标 { return heart.get_y(); } }; void compare(circular c1, spot dian) //点在 圆内, 圆上, 圆外 判断函数 { int temp_long = (c1.get_x() - dian.get_x()) * (c1.get_x() - dian.get_x()) + (c1.get_y() - dian.get_y()) * (c1.get_y() - dian.get_y()); if (temp_long > c1.get_r()) { cout << "点在圆外" << endl; } else if(temp_long < c1.get_r()) { cout << "点在圆内" << endl; } else { cout << "点在圆上" << endl; } } int main() { spot dian_1; circular c1; dian_1.set_x_y(2, 0);//设置一个点 c1.set_r(1);//设置圆的半径 c1.set_x_y(0, 0);//设置圆心x,y坐标 compare(c1, dian_1);//调取点与圆的位置关系函数 system("pause"); return 0; } ``` 最后修改:2021 年 09 月 10 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。