C++提高:String 类实现

简介: C++提高:String 类实现

题目


设计并实现一个 string 类,并重载<<、>>、[]、==、+、=等运算符


代码


#include<iostream>
#include<iomanip>
using namespace std;
class String
{
  friend ostream& operator<< (ostream&,String&);//重载<<运算符 
  friend istream& operator>> (istream&, String&);//重载>>运算符 
public:
  String(const char* str=NULL); // 赋值默认构造函数
  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::String(const char* str)
{
  // 如果初始化的字符串为空 那么初始为0 否则将传入的字符串赋值给当前对象的m_data
  if (!str)
    m_data = 0;
  else
  {
    m_data = new char[strlen(str)+1];
    strcpy(m_data, str);
  }
}
inline String::String(const String &other)
{
  if (!other.m_data)m_data = 0;//在类的成员函数内可以访问同种对象的私有成员(同种类则是友元关系)
  else
  {
    m_data = new char[strlen(other.m_data) + 1];
    strcpy(m_data, other.m_data);
  }
}
inline String& String::operator=(const String& other)
{
  if (this != &other)
  {
    delete[] m_data;
    if (!other.m_data) 
      m_data = 0;
    else
    {
      m_data = new char[strlen(other.m_data) + 1];
      strcpy(m_data, other.m_data);
    }
  }
  return *this;
}
inline String String::operator+(const String &other)const
{
  String newString;
  if (!other.m_data)
    newString = *this;
  else if (!m_data)
    newString = other;
  else
  {
    newString.m_data = new char[strlen(m_data) + strlen(other.m_data) + 1];
    strcpy(newString.m_data, m_data);
    strcat(newString.m_data, other.m_data);
  }
  return newString;
}
inline bool String::operator==(const String &s)
{
  if (strlen(s.m_data) != strlen(m_data))
    return false;
  return strcmp(m_data, s.m_data) ? false : true;
}
inline char& String::operator[](unsigned int e)
{
  if (e >= 0 && e <= strlen(m_data))
    return m_data[e];
}
ostream& operator<<(ostream& os, String& str)
{
  os << str.m_data;
  return os;
}
istream &operator>>(istream &input, String &s)
{
  char temp[255]; //用于存储输入流 
  input >> setw(255) >> temp;
  s = temp; //使用赋值运算符 
  return input; //使用 return 可以支持连续使用>>运算符 
}
int main()
{
  String str1 = "Yang!";
  String str2 = "My friend";
  String str3 = str1 + str2;
  cout << str3 << "\n" << str3.size() << endl;
  return 0;
}
相关文章
|
4天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
4天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
3天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
3天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
7天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
9天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
9天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
9天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
16天前
|
Java API 索引
Java基础—笔记—String篇
本文介绍了Java中的`String`类、包的管理和API文档的使用。包用于分类管理Java程序,同包下类无需导包,不同包需导入。使用API时,可按类名搜索、查看包、介绍、构造器和方法。方法命名能暗示其功能,注意参数和返回值。`String`创建有两种方式:双引号创建(常量池,共享)和构造器`new`(每次新建对象)。此外,列举了`String`的常用方法,如`length()`、`charAt()`、`equals()`、`substring()`等。
15 0