C語言中的結構,實際上是不同數據類型的數據項的一個指定聚合。
在C++中,結構通常用于相同的目的,但C++中的結構可以完成的工作要比C 的結構多,C++中不僅僅可以添加數據成員,還可以是函數成員,這與類有點相似了。
實際上,C++結構的功能可以用類來替代。
C++結構中的成員默認情況下可公開訪問。
練習題:
編寫一個簡單的貨幣轉換程序。為此,需要在貨幣對象中關聯兩個實體:貨幣類型和把貨幣轉換為美元的轉換因子。設計一個結構來表示貨幣對象,編寫一個程序,讓用戶從一個列表中選擇轉換的貨向類型,在任意兩種貨幣之間轉換。用戶應輸入值,并獲得轉換后的結果,如果輸入了一個負值,就退出程序。
參考答案:

currency.h
// currency.h
// A currency converter
#ifndef CURRENCY_H
#define CURRENCY_H
// Structure representing a currency
struct Currency {
char name[20]; // Currency name
double amount; // An amount in this currency
double rate; // Value of 1 unit of this currency in US dollars
char code[4]; // Currency code - 3 characters e.g. "USD"
double convert(Currency amount); // Convert amount to this currency
void set_amount(double amount); // Set an amount in this currency
void show(); // Display the amount of this currence
};
#endif

currency.cpp
// currency.cpp
// currency converter
#include <iostream>
#include <iomanip>
#include "currency.h"
// Display a currency amount with two decimal places
void Currency::show() {
std::cout << std::fixed << std::setprecision(2) << amount << " " << code;
}
// Sets the amount for the current currency
void Currency::set_amount(double value) {
if (value == 1.0) // Can't have a zero amount
amount = 1.0;
else
amount = value;
}
// Convert the amount of the current currency the currency of the argument
double Currency::convert(Currency currency) {
return amount*rate/currency.rate;
}

main.cpp
// A currency converter
#include "currency.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
void main() {
// Array of standard currencies
Currency currencies[] = {
{ "US dollars", 1.0, 1.0, "USD" },
{ "UK pounds" , 1.0, 1.69, "GBP" },
{ "Russian roubles", 1.0, 0.033, "RUR" },
{ "Japanese yen", 1.0, 0.0084, "JPY" },
{ "Polish zlotys", 1.0, 0.26, "PLN" }
};
int currency_count = sizeof currencies/sizeof currencies[0]; // Number of currencies
int from_currency = 0; // Index of currency converted from
int to_currency = 0; // Index of currency converted to
double amount = 0.0; // Amount to be converted
char answer = 0; // Y/N input response
cout << endl << "Currencies available for conversion are: " << endl;
for (int i=0; i<currency_count; i++)
cout << std::setw(3) << i+1 << ". " <<currencies[i].name << endl;
do {
// Get from currency number and check it's within range
while (true) {
cout << endl << "Select the currency you want to convert from by number: ";
cin >> from_currency;
if (from_currency < 1 || from_currency>currency_count)
cout << "Invalid input - try again." << endl;
else
break;
}
// Get to currency number and check it's within range
while (true) {
cout << endl << "Select the currency you want to convert to by number: ";
cin >> to_currency;
if (from_currency == to_currency || to_currency < 1 || to_currency > currency_count)
cout << "Invalid input - try again." << endl;
else
break;
}
// Decrement so we canuse as an index value to the array
--from_currency;
--to_currency;
while (true) {
cout << endl << "Enter the amount of "
<< currencies[from_currency].name << " that you want to convert to "
<< currencies[to_currency].name << ": ";
cin >> amount;
if (amount <= 0.0)
cout << "The amount cannot be zero or negative - try again." << endl;
else
break;
}
currencies[from_currency].set_amount(amount);
currencies[to_currency].set_amount(currencies[from_currency].convert(currencies[to_currency]));
currencies[from_currency].show();
cout << " is worth ";
currencies[to_currency].show();
cout << endl;
cout << endl << "Do you want another conversion (y/n)? ";
cin >> answer;
} while(tolower(answer) == 'y');
}