C++day3

本文详细介绍了C++中myString类的构造函数、析构函数、拷贝构造函数、拷贝赋值函数以及相关字符串操作,展示了如何在类中管理内存和重载运算符。
摘要由CSDN通过智能技术生成

1.思维导图

2.

#include <iostream>

using namespace std;

class Person
{
    const string name;
    int age;
    char sex;
public:
    //构造函数
    Person():name("0"),age(0),sex(0)
    {
        cout << "Person无参构造函数" << endl;
    }
    Person(string name,int age,char sex):name(name),age(age),sex(sex)
    {
        cout << "Person有参构造函数" << endl;
    }
    //析构函数
    ~Person()
    {
        cout << "准备释放空间" << endl;
        cout << "Person析构函数" << endl;
        cout << "释放空间完毕" << endl;
        cout << " " << endl;
    }
    //拷贝构造函数
    Person(const Person &other):name(other.name),age(other.age),sex(other.sex)
    {
        cout << "Person拷贝构造函数" << endl;
    }
    //拷贝赋值函数
    Person &operator=(const Person &other)
    {
        if(&other!=this)//判断拷贝类对象和被拷贝类对象是否是同一个类对象
        {

            this->age=other.age;
            this->sex=other.sex;
        }
        cout << "Person拷贝赋值函数" << endl;
        return *this;//返回调用者本身的引用
    }
    void show(void)
    {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "sex:" << sex << endl;
    }
};

class Stu
{
    Person p1;
    double* p;
public:
    //构造函数
    Stu():p1("0",0,0),p(new double(0))
    {
        cout << "Stu无参构造函数" << endl;
    }
    Stu(string name,int age,char sex,double score):p1(name,age,sex),p(new double(score))
    {
        cout << "Stu有参构造函数" << endl;
    }
    //析构函数
    ~Stu()
    {
        cout << " " << endl;
        cout << "准备释放空间" << endl;
        delete p;
        cout << "Stu析构函数" << endl;
        cout << "释放空间完毕" << endl;
    }
    //拷贝构造函数
    Stu(const Stu &other):p1(other.p1),p(new double(*(other.p)))
    {
        cout << "Stu拷贝构造函数" << endl;
    }
    //拷贝赋值函数
    Stu &operator=(const Stu &other)
    {
        if(&other!=this)//判断拷贝类对象和被拷贝类对象是否是同一个类对象
        {
            this->p1=other.p1;
            *(this->p)=*(other.p);
        }
        cout << "Stu拷贝赋值函数" << endl;
        return *this;//返回调用者本身的引用
    }
    void show(void)
    {
        p1.show();
        cout << "score:" << *p << endl;
    }
};

int main()
{
    cout << "---------------s1----------------" << endl;
    Stu s1;
    s1.show();
    cout << "---------------s2----------------" << endl;
    Stu s2("张三",23,'C',60);
    s2.show();
    cout << "---------------s3----------------" << endl;
    Stu s3=s2;
    s3.show();
    cout << "---------------s4----------------" << endl;
    Stu s4("李四",25,'A',98);
    s4.show();
    cout << "---------------s5----------------" << endl;
    Stu s5;
    s5=s4;
    s5.show();
    cout << "Hello World!" << endl;
    return 0;
}

 

 3.

/* 无参构造
 * 有参构造
 * 析构
 * 拷贝构造
 * 拷贝赋值
 * 字符串判空empty
 * 字符串长度str_len
 * 转换成C风格字符串c_str
 * 下标访问at
 * +、关系运算符、逻辑运算符、输入输出运算符的重载
 */
using namespace std;
class myString
{
    char *str;//字符串首地址
    int str_len;//字符串长度
public:
    //无参构造函数
    myString():str(new char(0)),str_len(0)
    {
        cout << "无参构造函数" << endl;
    }
    //有参构造函数
    myString(const char* src):str(new char[strlen(src)]),str_len(strlen(src))
    {
        cout << "有参构造函数" << endl;
        strcpy(str,src);
    }
    //析构函数
    ~myString()
    {
        cout << "析构函数" << endl;
        delete []str;    
    }
    //拷贝构造函数
    myString (const myString &other):str(new char[other.str_len+1]),str_len(other.str_len)
    {
        cout << "拷贝构造函数" << endl;
        strcpy(str,other.str);
    }
    //拷贝赋值函数
    myString &operator=(const myString &other)
    {
        cout << "拷贝赋值函数" << endl;
        if(&other!=this)//判断被拷贝的类对象是否是拷贝类对象本身
        {
            delete []this->str;//释放拷贝空间,防止拷贝类对象申请的空间小于被拷贝类对象申请的空间
            this->str=new char[other.str_len+1];
            strcpy(this->str,other.str);
            this->str_len=other.str_len;
        }
        return *this;
    }
    //字符串判空
    bool my_empty()//无参数,返回值是bool类型
    {
        if(str_len<=0)
        {
            cout << "字符串为空\t";
            return false;
        }
        else
        {
            cout << "字符串不为空\t";
            return true;
        }

    }
    //字符串长度
    int my_size()//无参数
    {
        return str_len;
    }
    //转换成C风格字符串
    char* my_c_str()//无参数
    {
        return str;
    }
    //下标访问字符串
    int my_at(int pos)
    {
        if(pos<0||pos>str_len-1)
        {
            cout << "位置越界\t";
            return -1;
        }
        cout << *(str+pos) << "\t";
        return pos;
    }
    void show(void)
    {
        int i;
        for(i=0;i<str_len;i++)
        {
            cout << *(str+i);
        }
        cout << " " << endl;
    }
    //+运算符重载
    myString operator+(const myString s);
};

myString myString::operator+(const myString s)
{
//    delete []this->str;
//    this->str=new char[this->str_len+s.str_len+1];

    strcat(this->str,s.str);
    this->str_len=this->str_len+s.str_len;
    return *this;
}

int main()
{
    myString s1,s2;
    s1="nihao";//调用了有参构造函数,拷贝赋值函数,析构函数
    s1.show();
    cout << s1.my_empty() << endl;;
    cout << s1.my_size() << endl;
    cout << s1.my_c_str() << endl;;

    cout << s1.my_at(0) << endl;
    cout << s1.my_at(1) << endl;
    cout << s1.my_at(4) << endl;
    cout << s1.my_at(5) << endl;
    s2=s1;
    s2.show();
    myString s3("please speak english");
    s3.show();
    myString s4=s3;
    s4.show();
    myString s5;
    s5=s1+" "+s3;
    s5.show();






    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值