Code · 1 file
Sample C++ File
C++ is a high-performance language for games, browsers and trading systems. This sample uses C++20. The C++ sample is a small program — a customer record, a VIP check and a formatted total — that is the same in every language we offer, so you can compare them side by side.
C++ · 925 B
Sample C++ file
An idiomatic C++ example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.
CC0 · free for any use · Verified by an independent tool · Report a problem
// Sample C++20 program — samplefiles.com
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
struct Customer {
int id;
std::string name;
double balance;
[[nodiscard]] bool is_vip() const { return balance >= 10'000; }
};
double total_balance(const std::vector<Customer>& customers) {
return std::accumulate(customers.begin(), customers.end(), 0.0,
[](double sum, const Customer& c) { return sum + c.balance; });
}
int main() {
const std::vector<Customer> customers{{1, "Ada", 12'500}, {2, "Grace", 830.5}, {3, "Linus", 10'000}};
for (const auto& c : customers)
std::cout << std::left << std::setw(7) << c.name << std::right << std::setw(10) << std::fixed
<< std::setprecision(2) << c.balance << (c.is_vip() ? " VIP" : "") << '\n';
std::cout << "Total: " << total_balance(customers) << '\n';File details
Download from the command line
curl -L -o sample.cpp https://files.samplefiles.com/code/cpp/sample.cppwget https://files.samplefiles.com/code/cpp/sample.cpphttps://files.samplefiles.com/code/cpp/sample.cppprintf '%s %s\n' 4a7d0f45a27f026e4b2bb3f27c3c4dc511776761fd64dd383f37a55d3e82b741 sample.cpp | shasum -a 256 -cOr check the download in your browser with the checksum calculator.