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;
}
相关推荐
H_BB1 分钟前
LRU缓存
数据结构·c++·算法·缓存
charlie1145141911 分钟前
嵌入式现代C++:何时用 C++、用哪些 C++ 特性(折中与禁用项)
开发语言·c++·笔记·学习
历程里程碑2 小时前
LeetCode热题11:盛水容器双指针妙解
c语言·数据结构·c++·经验分享·算法·leetcode·职场和发展
郝学胜-神的一滴2 小时前
使用OpenGL绘制卡通效果的圣诞树
开发语言·c++·程序人生·游戏·图形渲染
Morwit9 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
OliverH-yishuihan10 小时前
开发linux项目-在 Windows 上 基于“适用于 Linux 的 Windows 子系统(WSL)”
linux·c++·windows
七禾页丫10 小时前
面试记录12 中级c++开发工程师
c++·面试·职场和发展
zmzb010311 小时前
C++课后习题训练记录Day56
开发语言·c++
编程小Y11 小时前
C++ Insights
开发语言·c++
王老师青少年编程12 小时前
csp信奥赛C++标准模板库STL案例应用5
c++·stl·set·集合·标准模板库·csp·信奥赛