C++通过深拷贝函数申请动态内存来存放数据

 题目要求如下:

定义一个course类,该类中有私有数据成员char*型指针
introduction和整型课号ID,要求在构造函数里申请动态内存来存
放课程介绍,并实现该类的拷贝构造函数。

实现拷贝功能的函数如下:

  1. Course(const Course& P) //拷贝函数,实现深拷贝
  2.     {
  3.         Introduction = new char(*P.Introduction);        //通过使用new来申请动态内存
  4.         ID = P.ID;
  5.         introduction = new char(*P.introduction);        //通过使用new来申请动态内存
  6.         id = P.id;
  7.     }
  8. 实现动态内存释放的析构函数如下:
  9. ~Course()   //析构函数
  10.     {
  11.         if (introduction != NULL)
  12.         {
  13.             delete introduction;        //释放new申请的动态内存
  14.             introduction = NULL;
  15.         }
  16.         if (Introduction != NULL)
  17.         {
  18.             delete Introduction;        //释放new申请的动态内存
  19.             Introduction = NULL;
  20.         }
  21.     }
  22. 代码运行过程:第一次传递数值是调用默认构造函数,当把第一次传递的参数要拷贝时,调用盛拷贝函数。例如:
  23. Course P1(ch, n);
  24.     cout << "P1 Introduction:" << *P1.introduction << "     P1 ID:" << P1.id << endl;
  25. 执行之一过程是不会调用深拷贝函数,当进行一下步骤时才会调用深拷贝函数
  26. Course P2(P1);
  27.     cout << "P2 Introduction:" << *P2.introduction<< "     P2 ID:" << P2.id<< endl;
  28. 要注意输出是要用“*”符号获取该地址上的有关参数。

完整的代码如下:仅供参考


  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. //定义Course类
  5. class Course
  6. {
  7. private:    //定义两个私有变量Introduction和ID
  8.     char* Introduction;
  9.     int ID;
  10. public:     //定义公有变量
  11.     char* introduction;
  12.     int id;
  13.     Course(char x, int n)   //有参构造函数
  14.     {
  15.         Introduction = new char(x);
  16.         ID = n;
  17.         introduction = new char(x);
  18.         id = n;
  19.     }
  20.     ~Course()   //析构函数
  21.     {
  22.         if (introduction != NULL)
  23.         {
  24.             delete introduction;        //释放new申请的动态内存
  25.             introduction = NULL;
  26.         }
  27.         if (Introduction != NULL)
  28.         {
  29.             delete Introduction;        //释放new申请的动态内存
  30.             Introduction = NULL;
  31.         }
  32.     }
  33.     Course(const Course& P) //拷贝函数,实现深拷贝
  34.     {
  35.         Introduction = new char(*P.Introduction);
  36.         ID = P.ID;
  37.         introduction = new char(*P.introduction);
  38.         id = P.id;
  39.     }
  40. };
  41. void test1()
  42. {
  43.     char ch;
  44.     int n;
  45.     cin >> ch >> n; //第一次输入ch和n
  46.     Course P1(ch, n);
  47.     cout << "P1 Introduction:" << *P1.introduction << "     P1 ID:" << P1.id << endl;
  48.     Course P2(P1);
  49.     cout << "P2 Introduction:" << *P2.introduction<< "     P2 ID:" << P2.id<< endl;
  50.     cin >> ch >> n; //第二次输入ch和n
  51.     Course P3(ch, n);
  52.     cout << "P3 Introduction:" << *P3.introduction<< "     P3 ID:" << P3.id << endl;
  53.     Course P4(P3);
  54.     cout << "P4 Introduction:" << *P4.introduction << "     P4 ID:" << P4.id << endl;
  55. }
  56. int main()
  57. {
  58.     test1();
  59.     return 0;
  60. }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨露02

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值