zoukankan      html  css  js  c++  java
  • 136.异常的多态,父类对象,传递子类的引用或指针(地址)

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //纯虚类
     5 class error
     6 {
     7 public:
     8     //纯虚函数
     9     virtual void showerror() = 0;
    10 };
    11 
    12 class big :public error
    13 {
    14 public:
    15     void showerror()
    16     {
    17         cout << "big error" << endl;
    18     }
    19     
    20 };
    21 
    22 class small :public error
    23 {
    24 public:
    25     void showerror()
    26     {
    27         cout << "small error" << endl;
    28     }
    29 
    30 };
    31 
    32 class zero :public error
    33 {
    34 public:
    35     void showerror()
    36     {
    37         cout << "zero error" << endl;
    38     }
    39 
    40 };
    41 
    42 //抛出一个引用对象
    43 class print3d
    44 {
    45 public:
    46     print3d(double size)
    47     {
    48         if (size == 0)
    49         {
    50             throw &zero();
    51         }
    52         else if (size > 1000)
    53         {
    54             throw &big();
    55         }
    56         else if (size < 1)
    57         {
    58             throw &small();
    59         }
    60     }
    61 };
    62 
    63 void main()
    64 {
    65     try
    66     {
    67         print3d dg(0);
    68     }
    69     catch (error *p)
    70     {
    71         p->showerror();
    72     }
    73     cin.get();
    74 }
  • 相关阅读:
    Linux ansible的group模块
    ansible copy 模块详解
    Linux centos yum仓库 自制
    ansible 的playbook脚本
    Linux centos 监控备份
    Linux centos nginx下载安装初步
    周总结5
    周总结4
    爬取
    结对开发
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8686246.html
Copyright © 2011-2022 走看看