c++ 类中特殊成员函数

作业:

仿照string类,自己手动实现 My_string,分文件编译

fun.h代码

#ifndef FUN_H
#define FUN_H

#include <iostream>

using namespace std;

class My_string {
private:
    char *ptr;  // 指向字符数组的指针
    int size;   // 字符串的最大容量
    int len;    // 字符串当前长度

    void expand();  // 扩容函数

public:
    // 无参构造
    My_string();

    // 有参构造
    My_string(const char* src);
    My_string(int num, char value);

    // 拷贝构造
    My_string(const My_string& other);

    // 拷贝赋值
    My_string& operator=(const My_string& other);

    // 析构函数
    ~My_string();

    // 判空
    bool empty();

    // 尾插
    void push_back(char value);

    // 尾删
    void pop_back();

    // at函数实现
    char &at(int index);

    // 清空函数
    void clear();

    // 返回C风格字符串
    char *data();

    // 返回实际长度
    int get_length();

    // 返回当前最大容量
    int get_size();
};

#endif // FUN_H

fun.cpp代码

#include "fun.h"

#include <cstring>

// 无参构造
My_string::My_string():size(15)
{
    this->ptr = new char[size];
    this->ptr[0] = '\0';            //表示串为空串
    this->len = 0;
}

// 有参构造
My_string::My_string(const char* src)
{
    len = strlen(src);
    size = len + 1;
    ptr = new char[size];
    strcpy(ptr, src);
}

My_string::My_string(int num, char value) : size(num + 1), len(num)
{
    ptr = new char[size];
    memset(ptr, value, num);
    ptr[num] = '\0';
}

// 拷贝构造
My_string::My_string(const My_string& other) : size(other.size), len(other.len)
{
    ptr = new char[size];
    strcpy(ptr, other.ptr);
}

// 拷贝赋值
My_string& My_string::operator=(const My_string& other)
{
    if (this != &other)
    {
        delete[] ptr;
        size = other.size;
        len = other.len;
        ptr = new char[size];
        std::strcpy(ptr, other.ptr);
    }
    return *this;
}

// 析构函数
My_string::~My_string(){
    delete[] ptr;
}

// 判空
bool My_string::empty(){
    return len == 0;
}

// 尾插
void My_string::push_back(char value){
    if (len + 1 >= size) {
        expand();
    }
    ptr[len++] = value;
    ptr[len] = '\0';
}

// 尾删
void My_string::pop_back(){
    if (len > 0) {
        ptr[--len] = '\0';
    }
}

// at函数实现
char& My_string::at(int index){
    if (index < 0 || index >= len){
        cout << "Index out of range" << endl;
    }
    return ptr[index];
}

// 清空函数
void My_string::clear(){
    len = 0;
    ptr[0] = '\0';
}

// 返回C风格字符串
char* My_string::data(){
    return ptr;
}

// 返回实际长度
int My_string::get_length(){
    return len;
}

// 返回当前最大容量
int My_string::get_size(){
    return size;
}

// 扩容函数
void My_string::expand() {
    size *= 2;
    char* new_ptr = new char[size];
    strcpy(new_ptr, ptr);
    delete[] ptr;
    ptr = new_ptr;
}

main.cpp代码

#include <iostream>
#include "fun.h"

int main() {
    My_string s1;
    cout << "Size = " << s1.get_size() << endl;
    s1.push_back('h');
    s1.push_back('e');
    s1.push_back('l');
    s1.push_back('l');
    s1.push_back('o');

    cout << "s1 = " << s1.data() << endl;
    cout << "Length = " << s1.get_length() << endl;
    cout << "Size = " << s1.get_size() << endl;

    s1.pop_back();
    cout << "s1 = " << s1.data() << endl;

    My_string s2(5,'A');
    cout << "s2 = " << s2.data() << endl;
    My_string s3("hello");
    cout << "s3 = " << s3.at(1) << endl;

    My_string s4 = s2;
    cout << "s4 = " << s4.data() << endl;

    s1 = s3;
    cout << "s1 = " << s1.data() << endl;
    s1.clear();
    cout<<"empty = "<<s1.empty()<<endl;

    for(int i=0;i<30;i++)
    {
        s1.push_back('a');
        cout<<s1.get_size()<<endl;
    }

    return 0;
}

运行结果:

知识梳理:

相关推荐
黑不溜秋的36 分钟前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
Dream it possible!3 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴3 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程3 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空4 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc7745 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白16 小时前
C++——list模拟实现
开发语言·c++
程序员老舅6 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初1116 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
cookies_s_s8 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法