void displayRectangle(Rectangle r)
{
cout << "Length = " << r.getLength() << endl;
cout << "Width = " << r.getWidth() << endl;
cout << "Area = " << r.getArea() << endl; '
}
Rectangle box(15, 10);
displayRectangle(box);
Length = 15
Width = 10
Area = 150
#include <iostream> #include <iomanip> #include <string> using namespace std; class InventoryItem { private: int partNum; // Part number string description; // Item description int onHand; // Units on hand double price; // Unit price public: void storeInfo(int p, string d, int oH, double cost); // Prototype int getPartNum() { return partNum; } string getDescription() { return description; } int getOnHand() { return onHand; } double getPrice() { return price; } }; void InventoryItem::storeInfo(int p, string d, int oH, double cost) { partNum = p; description = d; onHand = oH; price = cost; } //Function prototypes for client program void storeValues (InventoryItem&);// Receives an object by reference void showValues (InventoryItem); // Receives an object by value int main() { InventoryItem part; // part is an Inventoryltem object storeValues(part); showValues(part); return 0; } void storeValues(InventoryItem &item) { int partNum; // Local variables to hold user input string description; int qty; double price; // Get the data from the user cout << "Enter data for the new part number n"; cout << "Part number: "; cin >> partNum; cout << "Description: "; cin.get(); getline (cin, description); cout << "Quantity on hand: "; cin >> qty; cout << "Unit price: "; cin >> price; item.storeInfo(partNum, description, qty, price); } void showValues(InventoryItem item) { cout << fixed << showpoint << setprecision(2) << endl; cout << "Part Number : " << item.getPartNum() << endl; cout << "Description : " << item.getDescription () << endl; cout << "Units On Hand: " << item.getOnHand () << endl; cout << "Price : $" << item.getPrice() << endl; }程式輸出結果:
Enter data for the new part number
Part number: 175
Description: Hammer
Quantity on hand: 12
Unit price: 7.49
Part Number : 175
Description : Hammer
Units On Hand: 12
Price : $7.49
void showValues (const InventoryItem&) //函數原型
void showValues (const InventoryItem &item) // 函數頭
double getPrice() const
如果 showValues 嘗試呼叫任何其他 InventoryItem 函數,則會發生編譯器錯誤。請注意,當 showValues 被修改為具有常數參照形參時,只有函數原型和函數頭被更改為包含關鍵字 const。showValues 函數的主體和對 showValues 的呼叫不會改變。InventoryItem storeValues() { InventoryItem templtem; // Inventoryltem 區域性物件 int partNum; //儲存使用者輸入的區域性變數 string description; int qty; double price; //在此編寫獲取使用者輸入的程式碼. //將資料儲存到InventoryItem物件並返回它 tempItem.storeInfo(partNum, description, qty, price); return tempItem; }main 函數隨後應建立 part 語句,如下所示:
InventoryItem part = storeValues();
下面的程式修改了之前的程式,以納入剛才討論的技術。以前名為 storeValues 的函數被重新命名為 createItem,因為它現在會建立一個名為 InventoryItem 的物件並將其返回給 main。showValues 函數現在接收 part 作為常數參照,而不是像以前一樣按值傳遞它。#include <iostream> #include <iomanip> #include <string> using namespace std; class InventoryItem { private: int partNum; // Part number string description; // Item description int onHand; // Units on hand double price; // Unit price public: void storeInfo (int p, string d, int oH, double cost);// Prototype int getPartNum () const { return partNum; } string getDescription() const { return description; } int getOnHand() const { return onHand; } double getPrice() const { return price; } }; void InventoryItem::storeInfo(int p, string d, int oH, double cost) { partNum = p; description = d; onHand = oH; price = cost; } InventoryItem createItem(); // Returns an Inventoryltem object void showValues (const InventoryItem&); int main() { InventoryItem part = createItem (); showValues(part); return 0; } InventoryItem createItem() { InventoryItem tempItem;// Local Inventoryltem object int partNum; // Local variables to hold user input string description; int qty; double price; //Get the data from the user cout << "Enter data for the new part number n"; cout << "Part number: "; cin >> partNum; cout << "Description:"; cin.get(); getline(cin, description); cout << "Quantity on hand: "; cin >> qty; cout << "Unit price: "; cin >> price; tempItem.storeInfo(partNum, description, qty, price); return tempItem; } void showValues(const InventoryItem &item) { cout << fixed << showpoint << setprecision(2) << endl; cout << "Part Number : " << item.getPartNum() << endl; cout << "Description : " << item.getDescription () << endl; cout << "Units On Hand: " << item.getOnHand () << endl; cout << "Price : $"<< item.getPrice () << endl; }程式輸出結果:
Enter data for the new part number
Part number: 175
Description:Hammer
Quantity on hand: 12
Unit price: 7.49
Part Number : 175
Description : Hammer
Units On Hand: 12
Price : $7.49