modules/mod1/mod1.cppm

The following code example is taken from the book
C++20 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2021
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

module;              // start module unit with global module fragment

#include <string>
#include <vector>

export module Mod1;  // module declaration

struct Order {
  int count;
  std::string name;
  double price;

  Order(int c, const std::string& n, double p)
   : count{c}, name{n}, price{p} {
  }
};

export class Customer {
private:
  std::string name;
  std::vector<Order> orders;
public:
  Customer(const std::string& n)
   : name{n} {
  }
  void buy(const std::string& ordername, double price) {
    orders.push_back(Order{1, ordername, price});
  }
  void buy(int num, const std::string& ordername, double price) {
    orders.push_back(Order{num, ordername, price});
  }
  double sumPrice() const;
  double averagePrice() const;
  void print() const;
};