大一下,第三周---类的初体验

来源:互联网 发布:十大网络作家 编辑:程序博客网 时间:2024/05/02 00:58

编写基于对象的程序。数据成员包括长宽高,体积,要求用成员函数实现下面的功能

(1):由键盘输入3个长方柱的长(length),宽(width),高(high)。

(2):计算长方柱的体积(volume)和表面积(areas)。

(3):输入这3个长方柱的体积和表面积。

#include <iostream>using namespace std;class Figure{public:void set_data();float volume();float areas();                void show_data();private:float len;float weight;float high;float A;float V;}; int main() { Figure box1,box2,box3; box1.set_data();      //第一组数据 box1.volume(); box1.areas(); box1.show_data();box2.set_data();      //第二组数据 box2.volume(); box2.areas(); box2.show_data(); box3.set_data();     //第三组数据 box3.volume(); box3.areas(); box3.show_data(); return 0; } void Figure::set_data() { cout<<"输入对应的长、宽、高"<<endl; cin>> len; cin>> weight; cin>> high; } float Figure::areas() { return( 2* (len*weight+ len*high +high*weight)); } float Figure::volume() { return (len*weight*high); } void Figure::show_data() { cout<<"体积为"<<volume()<<"表面积为"<<areas()<<endl; }


原创粉丝点击