【c++习题】【17/5/22】重载数组下标操作符
一.写出程序运行结果
1#include <iostream >
using namespace std;
int a[10]={1,2, 3, 4, 5, 6, 7, 8, 9, 10};
int fun( int i);
void main()
{int i ,s=0;
for( i=0;i<=10;i++)
{ try
{ s=s+fun(i);}
catch(int)
{cout<<”数组下标越界!”<<endl;}
}
cout<<"s=”<<s<<endl;
}
int fun( int i)
{if(i>=10)
throw i;
return a[i];
}
数组下标越界!
S=55
2 #include <iostream>
using namespace std;
void f();
class T
{public:
T( )
{cout<<"constructor"<<endl;
try
{throw "exception";}
catch( char*)
{cout<<"exception”<<endl;}
throw "exception";
}
~T( ) {cout<<"destructor";}
};
void main()
{cout<<"main function”<< endl;
try{ f( ); }
catch( char *)
{ cout<<"exception2"<<endl;}
cout<<"main function”<<endl;
}
void f( )
{ T t; }
main function
constructor
exception
exception2
main function
二、程序设计题
1以String类为例,在String类的构造函数中使用new分配内存。如果操作不成功,则用try语句触发一个char类型异常,用catch语句捕获该异常。同时将异常处理机制与其他处理方式对内存分配失败这一异常进行处理对比,体会异常处理机制的优点。
2在1的基础上,重载数组下标操作符[],使之具有判断与处理下标越界功能。
解法一
#include
<iostream>
#include
<cstring>
using
namespace std;
class
String{
public:
String(const
char*);
String(const
String&);
~String();
char
operator[](int);
void
ShowStr(){cout<<sPtr<<endl;}
private:
char
*sPtr;
};
1
#include <iostream>
using namespace std; int a[10]={1,2, 3, 4, 5, 6, 7, 8, 9, 10};
int fun( int i); void main()
{
int i, s=0;
for(i=0; i<=10; i++) { // 第十趟不会完整运行
try {
s=s+fun(i);
} catch(int) {
cout<<"数组下标越界!"<<endl;
}
}
cout<<"s="<<s<<endl;
} int fun(int i) {
if (i>=10) throw i;
return a[i];
} 2
#include <iostream>
using namespace std; void f(); class T {
public:
T() {
cout<<"constructor"<<endl; // 2
try
{ throw "exception";}
catch( char*)
{ cout<<"exception"<<endl;} // 3
throw "exception";
}
~T( ) {cout<<"destructor";} // 不被执行
}; void main()
{
cout<<"main function"<< endl; // 1
try { f( ); }
catch( char *)
{ cout<<"exception2"<<endl;} // 4
cout<<"main function"<<endl; // 5
} void f( )
{ T t; } 3
#include <iostream>
#include <cstring>
using namespace std;
class String{
public:
String(const char*);
String(const String&);
~String();
char operator[](int i);
void ShowStr(){cout<<sPtr<<endl;} private:
char *sPtr;
}; // 构造函数
String::String(const char *s) {
sPtr = new char[strlen(s)+1];
if (sPtr == NULL) throw ("Constructor abnormal");
strcpy(sPtr, s);
} String::String(const String& copy) {
sPtr = new char[strlen(copy.sPtr) + 1];
if (sPtr == NULL) throw ("Copy constructor abnormal");
strcpy(sPtr, copy.sPtr);
} String::~String() {
delete[] sPtr;
} // 重载数组下标操作符
char String::operator [] (int i) {
if (i < 0 || i > strlen(sPtr)) throw ("Exception: overflow.\n");
return sPtr[i];
} int main()
{
String hi("hello"); try {
hi.ShowStr(); printf("hi[0]=%c\n", hi[0]);
printf("hi[1]=%c\n", hi[1]);
printf("hi[2]=%c\n", hi[2]);
printf("hi[3]=%c\n", hi[3]);
printf("hi[4]=%c\n", hi[4]);
printf("hi[5]=%c(zero)\n", hi[5]);
printf("hi[6]=%c\n", hi[6]); // error!
} catch (char* exception) {
cout << exception << endl;
} return 0;
} /*
output:
hello
hi[0]=h
hi[1]=e
hi[2]=l
hi[3]=l
hi[4]=o
hi[5]= (zero)
Exception: overflow.
*/
【c++习题】【17/5/22】重载数组下标操作符的更多相关文章
- 网易云课堂_C++程序设计入门(下)_第8单元:年年岁岁花相似– 运算符重载_第8单元 - 作业2:OJ编程 - 重载数组下标运算符
第8单元 - 作业2:OJ编程 - 重载数组下标运算符 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...
- 5.6 C++重载下标操作符
参考:http://www.weixueyuan.net/view/6384.html 总结: 下标操作符是必须要以类的成员函数的形式进行重载的.其在类中的声明格式如下: 返回类型 & ...
- C++程序设计方法3:数组下标运算符重载
数组下标运算符重载 函数声明形式 返回类型operator[](参数): 如果返回类型是引用,则数组运算符调用可以出现在等式的左边,接受赋值,即: Obj[index] = value; 如果返回类型 ...
- js如何判断一组数字是否连续,得到一个临时数组[[3,4],[13,14,15],[17],[20],[22]];
var arrange = function(arr){ var result = [], temp = []; arr.sort(function(source, dest){ return sou ...
- C++学习29 重载[](下标运算符)
前面已经提到,下标操作符[]必须以类的成员函数的形式进行重载.在类中的声明格式如下: 返回值类型 & operator[] (参数) 或 const 返回值类型 & operator[ ...
- PHP 数组转json_encode,单个数组下标为了0时不对??
在 php 数组转json时,假如 有一个数组下标是顺序的,他json_encode后会直接变成一个简版二维json, $arr = ['1'=>1,'2'=>2]; echo (json ...
- poj 2262 筛法求素数(巧妙利用数组下标!)
Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41582 Accepted: ...
- C——数组下标与间址运算符
只说一句,数组下标与间址运算符*是等价的,即:a[i] = *(a+i),看代码: int main(int argc, char* argv[]) { ] = {, , , , }; int i; ...
- bash中不可以用字符串做数组下标
bash中可以用字符串做数组下标吗例如 test["abc"]=1------解决方案-------------------- 好像是误会,是awk里可以,bash shell里不 ...
随机推荐
- C#通过修改注册表改变IE默认选项
修改注册表,这个代码好实现,关键是怎么找到对应的注册表值,也就是说画一条线很容易,难的是找到要在哪里画,然后我百度了一圈,出来的都是画线的,没有指出或者指出的不全的注册表对应值,只能FQ谷歌了,也就有 ...
- 搭建RocketMQ踩的坑-内存不足
环境是vmvare+ubuntu 1.http://rocketmq.apache.org/docs/quick-start/ 按照官网来启动mqnamesrv和mqbroker报错 错误如下 The ...
- TP中单字母快捷函数总结
ThinkPHP中有许多使用简便的单字母函数,可以很方便开发者快速的调用,但是字母函数却不方便记忆,本文将所有的字母函数总结一下,以方便以后查找.1.U() URL组装 支持不同URL模式 U($ur ...
- 【转】Monkey测试3——Monkey测试结果分析
Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是monkey里面的哪个地方出错 2. 查看Monkey里面出错前的一些事件动作,并手 ...
- references non-existing project XXX, launch configuration问题的解决办法
Go to Project->properties In properties window's left pane select "Run/Debug Settings". ...
- linux shell习题训练
shell习题训练 求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2 ...
- Python eval 作用和风险 (string 转为dict list tuple)建议用“ast.literal_eval”
a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" b = eval(a) print b [[1, 2], [3, 4], [5, 6], [7, ...
- diamond源码阅读-diamond-client
读取数据 DiamondManager manager = new DefaultDiamondManager("DEFAULT_GROUP", "zml", ...
- Windows API之DuplicateHandle
在进程之间共享内核对象句柄的一种方法:DuplicateHandle 简单地说,该函数取得某个进程句柄表中的一个表项,然后把它拷贝到另一个进程的句柄表中. BOOL WINAPI DuplicateH ...
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 1)
本节中所阐述的内容,主要适用于SharePoint文档库中的文件和文件夹,以及列表中的文件夹.系统中的其他文件(如_layouts中的文件.配置文件.程序文件等)不在本章节的讨论范围之内. (一) ...