当前位置:首页>java>封装(示例代码)

封装(示例代码)

  • 2026-08-24 01:32:43
封装(示例代码)

my_string.h

#pragma once#include<iostream>#include<cstring>using namespace std;class MyString{public:    static const int npos = -1;    // 手动初始化    voidinit(char *s);    /**     * 构造函数(解决手动初始化问题)     * 1、作用:初始化成员变量(属性)--->防止内存泄漏(成员变量有指针成员)     * 2、特点:无返回值/函数名和类名相同/可以重载/自动调用(实例化对象)     * 3、种类     * a/无参构造函数     * 默认无参构造函数:系统自动生成;若类中没有定义其它构造函数,系统默认生成     * b/有参构造函数:拷贝构造函数、类型转换构造函数、移动拷贝构造函数     * explicit:禁止发生隐式类型转换 --- 修饰带一个参数的构造函数     * 默认拷贝构造函数(浅拷贝):若类中没有定义拷贝构造函数,系统会默认生成;     * ---------缺点(浅拷贝(成员变量里有指针成员)-----导致指针指向的同一空间被释放多次;--- 内存错误!):------实现方式:对象之间的成员互相赋值     * 解决问题的方式:     * 自定义实现拷贝构造函数:实现深拷贝     * 拷贝构造函数     */    MyString();                       // 无参构造函数    explicitMyString(constchar *p); // 有参构造函数    explicitMyString(constint &num);    // 有指针成员的时候要用自定义拷贝构造函数,实现深拷贝    // MyString(const MyString &other); // 拷贝构造函数    MyString(MyString &other);  // 拷贝构造函数    MyString(MyString &&other); // 移动拷贝构造函数(优化了,但不同编译器):实现深拷贝,必须要有移动拷贝    /**     * 默认的 = 运算符重载函数:浅拷贝     * 移动 = 运算符重载     */    /**     * 运算符重载:     * 1、运算符重载的作用?代码更加简洁、提高了代码的可读性、体现了C++的可扩充性、运算符重载仅仅只是语法上的方便,它是另一种函数调用的方式、运算符重载本质上是函数重载     * 2、运算符重载的方式?规则?     * 方式:成员函数重载和友元函数重载     * 规则:不允许发明新的运算符、不能改变运算符操作对象的个数、运算符被重载后其优先级和结核性不会改变     * 3、哪些运算符不能被重载:作用域解析运算符::、条件运算符? :、直接访问运算符.、类成员指针引用运算符.*、sizeof运算符     * 4、流运算符为什么只能友元方式重载:左侧运算量是cin或cout而不是对象本身,所以不满足后面一点,就只能申明为友元函数了     * 5、需要会实现一些特殊的运算符重载: ->、*、i++、++i、类型转换运算符    */    // 等号运算符重载函数    // MyString operator=(const MyString &other);    MyString &operator=(MyString &other);  // 接左值:拷贝    MyString &operator=(MyString &&other); // 接右值:移动    /**     * 析构函数     * 1、作用:释放对象成员变量所指向的内存空间,防止内存泄漏     * 2、特点:自动调用(对象离开作用域)/函数名:~类名/无返回值/不能重载     */    // 运算符重载==    bool operator==(const MyString &other); // 成员函数要调用const、non_const版本    bool operator>(const MyString &other);    bool operator>(const MyString &other) const;    // friend bool operator==(const MyString &s1, const MyString &s2); // 友元函数    ~MyString();    // friend bool operator>(const MyString &s1, const MyString &s2);    char operator[](const int index);    char operator[](const int index) const;    friend MyString operator+(const MyString &s1, const MyString &s2);    friend ostream &operator<<(ostream &out, const MyString &s);    friend istream &operator>>(istream &in, MyString &s);    // 类型转换运算符重载    operatorint()    {        return atoi(m_p);    }    // new / delete    void *operatornew(size_t size)    {        cout << "operator new" << end;        void *p = malloc(size);        return p;    }    voidoperatordelete(void *p)    {        cout << "operator delete" << endl;        free(p);        p = nullptr;    }    // 成员函数    // 插入    // 删除    // 替换    // 查找    // 属性    // 迭代器    // 内置类:只能通过外类使用    class Iterator    {    public:        Iterator(char *p) : m_p(p)        {        }        Iterator &operator++()        {            m_p++;            return *this;        }        Iterator operator++(int n)        {            Iterator temp(m_p);            m_p++;            return temp;        }        char operator*()        {            return *m_p;        }        bool operator!=(const Iterator &other)        {            return m_p != other.m_p;        }        bool operator==(const Iterator &other)        {            return m_p == other.m_p;        }    private:        char *m_p;    };    typedef Iterator iterator;    typedef const char *const_iterator;    class Reverse_iterator    {    public:        Reverse_iterator(char *p) : m_p(p)        {        }        Reverse_iterator &operator++()        {            m_p--;            return *this;        }        Reverse_iterator operator++(int n)        {            Reverse_iterator temp(m_p);            m_p--;            return temp;        }        bool operator!=(const Reverse_iterator &other)        {            return m_p != other.m_p;        }        bool operator==(const Reverse_iterator &other)        {            return m_p == other.m_p;        }        char operator*()        {            return *m_p;        }    private:        char *m_p;    };    typedef Reverse_iterator reverse_iterator;    iterator begin();    iterator end();    const_iterator cbegin();    const_iterator cend();    reverse_iterator rbegin();    reverse_iterator rend();    intsize();    constchar *c_str();    // 运算符重载private:    char *m_p; // 野指针};

my_string.cpp

#include"my_string.h"MyString::MyString(){    cout << "MyString" << endl;    m_p = nullptr;}MyString::MyString(const char *p){    cout << "MyString char *" << endl;    // m_p = p;    if (p != nullptr)    {        int len = strlen(p);        m_p = new char[len + 1];        memcpy(m_p, p, len);    }    else    {        m_p = nullptr;    }}MyString::MyString(const int &num){    cout << "MyString int" << endl;    m_p = nullptr;}MyString::MyString(MyString &other) // 深拷贝{    cout << "MyString copy" << endl;    if (other.m_p != nullptr)    {        int len = strlen(other.m_p);        m_p = new char[len + 1];        memcpy(m_p, other.m_p, len);    }    else    {        m_p = nullptr;    }}MyString::MyString(MyString &&other){    cout << "MyString move copy" << endl;    m_p = other.m_p;    other.m_p = nullptr;}MyString::~MyString(){    cout << "~MyString" << endl;    if (m_p != nullptr) // 不能释放空    {        delete[] m_p;    }    m_p = nullptr;}MyString &MyString::operator=(MyString &other){    cout << "MyString operator = " << endl;    // m_p = other.m_p; // 浅拷贝    if (other.m_p != nullptr)    {        int len = strlen(other.m_p);        m_p = new char[len + 1];        memcpy(m_p, other.m_p, len);    }    else    {        m_p = nullptr;    }    return *this; // 返回当前对象的返回值(copy)}MyString &MyString::operator=(MyString &&other){    cout << "MyString operator move = " << endl;    m_p = other.m_p; // 浅拷贝    other.m_p = nullptr;    return *this; // 返回当前对象的返回值(copy)}voidMyString::init(char *s){    m_p = s;}intMyString::size(){    return strlen(m_p);}constchar *MyString::c_str(){    return m_p;}MyString::iterator MyString::begin(){    return m_p;}MyString::iterator MyString::end(){    return m_p + size();}MyString::const_iterator MyString::cbegin(){    return m_p;}MyString::const_iterator MyString::cend(){    return m_p + size();}MyString::reverse_iterator MyString::rbegin(){    return Reverse_iterator(m_p + size());}MyString::reverse_iterator MyString::rend(){    return Reverse_iterator(m_p - 1);}// 运算符重载bool MyString::operator==(const MyString &other){    return strcmp(this->m_p, other.m_p) == 0 ? true : false; }bool MyString::operator>(const MyString &other){    return strcmp(this->m_p, other.m_p) > 0 ? true : false; }bool MyString::operator>(const MyString &other) const{    return strcmp(const_cast<MyString *>(this)->m_p, other.m_p) > 0 ? true : false; }char MyString::operator[](const int index){    return m_p[index];}char MyString::operator[](const int index) const{    return (const_cast<MyString *>(this))->m_p[index];}MyString operator+(const MyString &s1, const MyString &s2){    int len1 = 0;    int len2 = 0;    MyString temp;    if (s1.m_p != nullptr)    {        len1 = strlen(s1.m_p);    }    if (s2.m_p != nullptr)    {        len2 = strlen(s2.m_p);    }    if (len1 + len2 > 0)    {        temp.m_p = new char[len1 + len2 + 1];        memset(temp.m_p, 0, len1 + len2 + 1);        strcat(temp.m_p, s1.m_p);        strcat(temp.m_p, s2.m_p);    }    return temp;}ostream &operator<<(ostream &out, const MyString &s){    out << "data: ";    out << s.m_p;    return out;}istream &operator>>(istream &in, MyString &s){    string s1;    in >> s1;    int len = strlen(s1.c_str());    s.m_p = new char[len + 1];    memcpy(s.m_p, s1.c_str(), len);    return in;}// bool operator==(const MyString &s1, const MyString &s2)// {//     return strcmp(s1.m_p, s2.m_p) == 0 ? true : false;// }// bool operator>(const MyString &s1, const MyString &s2)// {//     return strcmp(s1.m_p, s2.m_p) > 0 ? true : false;// }

main.cpp

#include <iostream>#include "my_string.h"using namespace std;struct Student{    int m_num;    string m_name;    int age;};ostream &operator<<(ostream &out, const Student &stu){    out << "name = " << stu.m_name << endl;    out << "num = " << stu.m_num << endl;    out << "age = " << stu.age << endl;    return out;}// 左右值引用:优点:解决了常引用的缺点voidtest(MyString &&str) // 用引用减少不必要的拷贝(接右值){}voidtest(MyString &str) // 接左值{}MyString get_str(){    MyString s1("hello");    return s1;}// 缺点:由于有const限定,在函数体内就不能有修改str的操作// void test(const MyString &str)// {// }voidtest(const MyString &s1, const MyString s2){    if (s1 > s2)    {    }}intmain(int argc, char **argv){#if 0    // MyString s1; // 对象存储模型,对象的大小是成员变量总和;成员函数被所有该类的对象共享的    MyString s1; // 自动调用构造函数:MyString();    char *p1 = new char[100]{"hello2"};    {        MyString s2(p1); // MyString(char *p);    }    MyString s3(p1);    // s1.init("hello world"); // 手动初始化;缺点:忘记初始化,后续使用容易造成内存泄漏    // s1.init(p1);    string temp = "hello";    MyString s4(temp.data());    // MyString s5 = "hello world"; // 发生了隐式类型转换    // 缺点:不安全;歧义:误认s5是char *类型    // MyString s6 = 100;    MyString s5("hello world");    MyString s6(5);    // 什么时候调用拷贝构造函数?用已有对象初始化新的对象    MyString s7(s3); // MyString(MyString &other);#endif#if 0    char *p1 = new char[100]{"hello world"};    MyString s1(p1);    MyString s2(s1);    test(s2); // 按引用传递,减少不必要的拷贝    MyString s3;    // s3 = s1; // 不调用拷贝构造函数,实际调 = 运算符重载函数    s3.operator=(s1);#endif#if 0    // 匿名对象/临时对象    // 左值:可以取地址/生命周期长    // 右值:不可以取地址/生命周期短(一条语句长度)    // 左值引用:只能绑定左值/右值引用:只能绑定右值    MyString("hello world");    MyString s1("hello2");    MyString s2;    s2 = MyString("hello"); // 移动(临死的对象还调用了深拷贝)    test(s1);                // 左值    test(MyString("hello")); // 右值引用不能绑定在左值上,加const可以#endif    /**     * 引用:减少不必要的拷贝(函数传参)---左值引用     * 右值引用:减少由临时对象产生的不必要的拷贝问题    */#if 0    char *p = new char[100]{"hello"};    MyString s1(p);    MyString s2(s1);    MyString s3(MyString(p));#endif#if 0    MyString temp = get_str(); // -fno-elide-constructors (关闭函数返回值优化)#endif#if 0    MyString s1("hello");    MyString s2("world");    if (s1 == s2)    // if (s1.operator==(s2))    // if (operator==(s1, s2))    {        cout << "s1 == s2" << endl;    }    else    {        cout << "s1 != s2" << endl;    }    // if (s1 > s2)    if (s1.operator>(s2))    {        cout << "s1 > s2" << endl;    }    else    {        cout << "s1 < s2" << endl;    }    test(s1, s2);    MyString s3;    s3 = s1 + s2;    cout << s3.c_str() << endl;    cout << s3 << endl; // 流运算符只能用友元函数重载,因为流运算符左操作数不是自定义类型,而是cout对象    // operator<<(operator<<(cout, s3), endl)    Student stu = {1, "zhangsan", 20};    cout << stu;    MyString temp;    cin >> temp;    cout << temp << endl;    cout << temp[2] << endl;#endif#if 0    MyString s1("hello");    MyString s2("HELLO");    for (auto it = s1.begin(); it != s1.end(); it++)    {        cout << *it << endl;    }    for (auto it = s2.rbegin(); it != s2.rend(); ++it)    {        cout << *it << endl;    }#endif    MyString s("123");    int num = static_cast<int>(s);    cout << num << endl;    return 0;}

最新文章

随机文章