以string看C++面向对象程序设计特性

考虑string 类体内应该含有什么?
  • 需要定义的操作,可以从需求中看
#include "string.h"
#include <iostream>

using namespace std;

int main()
{
  String s1("hello"); 
  String s2("world");
    
  String s3(s2);      //执行了一个构造
  cout << s3 << endl;
  
  s3 = s1;    //执行了一个赋值
  cout << s3 << endl;     
  cout << s2 << endl;  
  cout << s1 << endl;      
}
一个题外话

关于string的设计,一个字符指针,指向一片空间连续的区域

  1. 以结束符确定 "\0” C/C++的采用如此方法
  2. 添加长度

类体的定义(三大函数)
class String
{
public:                                 
   String(const char* cstr=0);                 // 默认初始化    
   String(const String& str);      // 拷贝构造                
   String& operator=(const String& str);      //拷贝赋值    
   ~String();    // 当类死亡时,调用析构函数 (如: 离开类的作用域)                                 
   char* get_c_str() const { return m_data; }
private:
   char* m_data;
};

// 定义标准库string所需的操作

    1. 头文件的标准定义格式

#ifdef //任取但不得与标准库冲突
#define // _ 大写 _ H _
……#endif

  • 2.在类体内提供函数的声明
    • String::func_name(……) {   }  //在外定义函数
      
    •     inline  //显示声明为内联函数   
      
  • 3.操作符重载
    • String& operator=(const String& str);
      
    (本质上是函数,函数名operator -运算符-)
  • 4.添加头文件
    ** 如果要在头文件中用到标准库同样要加#include<……> 在就近的位置添加即可 **
  • 5.动态内存分配 new delete
    delete[] 删除分配的动态数组

输出流<< 全局函数
  • 定于为全局函数,将指针丢给cout
#include <iostream>

os operator<<(ostream& os,const String& str)
{
    os<<str.get_c_str();
    return os;
}
// 使用
{
    String s1("hello!");
    cou<<s1<<endl;
}
只要类中带有指针,则必须写出拷贝构造和拷贝赋值

String (const String & str)
String& operator = (const String& str)


//使用示例

    1. 添加头文件 #include“ ”
    1. 声明类对象
      String str;
    1. 构造函数初始化
      string str(“hello NCU!”)
    1. 使用已经重载的 " <<"
      输出类的成元

附上string类的完整实现
#ifndef __MYSTRING__
#define __MYSTRING__

class String
{
public:                                 
   String(const char* cstr=0);                 // 默认初始化    
   String(const String& str);      // 拷贝构造                
   String& operator=(const String& str);      //拷贝赋值    
   ~String();    // 当类死亡时,调用析构函数 (如: 离开类的作用域)                                 
   char* get_c_str() const { return m_data; }
private:
   char* m_data;
};

#include <cstring>

inline
String::String(const char* cstr)
{
   if (cstr) {
      m_data = new char[strlen(cstr)+1];
      strcpy(m_data, cstr);
   }
   else {   
      m_data = new char[1];
      *m_data = '\0';
   }
}

inline
String::~String()
{
   delete[] m_data;
}

inline
String& String::operator=(const String& str)
{
   if (this == &str)
      return *this;

   delete[] m_data;
   m_data = new char[ strlen(str.m_data) + 1 ];
   strcpy(m_data, str.m_data);
   return *this;
}

inline
String::String(const String& str)
{
   m_data = new char[ strlen(str.m_data) + 1 ];
   strcpy(m_data, str.m_data);
}

#include <iostream>
using namespace std;

ostream& operator<<(ostream& os, const String& str)
{
   os << str.get_c_str();
   return os;
}

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值