Circle circle[4];
這 4 個物件是 circle[0]、circle[l]、circle[2] 和 circle[3]。circle[2].findArea();
下面的程式通過建立和使用 Circle 類物件陣列來演示了上述操作。以下是它使用的 Circle 類的定義:// Thisheader file contains the Circle class declaration. #ifndef CIRCLE_H #define CIRCLE_H #include <cmath> class Circle { private: double radius; // Circle radius int centerX, centerY; // Center coordinates public: Circle() // Default constructor { // accepts no arguments radius = 1.0; centerX = centerY = 0; } Circle(double r) // Constructor 2 { // accepts 1 argument radius = r; centerX = centerY = 0; } Circle(double r, int x, int y) // Constructor 3 { // accepts 3 arguments radius = r; centerX = x; centerY = y; } void setRadius(double r) { radius = r; } int getXcoord() { return centerX; } int getYcoord() { return centerY; } double findArea() { return 3.14 * pow(radius, 2); } }; // End Circle class declaration #endif在接下來的程式中,需要特別注意其關鍵部分,在第 5 行中包含了 Circle.h 標頭檔案,在該檔案中包含了 Circle 類定義。然後是在第 11 行中,建立了一個由 4 個 Circle 物件組成的陣列。在第 13?19 行中的迴圈為每個物件呼叫 setRadius 方法。在第 23?26 行使用第 2 個迴圈為每個物件呼叫 findArea 方法並顯示結果。
// This program uses an array of objects. // The objects are instances of the Circle class. #include <iostream> #include <iomanip> #include "Circle.h" // Circle class declaration file using namespace std; const int NUM_CIRCLES = 4; int main() { Circle circle[NUM_CIRCLES]; // Define an array of Circle objects // Use'a loop to initialize the radius of each object for (int index = 0; index < NUM_CIRCLES; index++) { double r; cout << "Enter the radius for circle " << (index+1) <<":"; cin >> r; circle[index].setRadius(r); } // Use a loop to get and print out the area of each object cout << fixed << showpoint << setprecision(2); cout << "nHere are the areas of the " << NUM_CIRCLES << "circles . n"; for (int index = 0; index < NUM_CIRCLES; index++) { cout << "circle " << (index+1) << setw (8) << circle[index].findArea() << endl; } return 0; }程式輸出結果:
Enter the radius for circle 1:0
Enter the radius for circle 2:2
Enter the radius for circle 3:2.5
Enter the radius for circle 4:10
Here are the areas of the 4circles .
circle 1 0.00
circle 2 12.56
circle 3 19.62
circle 4 314.00
Here are the areas of the 4 circles.
circle1 3.14
circle2 3.14
circle3 3.14
circle4 3.14
circle[NUM_CIRCLES] = {0.0, 2.0, 2.5, 10.0};
這將呼叫建構函式接受一個 double 引數,並設定以下半徑值:物件 | 半徑 |
---|---|
circle[0] | 0.0 |
circle[1] | 2.0 |
circle[2] | 2.5 |
circle[3] | 10.0 |
circle[NUM_CIRCLES] = {0.0, 2.0, 2.5};
在下面程式中有該用法演示:// This program demonstrates how an overloaded constructor // that accepts an argument can be invoked for multiple objects //when an array of objects is created. #include <iostream> #include <iomanip> #include "Circle.h" // Circle class declaration file using namespace std; const int NUM_CIRCLES = 4; int main() { // Define an array of 4 Circle objects. Use an initialization list // to call the 1-argument constructor for the first 3 objects. // The default constructor will be called for the final object. Circle circle[NUM_CIRCLES] = {0.0, 2.0, 2.5}; // Display the area of each object cout << fixed << showpoint << setprecision (2); cout << "Here are the areas of the " << NUM_CIRCLES << " circles . n"; for (int index = 0; index < NUM_CIRCLES; index++) { cout << "circle " << (index+1) << setw (8) << circle[index].findArea() << endl; } return 0; }程式輸出結果:
Here are the areas of the 4 circles .
circle 1 0.00
circle 2 12.56
circle 3 19.62
circle 4 3.14
Circle circle[3] = {Circle(4.0, 2, 1),Circle(2.0, 1, 3),Circle (2.5, 5, -1) };
circle[0] 的 radius 變數設定為 4.0,centerX 變數設定為 2,centerY 變數設定為 1;circle[1] 的 radius 變數設定為 2.0,centerX 變數設定為 1,centerY 變數設定為 3;circle[2] 的 radius 變數設定為 2.5,centerX 變數設定為 5,centerY 變數設定為 -1。Circle circle [3] = { 4.0,Circle (2.0, 1, 3),2.5 };
該語句為 circle[0] 和 circle[2] 呼叫 1 引數建構函式,而為 circle[1] 呼叫的則是 3 引數建構函式。