当前位置:编程学习 > C/C++ >>

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

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,