c++试题,这道题目谁有标准答案的,有的话发给我,谢谢!
来源:百度知道 编辑:UC知道 时间:2024/06/22 19:27:00
编写类String的构造函数、析构函数和赋值函数
已知类String的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
请编写String的上述4个函数。
这道题目谁有标准答案的,有的话发给我,谢谢!
已知类String的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
请编写String的上述4个函数。
这道题目谁有标准答案的,有的话发给我,谢谢!
竟然找答案,小心被告发,百度不允许求答案的。
这里有个完整版:http://hi.baidu.com/_%E2d_%B7%B3_%DE%B2%C2%D2/blog/item/efbfdd18e53108b14aedbc9c.html
#include<iostream>
using namespace std;
class String{
friend ostream& operator<< (ostream&,String&);
public:
String(const char* str=NULL); //赋值构造兼默认构造函数(char)
String(const String &other); //赋值构造函数(String)
String& operator=(const String&other); //operator=
String operator+(const String &other)const; //operator+
bool operator==(const String&); //operator==
char& operator[](unsigned int); //operator[]
size_t size(){return strlen(m_data);};
~String(void) {delete[] m_data;}
private:
char *m_data;
};
inline String::Str