C++ day3

 

#include <iostream>
using namespace std;

class Person
{
    const string name;
    int age;
    char sex;
public:
    Person():name("lisi"),age(19),sex('m'){}
    Person(string name,int age,char sex):name(name),age(age),sex(sex){}
    ~Person(){}
    void show()
    {
        cout << name << "\t" << age << "\t" << sex << "\t" << endl;
    }
    Person(const Person &other):name(other.name)
    {
      this->age=other.age;
        this->sex=other.sex;
    }
    Person &operator=(const Person &other)
    {
        if(&other!=this)
        {
            this->age=other.age;
            this->sex=other.sex;
        }
    }
};
class Stu
{
    Person p1;
    double *p;
public:
    Stu():p(new double){}
    Stu(string name,int age ,char sex,double score):p1(name,age,sex),p(new double(score)){}

    ~Stu()
    {
        delete p;
    }
    void show()
    {
        p1.show();
        cout << *p << endl;
    }
    Stu(const Stu &other):p(new double(*(other.p))),p1(other.p1){}
    Stu &operator=(const Stu &other)
    {
        if(&other!=this)
        {
            this->p1=other.p1;
            *(this->p)=*(other.p);
        }

    }
};
int main()
{
\
    Person s1;
    s1.show();
    Stu s2("zhangsan",20,'m',60);
    s2.show();
    Stu s3=s2;
    s3.show();
    Stu s4=s2;
    s4.show();

    return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
class myString
{
    private:
        char *str;          //记录c风格的字符串
        int size;            //记录字符串的实际长度
    public:
        //无参构造
        myString():size(10)
        {
            str=new char(size);
            strcpy(str,"");
            cout << "无参构造" << endl;
        }
        //有参构造
        myString(const char *s)
        {
            size = strlen(s);
            str = new char[size+1];
            strcpy(str,s);
            cout << "有参构造" << endl;
        }
        //拷贝构造
        myString(const myString &other):str(new char[other.size+1]),size(other.size)
        {
            strcpy(this->str,other.str);
            cout << "拷贝构造" << endl;
        }
        //拷贝赋值函数
        myString &operator=(const myString &other)
        {
            if(this!=&other)
            {
                char *p=other.str;
                char *q=this->str;
                while (*p!='\0') {
                    *q=*p;
                    q++;
                    p++;
                }
                this->size=other.size;
            }
            cout << "拷贝赋值" << endl;
            return *this;
        }
        //析构函数
        ~myString()
        {
            delete[]str;
            cout << "析构函数" << endl;
        }
        //判空函数
        bool empty()
        {
            if(this->size==0)
                return true;
            else
                return false;
        }
        //size函数
        int my_size()
        {
            return this->size;
        }
        //c_str函数
        char *c_str()
        {
            return this->str;
        }
        //at函数
        char &at(int pos)
        {
            return *(str+pos);
        }
};
int main(){
    //调用无参构造
    myString s1;
    cout << "my_size=" << s1.my_size() << "\t" << "str1=" << s1.c_str() << endl;
    //调用有参构造
    myString s2("hello");
    cout << "my_size=" << s2.my_size() << "\t" << "str2=" << s2.c_str() << endl;
    //调用拷贝构造
    myString s3(s2);
    cout << "my_size=" << s3.my_size() << "\t" << "str3=" << s3.c_str() << endl;
    //调用at函数
    cout << "第一个字符:" << s3.at(0) << endl;
    cout << "第二个字符:" << s3.at(1) << endl;
    cout << "第三个字符:" << s3.at(2) << endl;
    //调用有参构造
    myString s4("");
    cout << "my_size=" << s4.my_size() << "\t" << "str4=" << s4.c_str() << endl;
    //调用empty函数
    if(s3.empty())
        cout << "s3字符串为空" << endl;
    else
        cout << "s3字符串不为空" << endl;
    if(s4.empty())
        cout << "s4字符串为空" << endl;
    else
        cout << "s4字符串不为空" << endl;
    //调用赋值构造
    s4=s3;
    cout << "my_size=" << s4.my_size() << "\t" << "str4=" << s4.c_str() << endl;

    return 0;
}

相关推荐

  1. MSc CDA Take-Home

    2024-04-25 21:38:02       61 阅读
  2. CDA一级备考策略分享

    2024-04-25 21:38:02       32 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-04-25 21:38:02       50 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-04-25 21:38:02       37 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-04-25 21:38:02       79 阅读
  6. web server apache tomcat11-33-CDI

    2024-04-25 21:38:02       34 阅读
  7. Spring Data访问 MongoDB(十六)----CDI集成

    2024-04-25 21:38:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-25 21:38:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-25 21:38:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-25 21:38:02       87 阅读
  4. Python语言-面向对象

    2024-04-25 21:38:02       96 阅读

热门阅读

  1. 传输层协议

    2024-04-25 21:38:02       25 阅读
  2. 跨域问题+解决跨域express

    2024-04-25 21:38:02       37 阅读
  3. 如何使用PHP进行邮件发送?

    2024-04-25 21:38:02       35 阅读
  4. 【MHA】MySQL高可用MHA介绍2-安装,配置,要求与限制

    2024-04-25 21:38:02       24 阅读
  5. C#多线程之(Thread)详解与示例

    2024-04-25 21:38:02       30 阅读
  6. MacOS - 打开 App 无法验证开发者解决方案

    2024-04-25 21:38:02       32 阅读
  7. C语言面经

    2024-04-25 21:38:02       31 阅读
  8. Linux错误(3)Linux里IP套接字sendmsg出现EPERM错误

    2024-04-25 21:38:02       24 阅读
  9. Node.js

    2024-04-25 21:38:02       30 阅读
  10. 什么是Spring容器中的组件

    2024-04-25 21:38:02       32 阅读
  11. 快速安装protoc

    2024-04-25 21:38:02       35 阅读
  12. 【使用 SLF4J 进行日志记录】

    2024-04-25 21:38:02       36 阅读