实验名称 | 拷贝构造函数 |
实验目的 |
|
实验任务 |
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; } |
实验内容
#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; }
#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; }
#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; }
#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; }
#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; } | |
小结 |