A binary quadratic form, with integer coefficients, is some
$$ f(x,y) = A x^2 + B xy + C y^2. $$ The discriminant is
$$ \Delta = B^2 - 4 A C. $$
We will abbreviate this by $$ \langle A,B,C \rangle. $$ It is primitive if $\gcd(A,B,C)=1. $
Standard fact, hard to discover but easy to check: $$ (A x^2 + B x y + C D y^2 ) (C z^2 + B z w + A D w^2 ) = A C X^2 + B X Y + D Y^2,$$
where $ X = x z - D yw, \; Y = A xw + C yz + B yw. $ This gives us Dirichlet's definition of "composition" of quadratic forms of the same discriminant,
$$ \langle A,B,CD \rangle \circ \langle C,B,AD \rangle = \langle AC,B,D \rangle. $$ In particular, if this $D=1,$ the result represents $1$ and is ($SL_2 \mathbf Z$) equivalent to the "principal" form for this discriminant. Oh, duplication or squaring in the group; if $\gcd(A,B)=1,$
$$ \langle A,B,AD \rangle^2 = \langle A^2,B,D \rangle. $$
This comes up with positive forms: $ \langle A,B,C \rangle \circ \langle A,-B,C \rangle = \langle 1,B,AC \rangle $ is principal, the group identity.
Probably should display some $SL_2 \mathbf Z$ equivalence rules, these are how we calculate when things are not quite right for Dirichlet's rule:
$$ \langle A,B,C \rangle \cong \langle C,-B,A \rangle, $$
$$ \langle A,B,C \rangle \cong \langle A, B + 2 A, A + B +C \rangle, $$
$$ \langle A,B,C \rangle \cong \langle A, B - 2 A, A - B +C \rangle. $$
Imaginary first. Suppose we want to know about $\mathbf Q(\sqrt {-47}).$ Reduced positive forms $ \langle A,B,C \rangle $ obey $|B| \leq A \leq C$ and $B \neq -A,$ also whenever $A=C$ we have $B \geq 0.$ Our group of binary forms is
-47
class number 5
all
( 1, 1, 12)
( 2, -1, 6)
( 2, 1, 6)
( 3, -1, 4)
( 3, 1, 4)
This is an abelian group in any case, so it is cyclic of order 5. These are also the five elements in the ring of integers of $\mathbf Q(\sqrt {-47}).$
Here is the mapping from forms to ideals: given $ \langle A,B,C \rangle, $ drop the letter $C.$ That's it.
$$ \langle A,B,C \rangle \mapsto \left[ A, \frac{B + \sqrt \Delta}{2} \right]. $$
Oh, why is this an ideal, rather than just some $\mathbf Z$-lattice? Because, given $\alpha,\beta$ rational integers, $$ \left[ \alpha, \frac{\beta + \sqrt \Delta}{2} \right] $$ is an ideal if and only if
$$ 4 \alpha | ( \Delta - \beta^2 ). $$
Group: we already see how to do
$$ \langle 2,1,6 \rangle^2 \cong \langle 4,1,3 \rangle \cong \langle 3,-1,4 \rangle; $$
$$ \langle 2,1,6 \rangle \circ \langle 3,-1,4 \rangle \cong \langle 2,5,9 \rangle \circ \langle 3,5,6 \rangle \cong \langle 6,5,3 \rangle \cong \langle 3,-5,6 \rangle \cong \langle 3,1,4 \rangle; $$
$$ \langle 2,1,6 \rangle \circ \langle 3,1,4 \rangle \cong \langle 6,1,2 \rangle \cong \langle 2,-1,6 \rangle. $$
$$ \langle 2,1,6 \rangle \circ \langle 2,-1,6 \rangle \cong \langle 1,1,12 \rangle $$
in any case.
Best Answer
Perhaps the most illuminating way is to transport the class group structure from ideals to primitive binary quadratic forms. Below is a description of the standard maps from section 5.2, p. 225 of Henri Cohen's book $ $ A course in computational algebraic number theory. To me, this is one of the most beautiful examples of transport of structure.