首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++标准错误类的继承实现

2012-11-03 
c++标准异常类的继承实现出处来自百度。查来学习之用// AbnomalTest.cpp : 定义控制台应用程序的入口点。#in

c++标准异常类的继承实现

出处来自百度。查来学习之用

// 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;
 }
}

热点排行