zoukankan      html  css  js  c++  java
  • [C++]异常处理实例-基础版

    //头文件

    #pragma once
    #ifndef SALES_H_
    #define SALES_H_
    #include <stdexcept>
    #include <string>
    class Sales {
    public:
        enum { MONTHS = 12 };
        class bad_index :public std::logic_error {
        private:
            int bi;
        public:
            explicit bad_index(int ix, const std::string& s = "Index error in Sales object
    ");
            int bi_val() const { return bi; }
            virtual ~bad_index() throw() {}
        };
        explicit Sales(int yy = 0);
        Sales(int yy, const double* gr, int n);
        virtual ~Sales() {}
        int Year() const { return year; }
        virtual double operator[](int i) const;
        virtual double& operator[](int i);
    private:
        double gross[MONTHS];
        int year;
    };
    class LabeledSales :public Sales {
    public:
        class nbad_index :public Sales::bad_index {
        private:
            std::string lbl;
        public:
            nbad_index(const std::string& lb, int ix, const std::string& s = "Index error in LabelSales object
    ");
            const std::string& label_val() const { return lbl; }
            virtual ~nbad_index() throw() {}
        };
        explicit LabeledSales(const std::string& lb = "none", int yy = 0);
        LabeledSales(const std::string& lb, int yy, const double* gr, int n);
        virtual ~LabeledSales() {}
        const std::string& Label() const { return label; }
        virtual double operator[](int i) const;
        virtual double& operator[](int i);
    private:
        std::string label;
    };
    #endif // !SALES_H_
    View Code

    //方法实现文件

    #include "pch.h"
    #include "sales.h"
    using std::string;
    Sales::bad_index::bad_index(int ix, const string& s) :std::logic_error(s), bi(ix) {
    
    }
    Sales::Sales(int yy) {
        year = yy;
        for (int i = 0; i < MONTHS; i++)
            gross[i] = 0;
    }
    Sales::Sales(int yy, const double* gr, int n) {
        year = yy;
        int lim = (n < MONTHS) ? n : MONTHS;
        int i;
        for (i = 0; i < lim; i++)
            gross[i] = gr[i];
        for (; i < MONTHS; i++)
            gross[i] = 0;
    }
    double Sales::operator[](int i) const {
        if (i < 0 || i >= MONTHS)
            throw bad_index(i);
        return gross[i];
    }
    double& Sales::operator[](int i) {
        if (i < 0 || i >= MONTHS)
            throw bad_index(i);
        return gross[i];
    }
    LabeledSales::nbad_index::nbad_index(const string& lb, int ix, const string& s) :Sales::bad_index(ix, s) {
        lbl = lb;
    }
    LabeledSales::LabeledSales(const string& lb, int yy) : Sales(yy) {
        label = lb;
    }
    LabeledSales::LabeledSales(const string& lb, int yy, const double* gr, int n) : Sales(yy, gr, n) {
        label = lb;
    }
    double LabeledSales::operator[](int i) const {
        if (i < 0 || i >= MONTHS)
            throw nbad_index(Label(), i);
        return Sales::operator[](i);
    }
    double& LabeledSales::operator[](int i) {
        if (i < 0 || i >= MONTHS)
            throw nbad_index(Label(), i);
        return Sales::operator[](i);
    }
    View Code

    //主函数文件

    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cctype>
    #include <cstdlib>
    #include <ctime>
    #include <cmath>
    #include "sales.h"
    using namespace std;
    
    int main(void) {
        double vals[12] = {
            1220,1100,1122,2212,1232,2334,
            2884,2393,3302,2922,3002,3544
        };
        double vals2[12] = {
            12,11,22,21,32,34,
            28,29,33,29,32,35
        };
        Sales sales1(2011, vals, 12);
        LabeledSales sales2("Blogstar", 2012, vals2, 12);
        cout << "First try block:
    ";
        try {
            int i;
            cout << "Year = " << sales1.Year() << endl;
            for (i = 0; i < 12; i++) {
                cout << sales1[i] << ' ';
                if (i % 6 == 5)
                    cout << endl;
            }
            cout << "Year = " << sales2.Year() << endl;
            cout << "Label = " << sales2.Label() << endl;
            for (i = 0; i <= 12; i++) {
                cout << sales2[i] << ' ';
                if (i % 6 == 5)
                    cout << endl;
            }
            cout << "End of try block 1.
    ";
        }
        catch (LabeledSales::nbad_index& bad) {
            cout << bad.what();
            cout << "Company: " << bad.label_val() << endl;
            cout << "bad index: " << bad.bi_val() << endl;
        }
        catch (Sales::bad_index& bad) {
            cout << bad.what();
            cout << "bad index: " << bad.bi_val() << endl;
        }
        cout << "
    Next try block:
    ";
        try {
            sales2[2] = 37.5;
            sales1[20] = 23345;
            cout << "End of try block 2.
    ";
        }
        catch (LabeledSales::nbad_index& bad) {
            cout << bad.what();
            cout << "Company: " << bad.label_val() << endl;
            cout << "bad index: " << bad.bi_val() << endl;
        }
        catch (Sales::bad_index& bad) {
            cout << bad.what();
            cout << "bad index: " << bad.bi_val() << endl;
        }
        cout << "Done
    ";
        return 0;
    }
    View Code
  • 相关阅读:
    祝大家光棍节快乐!
    [Spring] Oracle TopLink O/R Mapping integrates Spring.
    VS2005 vs Eclipse, functions i expected.
    [English] Adverb for link (Chinese)
    有几个Gmail的Invitation
    Experience online service of MS small business (bCentral)
    多态(Polymorphism)
    Check your site and build meta tags for search engines
    关于怎样用javascript判断网页上我们想要必须选择的复选框至少选择一个的问题
    关于在VS2010中学习c++的MFC
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10463262.html
Copyright © 2011-2022 走看看