C++_day4

思维导图

代码

cpp 复制代码
#include <iostream>

//设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高
//、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象
//p1,设计这两个类的构造函数、析构函数和拷贝构造函数、拷贝赋值函数。
using namespace std;

class Per
{
private:
    string name;
    int age;
    double *heigh,*weigh;
public:
//    Per()
//    {
//        cout << "Per::无参构造函数" << endl;
//    }
    Per(string name="--",int age=0,double heigh=0.0,double weigh=0.0):name(name),age(age),heigh(new double(heigh)),weigh(new double(weigh))   //构造函数
    {
        cout << "Per::有参构造函数" << endl;
    }
    Per(const Per &other):name(other.name),age(other.age),heigh(new double(*(other.heigh))),weigh(new double(*(other.weigh)))
    {
        cout << "Per::拷贝构造函数" << endl;
    }
    Per &operator=(const Per &other)
    {
        if(this != &other)
        {
            name=other.name;
            age=other.age;
            heigh=other.heigh;
            weigh=other.weigh;
        }
        cout << "Per::拷贝赋值函数" << endl;
        return *this;
    }
    ~Per()
    {
        delete heigh;
        delete weigh;
        cout << "Per::析构函数" << endl;
    }
    void show()
    {
        cout << name << " " << age << " " << *heigh << " " << * weigh << " " ;
    }
};


class Stu
{
private:
    Per p1;
    double score;
public:

    Stu()
    {
      cout << "Stu::无参构造函数" << endl;
    }
    Stu(string name,int age,double heigh,double weigh,double score):p1(name,age,weigh,heigh),score(score)
    {
         cout << "Stu::有参构造函数" << endl;
    }
    Stu(const Stu &other):p1(other.p1),score(other.score)
    {
        cout << "Stu::拷贝构造函数" << endl;
    }
    Stu &operator=(const Stu &other)
    {
        if(this!=&other)
        {
            p1=other.p1;
            score=other.score;
        }
        cout << "Stu::拷贝赋值函数" << endl;
         return *this;
    }
    ~Stu()
    {
        cout << "Stu::析构函数" << endl;
    }
    void show()
    {
        p1.show();
        cout<< score <<endl;
    }
};

int main()
{
   Per p1;
   p1.show();
   cout << endl;
   Per p3("李四",19,199,67);
   p3.show();
   cout << endl << "========================" << endl;
   Stu s3;
   Stu s1("张三",18,189,56,99);
   s1.show();
   cout << "========================" << endl;
   Per p2(p1);
   p2.show();
   cout << endl << "========================" << endl;
   Stu s2(s1);
   s2.show();
   cout << "========================" << endl;
   p1=p3;
   p1.show();
   cout << endl << "========================" << endl;
   s3=s1;
   s1.show();
   cout << "========================" << endl;



    return 0;
}
相关推荐
LXS_35710 分钟前
Day 05 C++ 入门 之 指针
开发语言·c++·笔记·学习方法·改行学it
挂科是不可能出现的2 小时前
最长连续序列
数据结构·c++·算法
mjhcsp3 小时前
C++ int 类型深度解析:从底层实现到实战应用
c++·int
程序员老舅4 小时前
C++参数传递:值、指针与引用的原理与实战
c++·c/c++·值传递·引用传递·指针传递·参数传递机制
liu****5 小时前
8.list的使用
数据结构·c++·算法·list
立志成为大牛的小牛5 小时前
数据结构——二十六、邻接表(王道408)
开发语言·数据结构·c++·学习·程序人生
草莓熊Lotso6 小时前
C++ 方向 Web 自动化测试入门指南:从概念到 Selenium 实战
前端·c++·python·selenium
CoderCodingNo7 小时前
【GESP】C++五级考试大纲知识点梳理, (5) 算法复杂度估算(多项式、对数)
开发语言·c++·算法
星河队长7 小时前
VS创建C++动态库和C#访问过程
java·c++·c#
沐怡旸8 小时前
【穿越Effective C++】条款02:尽量以const, enum, inline替换#define
c++·面试