第五周实验报告1

 

  1. //默认构造函数  
  2. #include<iostream>  
  3.   
  4. #include<cmath>  
  5.   
  6. using namespace std;  
  7.   
  8. class Triangle  
  9. {  
  10. public:  
  11.       
  12.     Triangle(float x = 1, float y = 1, float z = 1);  
  13.       
  14.     //  Triangle(float x, float y, float z);  
  15.       
  16.     float perimeter(void);//计算三角形的周长  
  17.       
  18.     float area(void);//计算并返回三角形的面积  
  19.       
  20.     void showMessage();  
  21.       
  22. private:  
  23.       
  24.     float a,b,c; //三边为私有成员数据  
  25. };  
  26.   
  27. void Triangle::showMessage()  
  28. {  
  29.     cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;  
  30.       
  31.     if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a))  
  32.     {         
  33.         cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;  
  34.     }  
  35.       
  36.     else  
  37.     {  
  38.         cout<< "三边无法构成三角形!!!" << endl;  
  39.     }  
  40. }  
  41.   
  42. float Triangle::perimeter(void)  
  43. {  
  44.     float d;  
  45.       
  46.     d = a + b + c;  
  47.       
  48.     return d;  
  49. }  
  50.   
  51. float Triangle::area(void)  
  52. {  
  53.     float p = (a + b + c) / 2;  
  54.       
  55.     float m = sqrt(p*(p-a)*(p-b)*(p-c));    
  56.       
  57.     return m;  
  58. }  
  59.   
  60. Triangle::Triangle(float x, float y, float z)  
  61. {  
  62.     a = x;  
  63.       
  64.     b = y;  
  65.       
  66.     c = z;  
  67. }  
  68.   
  69. void main(void)  
  70. {  
  71.     Triangle Tri1;  //定义三角形类的一个实例(对象)  
  72.       
  73.     Tri1.showMessage();   
  74.       
  75.     Triangle Tri2(7,8,9);   //定义三角形类的一个实例(对象)  
  76.       
  77.     Tri2.showMessage();  
  78.       
  79.     Triangle Tri3(2);  
  80.       
  81.     Tri3.showMessage();   
  82. }  
  83. //带参数的构造函数  
  84. #include<iostream>  
  85.   
  86. #include<cmath>  
  87.   
  88. using namespace std;  
  89.   
  90. class Triangle  
  91. {  
  92. public:  
  93.       
  94. /*  Triangle() 
  95.     {  
  96.         a = 1; 
  97.          
  98.         b = 1; 
  99.          
  100.         c = 1; 
  101.     } 
  102.     */  
  103.       
  104.         Triangle(float x, float y, float z);  
  105.       
  106.     float perimeter(void);//计算三角形的周长  
  107.       
  108.     float area(void);//计算并返回三角形的面积  
  109.       
  110.     void showMessage();  
  111.       
  112. private:  
  113.       
  114.     float a,b,c; //三边为私有成员数据  
  115. };  
  116.   
  117. void Triangle::showMessage()  
  118. {  
  119.     cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;  
  120.       
  121.     if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a))  
  122.     {         
  123.         cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;  
  124.     }  
  125.       
  126.     else  
  127.     {  
  128.         cout<< "三边无法构成三角形!!!" << endl;  
  129.     }  
  130. }  
  131.   
  132. float Triangle::perimeter(void)  
  133. {  
  134.     float d;  
  135.       
  136.     d = a + b + c;  
  137.       
  138.     return d;  
  139. }  
  140.   
  141. float Triangle::area(void)  
  142. {  
  143.     float p = (a + b + c) / 2;  
  144.       
  145.     float m = sqrt(p*(p-a)*(p-b)*(p-c));    
  146.       
  147.     return m;  
  148. }  
  149.   
  150. Triangle::Triangle(float x, float y, float z)  
  151. {  
  152.     a = x;  
  153.       
  154.     b = y;  
  155.       
  156.     c = z;  
  157. }  
  158.   
  159. void main(void)  
  160. {  
  161. //  Triangle Tri1;  //定义三角形类的一个实例(对象)  
  162.       
  163. //  Tri1.showMessage();   
  164.       
  165.     Triangle Tri2(7,8,9);   //定义三角形类的一个实例(对象)  
  166.       
  167.     Tri2.showMessage();   
  168. }  
  169. //使用参数初始化表对成员函数初始化  
  170. #include<iostream>  
  171.   
  172. #include<cmath>  
  173.   
  174. using namespace std;  
  175.   
  176. class Triangle  
  177. {  
  178. public:  
  179.       
  180.     Triangle()  
  181.     {   
  182.         a = 1;  
  183.           
  184.         b = 1;  
  185.           
  186.         c = 1;  
  187.     }  
  188.       
  189.     Triangle(float x, float y, float z):a(x), b(y), c(z){}  
  190.       
  191.     float perimeter(void);//计算三角形的周长  
  192.       
  193.     float area(void);//计算并返回三角形的面积  
  194.       
  195.     void showMessage();  
  196.       
  197. private:  
  198.       
  199.     float a,b,c; //三边为私有成员数据  
  200. };  
  201.   
  202. void Triangle::showMessage()  
  203. {  
  204.     cout<<"三角形的三边长分别为:"<< a << " " << b << " " << c << endl;  
  205.       
  206.     if((a + b > c && a + c > b && b + c > a) && (a - b < c && a - c < b && b - c < a))  
  207.     {         
  208.         cout<<"该三角形的周长为:"<< perimeter()<<'\t'<<"面积为:"<< area()<<endl;  
  209.     }  
  210.       
  211.     else  
  212.     {  
  213.         cout<< "三边无法构成三角形!!!" << endl;  
  214.     }  
  215. }  
  216.   
  217. float Triangle::perimeter(void)  
  218. {  
  219.     float d;  
  220.       
  221.     d = a + b + c;  
  222.       
  223.     return d;  
  224. }  
  225.   
  226. float Triangle::area(void)  
  227. {  
  228.     float p = (a + b + c) / 2;  
  229.       
  230.     float m = sqrt(p*(p-a)*(p-b)*(p-c));    
  231.       
  232.     return m;  
  233. }  
  234.   
  235. /*Triangle::Triangle(float x, float y, float z) 
  236. { 
  237. a = x; 
  238.  
  239.   b = y; 
  240.    
  241.     c = z; 
  242. }*/  
  243.   
  244. void main(void)  
  245. {  
  246.     Triangle Tri1;  //定义三角形类的一个实例(对象)  
  247.       
  248.     Tri1.showMessage();   
  249.       
  250.     Triangle Tri2(7,8,9);   //定义三角形类的一个实例(对象)  
  251.       
  252.     Tri2.showMessage();   
  253. }  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值