- struct PayRoll
- {
- int empNumber;
- string name;
- double hours,payRate,grossPay;
- };
PayRoll deptHead, foreman, associate;
它們每一個都是 PayRoll 結構體的實例,可以被分配和擁有自己的內存,以保存其成員數(shù)據(jù)。請注意,盡管 3 個結構體變量具有不同的名稱,但每個變量都包含具有相同名稱的成員,如圖 1 所示。
- struct Date
- {
- int day, month, year;
- };
Date birthday = {23, 8, 1983};
該聲明定義 birthday 是一個 Date 結構體的變量,大括號內的值按順序分配給其成員。所以 birthday 的數(shù)據(jù)成員已初始化,如圖 2 所示。Date birthday = {23,8};
這里只有 day 和 month 成員被初始化,year 成員未初始化。但是,如果某個結構成員未被初始化,則所有跟在它后面的成員都需要保留為未初始化。使用初始化列表時,C++ 不提供跳過成員的方法。以下語句試圖跳過 month 成員的初始化。這是不合法的。Date birthday = {23,1983}; //非法
還有一點很重要,不能在結構體聲明中初始化結構體成員,因為結構體聲明只是創(chuàng)建一個新的數(shù)據(jù)類型,還不存在這種類型的變量。例如,以下聲明是非法的:
- //非法結構體聲明
- struct Date
- {
- int day = 23,
- month = 8,
- year = 1983;
- };
- struct Employee
- {
- string name; // 員工姓名
- int vacationDays, // 允許的年假
- daysUsed; //已使用的年假天數(shù)
- Employee (string n ="",int d = 0) // 構造函數(shù)
- {
- name = n;
- vacationDays = 10;
- daysUsed = d;
- }
- };
- deptHead.empNumber = 475;
- foreman.empNumber = 897;
- associate.empNumber = 729;
- cout << deptHead.empNumber << endl;
- cout << deptHead.name << endl;
- cout << deptHead.hours << endl;
- cout << deptHead.payRate << endl;
- cout << deptHead.grossPay << endl;
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- struct PayRoll
- {
- int empNumber; // Employee number
- string name; // Employee name
- double hours, // Hours worked
- payRate; // Hourly pay rate
- };
- int main()
- {
- PayRoll employee; // Employee is a PayRoll structure
- double grossPay; // Gross amount the employee earned this week
- cout << "Enter the employee1s number:";
- cin >> employee.empNumber;
- cout << "Enter the employee's name: ";
- cin.ignore();// Skip the '\n' character left in the input buffer
- getline(cin, employee.name);
- cout << "Hours worked this week: ";
- cin >> employee.hours;
- cout << "Employee's hourly pay rate: ";
- cin >> employee.payRate;
- // Calculate the employee's gross pay
- grossPay = employee.hours * employee.payRate;
- // Display the results
- cout << "\nHere is the employee1s payroll data:\n";
- cout << "Name: " << employee.name << endl;
- cout << "Employee number: " << employee.empNumber << endl;
- cout << "Hours worked: " << employee.hours << endl;
- cout << "Hourly pay rate: " << employee.payRate << endl;
- cout << fixed << showpoint << setprecision(2);
- cout << "Gross pay: $" << grossPay << endl;
- return 0;
- }
Enter the employee1s number:2214
Enter the employee's name: Jack Smith
Hours worked this week: 40
Employee's hourly pay rate: 12.50
Here is the employee1s payroll data:
Name: Jack Smith
Employee number: 2214
Hours worked: 40
Hourly pay rate: 12.5
Gross pay: $500.00
cin >> employee.empNumber; //正確
如果試圖通過結構體類型的名稱來訪問該成員,那么結果將是錯誤的:cin >> Payroll.empNumber; //錯誤
- struct Costs
- {
- double wholesale;
- double retail;
- };
- struct Item
- {
- string partNum;
- string description;
- Costs pricing;
- };
- widget.partnum = "123A";
- widget.description = "iron widget";
- widget.pricing.wholesale = 100.0;
- widget.pricing.retail = 150.0;
- cout << widget.retail; // 錯誤
- cout << widget.Costs.wholesale; // 錯誤
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- struct CostInfo
- {
- double food, // Food costs
- medical, // Medical costs
- license, // License fee
- misc; // Miscellaneous costs
- };
- struct PetInfo
- {
- string name; // Pet name
- string type; // Pet type
- int age; // Pet age
- CostInfo cost;
- PetInfo() // Default constructor
- {
- name = "unknown";
- type = "unknown";
- age = 0;
- cost.food = cost.medical = cost.license = cost.misc = 0.00;
- }
- };
- int main()
- {
- PetInfo pet;
- pet.name = "Sassy";
- pet.type = "cat";
- pet.age = 5;
- pet.cost.food = 300.00;
- pet.cost.medical = 200.00;
- pet.cost.license = 7.00;
- cout << fixed << showpoint << setprecision(2);
- cout << "Annual costs for my " << pet.age << "—year—old "<< pet.type << " " << pet.name << " are $"<< (pet.cost.food + pet.cost.medical +pet.cost.license + pet.cost.misc) << endl;
- return 0;
- }
程序輸出結果:
Annual costs for my 5-year-old cat Sassy are $507.00
- //程序1
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- struct Invltem// Holds data for an inventory item
- {
- int partNum; // Part number
- string description; // Item description
- int onHand; // Units on hand
- double price; // Unit price
- };
- // Function prototypes
- void getltemData(InvItem &) ;
- void showItem(const InvItem &);
- int main()
- {
- InvItem part; // Define an Invltem structure variable.
- getItemData(part);
- showItem(part);
- return 0;
- }
- void getItemData(InvItem &item)
- {
- cout << "Enter the part number: ";
- cin >> item.partNum;
- cout << "Enter the part description: ";
- cin.get();
- getline (cin, item.description);
- cout << "Enter the quantity on hand: ";
- cin >> item.onHand;
- cout << "Enter the unit price: ";
- cin >> item.price;
- }
- void showItem(const InvItem &item)
- {
- cout << fixed << showpoint << setprecision(2) << endl;
- cout << "Part Number : " << item.partNum << endl;
- cout << "Description : " << item.description << endl;
- cout << "Units On Hand : " << item.onHand << endl;
- cout << "Price : $" << item.price << endl;
- }
Enter the part number: 800
Enter the part description: Screwdriver
Enter the quantity on hand: 135
Enter the unit price: 1.25
Part Number : 800
Description : Screwdriver
Units On Hand: 135
Price : $1.25
- InvItem getItemData()
- {
- InvItem item;
- cout << "Enter the part number:";
- cin >> item.partNum;
- cout << "Enter the part description: ";
- cin.get();
- getline(cin, item.description);
- cout << "Enter the quantity on hand: ";
- cin >> item.onHand;
- cout << "Enter the unit price: ";
- cin >> item.price;
- return item;
- }
part = getItemData();
注意,C++ 只允許從函數(shù)返回單個值。然而,結構體提供了解決這一限制的方法。即使一個結構體可能有幾個成員,它在技術上還是一個單一的對象。通過在結構體中打包多個值,可以從函數(shù)返回任意數(shù)量的值。