c++标准异常类的继承实现
来源:岁月联盟
时间:2012-10-31
出处来自百度。查来学习之用
// AbnomalTest.cpp : 定义控制台应用程序的入口点。
#include "StdAfx.h"
#include <iostream>
using namespace std;
class triangle :public exception//定义一个三角形的类
{
public :
float a,b,c,d;
float s;
public:
triangle()
{}
triangle(float a1,float b1,float c1)
{
a=a1;
b=b1;
c=c1;
}
void judgment() throw(exception)//判断是否是三角形
{
if((a+b)<c ||(a+c)<b || (c+b)<a)//任意两边和小于第三边,就不是三角形
{
throw exception("不是三角形");
}
}
void dimension()//计算面积
{
d=(a+b+c)/2; //海伦公式
s=sqrt(d*(d-a)*(d-b)*(d-c));
}
};
void _tmain()
{
triangle a(7,2,3);
try
{
a.judgment();
a.dimension();
cout<<"三角形a的面积为: "<<a.s<<endl;
}
catch(exception &e)
{
wcout<<e.what()<<endl;
}
}