大佬教程收集整理的这篇文章主要介绍了C++复制构造函数(详解版),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class Address { private: String street; public: Address() {street = " ";} Address(String st) {setStreet(st);} void setStreet(String st){street = st;} String getStreet() const {return street;} };现在可以先创建 Mary 的地址,然后将 Joan 的地址初始化为 Mary 地址的副本,具体代码如下:
Address Mary("123 Main St");
Address joan = Mary;
//This program demonstrates the operation of the default copy constructor. #include <iostream> #include <String> using namespace std; class Address { private: String street; public: Address() {street ="";} Address(String st) {setStreet(st);} void setStreet(String st) {street = st;} String getStreet () const {return street;} }; int main() { // Mary and Joan live at same address Address Mary("123 Main St"); Address joan = Mary; cout << "Mary lives at " << Mary.getStreet() << endl; cout << "Joan lives at " << joan.getStreet() << endl; // Now Joan moves out joan.setStreet("1600 Pennsylvania Ave"); cout << "Now Mary lives at " << Mary.getStreet() << endl; cout << "Now Joan lives at " << joan.getStreet() << endl; return 0; }程序输出结果:
Mary lives at 123 Main St
Joan lives at 123 Main St
Now Mary lives at 123 Main St
Now Joan lives at 1600 Pennsylvania Ave
class numberArray { private: double *aPtr; int arraySize; public: numberArray(int size,double value); // ~numberArray(){ if(arraySize > 0) delete [] aPtr;} void print() const; void SETVALue(double value); };以上的类代码封装了 double 类型数字的数组(在真实的程序中也可能有类的其他成员)。为了使不同大小的数组具有灵活性,类包含一个指向数组的指针,而不是直接包含数组本身。
delete []
语句来解除分配数组,但是目前为了避免由默认的复制构造函数引起的问题而被注释掉。后面将很快指出这些问题的具体性质。//numberArray. h 的内容 #include <iostream> using namespace std; class numberArray { private: double *aPtr; int arraySize; public: numberArray(int size,double value); // ~numberArray(){if (arraySize > 0)delete [ ] aPtr;} // Commented out to avoid problems with the default copy constructor void print() const; void SETVALue(double value); }; //numberArray. cpp 的内容 #include <iostream> #include "numberArray.h" using namespace std; numberArray::numberArray(int size,double value) { arraySize = size; aPtr = new double[arraySize]; SETVALue(value); } void numberArray::SETVALue(double value) { for (int index = 0; index < arraySize; index++) aPtr[index] = value; } //Prints all the entries of the array. void numberArray::print() { for(int index = 0; index < arraySize; index++) cout << aPtr [index] << " "; } // main函数的内容 // This program demonstrates the deficiencies of // the default copy constructor. #include <iostream> #include <iomanip> #include "numberArray.h" using namespace std; int main() { // Create an object numberArray first(3,10.5); // Make a copy of the object numberArray second = first; // Display the values of the two objects cout << setprecision(2) << fixed << showpoint; cout << "Value stored in first object is "; first.print(); cout << endl << "Value stored in second object is"; second.print(); cout << endl << "Only the value in second object " << "will be changed." << endl; // Now change the value stored in the second object second.SETVALue(20.5); // Display the values stored in the two objects cout << "Value stored in first object is "; first.print(); cout << endl << "Value stored in second object is "; second.print(); return 0; }程序输出结果:
Value stored in second object is 10.50 10.50 10.50
Value stored in second object is 10.50 10.50 10.50
Only the value in second object will be changed.
Value stored in first object is 20.50 20.50 20.50
Value stored in second object is 20.50 20.50 20.50
以上是大佬教程为你收集整理的C++复制构造函数(详解版)全部内容,希望文章能够帮你解决C++复制构造函数(详解版)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。