09d1.cpp

来自「C++ interview materials. Very helpful fo」· C++ 代码 · 共 75 行

CPP
75
字号
/*
NIIT 《C++ & CGI PROGRAMMING &SCRRIPTING》 Skill Base
教材P9.8	9.D.1
审核:周永 Laozhou@swpi.edu.cn	23:58 2004-10-18
测试环境:Microsoft Windowsn 2000(VC++);Red Hat Linux 7.3;Red Hat Linux 9.0
*/

#include <iostream>
#include <string>
using namespace std;

class String
{
private:	
	char *str; 
	int check()
	{
		if (str == NULL)
			return 0;
		else 
			return 1;
	}
public:
	String()
	{
		str = 0;
	}
	String(char *inString)
	{
		str = new char[strlen(inString)+1];
		strcpy(str,inString);
	}
	void replace(char search, char repl)
	{
		if(check() == 0)
		{
			throw "NULL pointer exception";
		}

		int counter;
		for(counter = 0; str[counter] != '\0'; counter++)
		{
			if(str[counter] == search)
			{
				str[counter] = repl;
			}
		}
	}
	void display()
	{
		cout << str;
	}
};


int main()
{
	String strObject; //The Object Does Not Contain Anything 
	try
	{
		strObject.replace('+',' '); //The replace Function Raises An Exception 
		strObject.display();
	}
	catch(char *message)
	{
		cout << "Exception : " << message << endl;
	}
	catch(...)
	{
		cout <<"Unknown Exception!" <<endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?