C++面向对象程序设计50道编程题(第11题)

版权声明:版权所有,尊重原创。转载请注明出处: https://blog.csdn.net/qq_36426650/article/details/84591244

C++面向对象程序设计50道编程题(第11题)

摘要:C++程序设计实习是为学生提供了一个既动手又动脑,独立实践的机会,将课本上的理论知识和实际有机的结合起来,锻炼学生的分析问题和解决问题的能力,提高学生运用所学知识解决实际问题的能力。
  本专辑为编程入门者、高校计算机软件专业学习或复习提供C++程序设计题库。
  读者请先独立思考哦,再与参考程序进行比对检查。

一、问题描述

在这里插入图片描述

二、考察内容

  基本面向对象概念,如何创建类、对象,对类私有数据成员和公有成员函数的理解,简单的字符串处理。

三、难度等级

难度等级:★★☆☆☆

四、参考程序

#include<iostream.h>
#include<string.h>
class String_Integer{
	char *s;
public:
	String_Integer(char *str)
	{
		s=new char[strlen(str)+1];
		strcpy(s,str);
	}
	operator int()
	{
		int num=0;
		for(char *p=s;*p;p++)
			if(*p>='0'&&*p<='9')
				num=num*10+*p-'0';
		return num;
	}
	void show()
	{
		cout<<"字符串为:"<<s<<endl;
	}
	~String_Integer()
	{
		delete []s;
	}
};
void main()
{
	char str[50];
	cout<<"请输入字符串ab12  3c00d45ef :";
	cin.getline(str,50);
	String_Integer test(str);
	test.show();
	int n=test;
	cout<<"转换得到的整数为:"<<n<<endl;
}

五、心得感受

可以在评论处写下思考和编程此题的感受

猜你喜欢

转载自blog.csdn.net/qq_36426650/article/details/84591244