第五周实验报告 任务一

来源:互联网 发布:求剑若此不亦惑乎 编辑:程序博客网 时间:2024/04/30 17:12

源代码:

//默认构造函数#include<iostream>#include<cmath>using namespace std;class Triangle{public:Triangle(float x = 1, float y = 1, float z = 1);//Triangle(float x, float y, float z);float perimeter(void);//计算三角形的周长float area(void);//计算并返回三角形的面积void showMessage();private:float a,b,c; //三边为私有成员数据};void Triangle::showMessage(){cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a)){cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;}else{cout<< "三边无法构成三角形!!!" << endl;}}float Triangle::perimeter(void){float d;d = a + b + c;return d;}float Triangle::area(void){float p = (a + b + c) / 2;float m = sqrt(p*(p-a)*(p-b)*(p-c));  return m;}Triangle::Triangle(float x, float y, float z){a = x;b = y;c = z;}void main(void){Triangle Tri1;//定义三角形类的一个实例(对象)Tri1.showMessage();Triangle Tri2(7,8,9);//定义三角形类的一个实例(对象)Tri2.showMessage();Triangle Tri3(2);Tri3.showMessage();}//带参数的构造函数#include<iostream>#include<cmath>using namespace std;class Triangle{public:/*Triangle(){ a = 1;b = 1;c = 1;}*/Triangle(float x, float y, float z);float perimeter(void);//计算三角形的周长float area(void);//计算并返回三角形的面积void showMessage();private:float a,b,c; //三边为私有成员数据};void Triangle::showMessage(){cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a)){cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;}else{cout<< "三边无法构成三角形!!!" << endl;}}float Triangle::perimeter(void){float d;d = a + b + c;return d;}float Triangle::area(void){float p = (a + b + c) / 2;float m = sqrt(p*(p-a)*(p-b)*(p-c));  return m;}Triangle::Triangle(float x, float y, float z){a = x;b = y;c = z;}void main(void){//Triangle Tri1;//定义三角形类的一个实例(对象)//Tri1.showMessage();Triangle Tri2(7,8,9);//定义三角形类的一个实例(对象)Tri2.showMessage();}//使用参数初始化表对成员函数初始化#include<iostream>#include<cmath>using namespace std;class Triangle{public:Triangle(){ a = 1;b = 1;c = 1;}Triangle(float x, float y, float z):a(x), b(y), c(z){}float perimeter(void);//计算三角形的周长float area(void);//计算并返回三角形的面积void showMessage();private:float a,b,c; //三边为私有成员数据};void Triangle::showMessage(){cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a)){cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;}else{cout<< "三边无法构成三角形!!!" << endl;}}float Triangle::perimeter(void){float d;d = a + b + c;return d;}float Triangle::area(void){float p = (a + b + c) / 2;float m = sqrt(p*(p-a)*(p-b)*(p-c));  return m;}/*Triangle::Triangle(float x, float y, float z){a = x;  b = y;  c = z;}*/void main(void){Triangle Tri1;//定义三角形类的一个实例(对象)Tri1.showMessage();Triangle Tri2(7,8,9);//定义三角形类的一个实例(对象)Tri2.showMessage();}


截图:

收获:理解了构造函数在类里的应用···收获不小···

感想:一开始没弄清题意···后来好了~开心哈~

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肝胆彩超喝水了怎么办 胆囊b超喝了水怎么办 系统b超照不到怎么办 思维彩超宝宝不动怎么办 怀孕七个月胎儿缺氧怎么办 怀孕29周小孩偏小怎么办 孕29周胎儿臀位怎么办 刚怀孕有囊肿该怎么办 食杏中毒怎么办吃什么 猫吃了扁桃仁怎么办 夏天来了??点狐臭怎么办? 我有一小点狐臭怎么办 淘客网站被微信屏蔽怎么办 微信老是屏蔽网站怎么办 华帝售后没人理怎么办 新开的网店没生意怎么办 淘小二介入买家举证不全怎么办 新股申购中签后钱不够怎么办 买的东西收不到怎么办? 拼多多按错收货怎么办 团购招生做到一半不如意怎么办 网购付款后卖家没有货怎么办 淘宝退货商家不同意退款怎么办 淘宝确认收货后卖家拒绝退款怎么办 淘宝卖家不同意退款怎么办 淘宝仅退款卖家不处理怎么办 淘宝申请退款卖家不处理怎么办 快递没收到货要退款怎么办 发票给了不给钱怎么办 付款后不给发票怎么办 供货商不给发票怎么办 刚生过孩子太胖买衣服怎么办 黑色牛仔裤洗的发白怎么办 蘑菇街手机丢了怎么办 黑衣服上全是白毛毛怎么办 支付宝注销了钱怎么办 网上买东西手机号写错了怎么办 二类工资卡过万怎么办 淘宝发货地址写错怎么办 淘宝发货地址写错了怎么办 淘宝不能代付了怎么办