c++高级程序设计语言实验十一拷贝构造函数

实验名称

拷贝构造函数

实验目的

  1. 理解何时会调用拷贝构造函数。
  2. 理解何时需要自定义拷贝构造函数。

实验任务

  1. 验证教材第十四章例子程序ch14_01(第307页 修正版第317页)、ch14_02(第308页 修正版第318页)、ch14_3(第310页 修正版第320页)、ch14_04(第311页 修正版第321页)。
  2. 结合习题,理解拷贝构造函数的使用。

14.2(第317页 修正版第327页) ex14_02

写出下面程序的运行结果,请用增加拷贝构造函数的方法避免存在的问题。

#include <iostream>

using namespace std;

class Vector {

  public:

     Vector(int s=100);

     int& Elem(int ndx);

     void Display();

     void Set();

     ~Vector();

  protected:

     int size;

     int* buffer;

};

Vector::Vector(int s) {

  buffer=new int[size=s];

  for(int i=0; i<size; i++)

     buffer[i]=i*i;

}

int& Vector::Elem(int ndx) {

  if(ndx<0||ndx>=size) {

     cout <<"error in index" <<endl;

     exit(1);

  }

  return buffer[ndx];

}

void Vector::Display() {

  for(int j=0; j<size; j++)

     cout <<buffer[j] <<endl;

}

void Vector::Set() {

  for(int j=0; j<size; j++)

     buffer[j]=j+1;

}

Vector::~Vector() {

  //cout << "~Vector begin." << endl;

  delete[]buffer;

  //cout << "~Vector end." << endl;

}

int main() {

  Vector a(10);

  Vector b(a);

  a.Set();

  b.Display();

  return 0;

}

实验内容

  1. ch14_01

#include<iostream>

#include<cstring>

using namespace std;

class Student

{

   char name[40];

   int id;

   public:

      Student(char *pName= "no name",int ssId = 0)

      {

          strncpy(name,pName,40);

          name[39]='\0';

          id=ssId;

          cout << "Constructing new student"<< pName<< endl;

      }

      Student(Student&s)

      {

          cout << "Constructing copy of"<< s.name<<endl;

          strcpy(name,"copy of");

          strcat(name,s.name);

          id=s.id;

      }

      ~Student()

      {

          cout << "Sestructing "<< name << endl;

      }

};

void fn(Student s)

{

   cout << "In function fn() "<< endl;

}

int main()

{

   Student randy("Randy",1234);

   cout << "Calling fn()"<< endl;

   fn(randy);

   cout << "Returned from fn()"<< endl;

   return 0;

}

  1. ch14_02

#include<iostream>

#include<cstring>

using namespace std;

class Student

{

   char name[40];

   int id;

   public:

      Student(char *pName= "no name")

      {

          cout << "Constructing new student"<< pName<< endl;

          strncpy(name,pName,sizeof(name));

          name[sizeof(name)-1]='\0';

      }

      Student(Student&s)

      {

          cout << "Constructing copy of"<< s.name<<endl;

          strcpy(name,"copy of");

          strcat(name,s.name);

      }

      ~Student()

      {

          cout << "Destructing "<< name << endl;

      }

};

class Tutor

{

   Student student;

   public:

      Tutor(Student&s):student(s)

      {

          cout << "Constructing tutor"<< endl;

      }

};

void fn(Tutor tutor)

{

   cout << "In function fn() "<< endl;

}

int main()

{

   Student randy("Randy");

   Tutor tutor(randy);

   cout << "Calling fn()"<< endl;

   fn(tutor);

   cout << "Returned from fn()"<< endl;

   return 0;

}

  1. ch14_03

#include<iostream>

#include<cstring>

using namespace std;

class Person

{

   char * pName;

   public:

      Person(char * pN)

      {

          cout << "Constructing"<<pN<< endl;

          pName= new char[strlen(pN)+1];

          if(pName!=0)

          strcpy(pName,pN);

      }

      ~Person()

      {

          cout <<"Destructing"<< pName<< endl;

          pName[0]='\0';

          delete pName;

      }

};

int main()

{

   Person p1("Randy");

   Person p2= p1;

   return 0;

}

  1. ch14_04

#include<iostream>

#include<cstring>

using namespace std;

class Person

{

   char * pName;

   public:

      Person(char * pN);

      Person(Person &p);

      ~Person();

};

   Person::Person(char * pN)

   {

      cout << "Constructing"<<pN<< endl;

      pName= new char[strlen(pN)+1];

      if(pName!=0)

      strcpy(pName,pN);

   }

   Person::Person(Person& p)

   {

      cout <<"Copying"<< p.pName<<"into its own block"<< endl;

      pName = new char[strlen(p.pName)+1];

      if(pName!=0)

      strcpy(pName,p.pName);

   }

     

   Person::~Person()

   {

      cout <<"Destructing"<< pName<< endl;

      pName[0]='\0';

      delete pName;

   }

int main()

{

   Person p1("Randy");

   Person p2= p1;

   return 0;

}

  1. ex14_02

#include <iostream>

using namespace std;

class Vector {

   public:

      Vector(int s=100);

      Vector(Vector& v);

      int& Elem(int ndx);

      void Display();

      void Set();

      ~Vector();

   protected:

      int size;

      int* buffer;

};

//深拷贝

Vector::Vector(Vector& v)

{

   buffer= new int[size=v.size];

   for(int i=0;i<v.size;i++)

   {

      buffer[i]=i*i;

   }

}

Vector::Vector(int s) {

   buffer=new int[size=s];

   for(int i=0; i<size; i++)

      buffer[i]=i*i;

}

int& Vector::Elem(int ndx) {

   if(ndx<0||ndx>=size) {

      cout <<"error in index" <<endl;

      exit(1);

   }

   return buffer[ndx];

}

void Vector::Display() {

   for(int j=0; j<size; j++)

      cout <<buffer[j] <<endl;

}

void Vector::Set() {

   for(int j=0; j<size; j++)

      buffer[j]=j+1;

}

Vector::~Vector() {

   //cout << "~Vector begin." << endl;

   delete[]buffer;

   //cout << "~Vector end." << endl;

}

int main() {

   Vector a(10);

   Vector b(a);

   a.Set();

   b.Display();

   return 0;

}

小结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值