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