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;
}
相关推荐
hsjkdhs4 小时前
C++之友元函数与前向引用
开发语言·c++
LoveXming7 小时前
Chapter9—享元模式
java·c++·设计模式·享元模式·开闭原则
9毫米的幻想7 小时前
【Linux系统】—— 环境变量
linux·服务器·c语言·c++
guigu20127 小时前
C++ 面向对象进阶:继承深化与多态详解
开发语言·c++
晚风予卿云月8 小时前
详解STL中stack_queue为什么选择deque作为默认容器
c++·stl·deque·stack_queue
charlie1145141919 小时前
精读C++20设计模式——结构型设计模式:代理模式
c++·学习·设计模式·代理模式·c++20·概论
序属秋秋秋11 小时前
《C++进阶之C++11》【可变参数模板 + emplace接口 + 新的类功能】
c++·笔记·学习·c++11·可变参数模板·emplace系列接口
Pocker_Spades_A11 小时前
C++程序设计上机作业(1)
开发语言·c++
Chen--Xing12 小时前
OpenMP并行化编程指南
c++·密码学·openmp