编程高手进¥关于C++的问题~~~~~
来源:百度知道 编辑:UC知道 时间:2024/05/24 14:30:50
第一个程序:
#include <iostream.h>
#include <string.h>
#include <stdio.h>
/*创建一个字符串类:String*/
class String{
public:
String(const char* str = NULL); //构造函数之一
String(const String& another); //构造函数之二
void input_str(); //字符串输入
String& operator +(const String& rhs); //重载加法运算符
void print(); //字符串输出
private:
char *m_data;
char *addstr;
};
/*构造函数之一*/
String::String(const char* str)
{
if(str == NULL) //如果输入为空字符串,则添加“\0”表示空串。
{
m_data = new char[1];
m_data[0] = '\0';
}
else //如果输入非空串,复制该字符串。
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}
/*构造函数之二*/
String::String(const String& another)
{
m_data = new char[strlen(another.m_data) + 1];
st
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
/*创建一个字符串类:String*/
class String{
public:
String(const char* str = NULL); //构造函数之一
String(const String& another); //构造函数之二
void input_str(); //字符串输入
String& operator +(const String& rhs); //重载加法运算符
void print(); //字符串输出
bool find(char ch);
bool find(const String &ss);
void show();
friend ostream& operator<<(ostream &os, const String &s);
friend istream& operator>>(istream &is, String &s);
private:
char *m_data;
char *addstr;
};
/*构造函数之一*/
String::String(const char* str)
{
if(str == NULL) //如果输入为空字符串,则添加“\0”表示空串。
{
m_data = new char[1];
m_data[0] = '\0';
}
else //如果输入非空串,复制该字符串。
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}