zoukankan      html  css  js  c++  java
  • 第二十四章 异常和错误处理 5异常类的虚函数 简单

    //第二十四章 异常和错误处理 5异常类的虚函数
    #include <iostream>
    using namespace std;
    #include <iostream>
    using namespace std;
    const int num=5; 
    class people
    {
    public:
    	people(int Size=num);
    	~people(){ delete []p; };
    	int&operator[](int off);
    	const int&operator[](int off)const;
    	int GetSize()const{ return size;}
    
    	class wrong{};
    	class offset{
    		public:
    			offset(int Size):osize(Size){};
    			~offset(){};
    			virtual int Get(){ return osize;}
    			virtual void show(){
    			     cout<<"丢出offset异常"<<endl;
    				 cout<<"下标值"<<osize<<"出错"<<endl;
    			}
    		protected:
    			int osize;
    	};
    
    	class Big:public offset{
    	public:
    		Big(int Size):offset(Size){}
    		virtual void show(){
    		     cout<<"丢出Big异常"<<endl;
    			 cout<<"下标值为"<<offset::osize<<endl;
    		}
    	}; 
    	class Nav:public offset{
    	public:
    		Nav(int Size):offset(Size){}
    		virtual void show(){
    		    cout<<"丢出Nav异常"<<endl;
    			cout<<"下标值为"<<offset::osize<<endl;
    		}
    	}; 
    	class Small{
    		public:
    			Small(int Size):_size(Size){}
    			~Small(){}
    			virtual int Get(){ return _size;}
    			virtual void show(){
    			     cout<<"丢出Small异常"<<endl;
    				 cout<<"下标值为:"<<_size<<endl;
    			}
    		protected:
    			int _size;
    	};
    	class Zero: public Small{
    	public:
    		Zero(int Size):Small(Size){}
    		virtual void show()
    		{
    		     cout<<"丢出Small异常"<<endl;
    			 cout<<"下标值为:"<<Small::_size<<endl;
    		}
    	};
    private:
    	int *p;
    	int size;
    };
    
    people::people(int Size):size(Size)
    {
    	 cout<<"调用构造函数"<<endl;
    	 if(Size == 0){
    		 throw Zero(Size);
    	 }
    	 if(Size < 10){
    	     throw Small(Size);
    	 }
    	 if(Size > 10000){
    	     throw Big(Size);
    	 }
    	 if(Size < 1){
    	     throw Nav(Size);
    	 }
         p = new int[Size];
    	 for(int i=0; i<Size; i++)
    	 {
    	     p[i] = 0;
    	 }
    }
    
    int&people::operator[](int off)
    {
    	if(off>=0 && off < GetSize())
    	{
    	     return p[off];
    	}
    	throw wrong();
    	return p[0];
    }
    
    //一样,只是该函数内的值是不可更改并且返回值也是不可更改的
    const int&people::operator[](int off)const
    {
    	int Size = GetSize();
      	if(off>=0 && off < GetSize())
    	{
    	     return p[off];
    	}
    	throw wrong();
    	return p[0];    
    }
    
    int main()
    {
    	try{
    	    people one(9);
    		for(int i=0; i<100; i++){
    		   one[i] = i;
    		   cout<<"one["<<i<<"]:"<<i<<endl;
    		}
    	}
    	catch(people::wrong){
    	    cout<<"超过数组长度,不能继续进行赋值操作"<<endl;
    	}
    	catch(people::Big big){
    		//cout<<"数组值过大,值为:"<<big.Get()<<endl;
    		big.show();
    	}
    	catch(people::Small small){
    		//cout<<"数组值过小,值为:"<<small.Get()<<endl;
    		small.show();
    	}
    	catch(people::Zero zero){
    	     //cout<<"下标值为0"<<endl;
    		zero.show();
    	}
    	catch(people::Nav nav){
    	    // cout<<"下标值为负数"<<endl;
    		nav.show();
    	}
    	catch(people::offset offset){
    	     //cout<<"下标值过大或者过小"<<endl;
    		 offset.show();
    	}
        return 0;
    }
    

      

  • 相关阅读:
    jQuery——通过Ajax发送数据
    Python爬虫入门教程 71-100 续上篇,python爬虫爬取B站视频
    实战演练:PostgreSQL在线扩容
    直播丨Oracle比特币勒索&数据库大咖讲坛
    使用seaborn绘制强化学习中的图片
    nginx stream模块
    工具用的好下班走的早
    10年大数据平台经验,总结出这份数据建设干货(内含多张架构图)
    nginx 配置4层转发
    详解pytorch中的max方法
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2719239.html
Copyright © 2011-2022 走看看