DAY5, C++(重载运算符问题,类的继承问题)

1.完善my_string类,将能够重载的运算符,全部进行重载;

---mystring.h---头文件
#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>

using namespace std;

class myString
{
private:
    char *str;  //记录C风格字符串
    int size;  //字符串实际长度

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

    //有参构造
    myString(const char *s);

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

    //拷贝赋值,先释放空间
    myString &operator = (const myString &other);

    //析构函数
    ~myString();

    //判空函数
    bool Empty();

    //size函数
    int Size();

    //c_str函数
    char* C_str();

    //at函数
    char &my_at(int pos);

    void show();  //输出size str

    //加号运算符重载,size,str
    const myString operator + (const myString &R)const
    {
        myString temp;//(无参构造)
        temp.size = this->size + R.size;
        temp.str = new char[(temp.size)+1];
        strcpy(temp.str,str);
        delete []str;

        return strcat(temp.str,R.str);
    }

    //加等于运算符重载
    myString &operator += (const myString &R)
    {

        size += R.size;
        char *newstr = new char[size + 1];
        strcpy(newstr,str);
        strcat(newstr,R.str);

        delete [] str;
        str = newstr;

        return *this;
    }

    //关系运算符重载==
    bool operator == (const myString &R)const
    {
        return strcmp(this->str,R.str) == 0;
    }

    //单目运算符重载 &
    const myString *operator &()const
    {
        return this;
    }

    //前置自增(++O)运算符重载,size+1,str+1(指针移动一字符)
    myString &operator ++()
    {
        ++this->size;
        ++this->str;

        return *this;
    }

    //后置自增(O++)运算符重载,存储变化之前的数据
    myString operator ++(int)
    {
        myString temp;
        temp.size = size++;
        temp.str = new char[(temp.size)+1];
        // temp.str = str++;
        strcpy(temp.str,str);

        return temp;
    }

    //中括号[]运算符重载
    char &operator[] (const int index)
    {
        if (index >= 0 && index < size)
        {
            return str[index];
        }else
        {
            // 处理越界情况
            return str[0];
        }
    }

    //重载小括弧运算符
    void operator()(string msg)
    {
        cout<<"msg = "<<msg<<endl;
    }



};



#endif // MYSTRING_H
---mystring.cpp---函数实现
#include "mystring.h"

myString::myString():size(10)  //无参构造
{
     str = new char[size];
     strcpy(str,"");
}


myString::myString(const char *s)  //有参构造
{
    size = strlen(s);
    str = new char[size+1];
    strcpy(str,s);
}

myString::myString(const myString &other)   //拷贝构造
{
    size = other.size;
    str = new char[size+1];
    strcpy(str,other.str);
}
myString &myString::operator = (const myString &other)  //拷贝赋值
{
    if(this != &other)
    {
        delete []str;
        size = other.size;
        str = new char[size+1];
        strcpy(str,other.str);
    }
    return *this;
}

myString::~myString()  //析构函数
{
    delete []str;
}

bool myString::Empty()  //判空函数
{
    return  size == 0;
}

int myString::Size()  //size函数
{
    return strlen(str);
}

char* myString::C_str()  //c_str函数
{
    return str;
}

char & myString::my_at(int pos)  //at函数
{
    if(pos<0 || pos>size-1)
    {
        cout<<"越界访问"<<endl;
        return str[0];
    }
    return str[pos];
}

void myString::show()
{
    cout<<"size= "<<size<<"   str= "<<str<<endl;
}



---main.cpp---主函数
#include "mystring.h"

int main()
{
    myString s1("hello world");  //有参构造
    s1.show();
    cout<<endl;

    myString s2(s1);  拷贝构造
    s2.show();
    cout<<endl;

    myString s4;  //拷贝赋值
    s4 = s1;
    s4.show();

    if(s1.Empty())  // 判空
        cout<<"空串"<<endl;
    cout<<"非空串"<<endl;
    cout<<"s1.Size= "<<s1.Size()<<endl;   //size函数,得到实际大小
    cout<<endl;

    cout<<"s2.C_str= "<<s1.C_str()<<endl;  //c_str函数,得到起始地址
    cout<<endl;

    cout<<"s1.my_at(0)= "<<s1.my_at(0);       //at函数,访问字符
    cout<<"   s1.my_at(1)= "<<s1.my_at(1)<<endl;
    cout<<endl;

    myString s3;        //加号运算符重载
    s3 = s1 + s2;
    s3.show();
    cout<<endl;

    myString s5("nihao");  //加等于运算符重载
    myString s6("hello");
    s5 += s6;
    s5.show();
    cout<<endl;

    if(s1==s2)           //关系运算符重载==
        cout<<"相等"<<endl;
    else
        cout<<"不相等"<<endl;
    cout<<endl;

    cout<<"&s1= "<<&s1<<endl;       //单目运算符重载 &
    cout<<endl;

    ++s6;          //前置自增(++O)运算符重载,size+1,str+1(指针移动一字符)
    s6.show();

    myString s7("zhang");
    s7++;                //后置自增(O++)运算符重载,存储变化之前的数据
    s7.show();
    cout<<endl;

    cout<<"s7[1]= "<<s7[1]<<"  s7[3]= "<<s7[3]<<endl;  //中括号[]运算符重载
    cout<<endl;

    s1("long time");  //重载小括弧运算符


    return 0;
}
结果测试--- 

 

2. 将继承过程中特殊成员函数相关代码;

#include <iostream>

using namespace std;

class Father  //父类
{
protected:
    string name;

public:
    Father() {cout<<"fa无参构造"<<endl;}
    Father(string n):name(n) {cout<<"fa有参构造"<<endl;}
    Father(const Father &other):name(other.name){cout<<"fa拷贝构造函数"<<endl;}
    Father &operator = (const Father &other)
    {
        if(this != &other)
        {
            this->name = other.name;
        }
        cout<<"Father::拷贝赋值函数"<<endl;
        return *this;
    }

    ~Father() {cout<<"Father::析构函数"<<endl;}
};

class Son:public Father  //子类
{
private:
    string toy;

public:
    Son() {cout<<"son无参构造"<<endl;}
    Son(string n,string t):Father(n),toy(t) {cout<<"son有参构造"<<endl;}
    Son(const Son &other):Father(other),toy(other.toy)  //传other
        {cout<<"son拷贝构造函数"<<endl;}

    Son &operator = (const Son &other)
    {
        if(this != &other)
        {
            //显性调用父类的拷贝赋值函数
            Father::operator=(other);  //对继承下来的成员的赋值工作

            this->toy = other.toy;
        }
        cout<<"son::拷贝赋值函数"<<endl;
        return *this;
    }

    void show()
    {
        cout<<"name= "<<name<<"   toy= "<<toy<<endl;
    }

    ~Son() {cout<<"son::析构函数"<<endl;}
};

int main()
{
    Son s1;     //无参构造
    cout<<endl;  

    Son s2("关羽","摩托车");  //有参构造
    cout<<endl;

    Son s3(s2);  //拷贝构造
    s3.show();
    cout<<endl;

    Son s4;
    s4 = s2;     //拷贝赋值
    s4.show();
    cout<<endl;


    return 0;
}

 

3. C++今日思维导图;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值