第5周项目1-三角形类雏形

来源:互联网 发布:.wang域名值得投资吗 编辑:程序博客网 时间:2024/05/12 12:44


Copyright (c) 2016,
All rights reserced.
文件名称:main.cpp
作者:李鑫

完成日期:2016.3.31

问题描述:下面设计一个三角形类,请给出成员函数的定义。

#include<iostream>#include<cstdlib>#include<Cmath>using namespace std;class Triangle{public:    void setABC(double x, double y, double z);    double perimeter();    double area();private:    double a,b,c;};int main(){    Triangle tri1;    tri1.setABC(4,5,6);    cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;    return 0;}void Triangle::setABC(double x, double y, double z){    if(x+y>z&&x+z>y&&y+z>x)    {        a=x;        b=y;        c=z;    }    else    {        cout<<"不能构成三角形"<<endl;        exit(0);    }}double Triangle::perimeter(void){    return a+b+c;}double Triangle::area(void){    double p=(a+b+c)/2;    return sqrt(p*(p-a)*(p-b)*(p-c));}<img src="http://img.blog.csdn.net/20160331090643722" alt="" />
0 0