2024.4.25

练习添加拷贝赋值函数

#include <iostream>
using namespace std;
class Person
{
    const string name;
    int age;
    char sex;
public:
    Person():name("zhangsan")
    {}
    Person(string name,int age,char sex):name(name),age(age),sex(sex)
    {}
    ~Person()
    {
        cout << "Person的析构函数" << endl;
    }
    void show()
    {
        cout << "Person中的show: " << name << "\t" << age << "\t" << sex << endl;
    }
    Person(const Person &other):name(other.name),age(other.age),sex(other.sex)
    {}

    //拷贝赋值函数
    Person &operator=(const Person &other)
    {
        this->age = other.age;
        this->sex = other.sex;
        return *this;
    }
};
class Stu
{
    Person p1;
    double *pd;  //指针指向堆区的空间
public:
    Stu():pd(new double)
    {}
    //第四个参数是double类型的变量,在给pd申请空间时使用该变量初始化
    Stu(string name,int age,char sex,double score):p1(name,age,sex),pd(new double(score))
    {}
    ~Stu()
    {
        cout << "准备释放空间:" << pd << endl;
        delete pd;
        cout << "Stu的析构函数" << endl;
    }
    void show()
    {
        p1.show();  //通过Person类型的p1成员,调用Person中的show函数
        cout << "Stu中的show:" << *pd << endl;
    }
    //实现Stu的拷贝构造,调用其他类的拷贝构造函数时,也需要传同类引用
    Stu(const Stu &other):pd(new double(*(other.pd))),p1(other.p1)
    {}
    //拷贝赋值函数
    Stu &operator=(const Stu &other)
    {
        this->p1=other.p1;
        *(this->pd) = *pd;
        return *this;
    }
};
int main()
{
    Stu s1("zhangsan",20,'m',23.9);
    s1.show();
    Stu s2("lisi",19,'f',77);
    s2.show();
    s2 = s1;
    s2.show();
    cout << endl;

    Person p1("xx",18,'m');
    p1.show();
    Person p2("yy",19,'f');
    p2.show();
    p2=p1;
    p2.show();
    return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:
    char *str;          //记录c风格的字符串
    int size;            //记录字符串的实际长度
public:
    //无参构造
    myString():str(new char)
    {}
    //有参构造
    myString(char *str,int size):str(new char),size(size)
    {}
    //拷贝构造
    myString(const myString &other):str(new char(*(other.str))),size(other.size)
    {}
    //拷贝赋值函数
    myString &operator=(const myString &other)
    {
        *(this->str)=*(other.str);
        this->size = other.size;
    }
    //析构函数
    ~myString()
    {
        delete []str;
    }
    //判空函数
    void empty(myString &other)
    {
        if(other.str==nullptr)
        {
            cout << "指针为空" << endl;
        }
    }
    //size函数
    int mysize()
    {
        int c = 0;
        while(this->str[c]!='\0')
        {
            c++;
        }
        c++;
        return c;
    }
    //c_str函数
    char* myc_str(string s,myString &other)
    {
        strcpy(other.str,s.c_str());
        return other.str;
    }
    //at函数
    char &at(int pos)
    {
        cout << this->str[pos] <<endl;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值