Let the number of boys in the class be represented as \( B \) and the number of girls be represented as \( G \). According to the problem, we are given that there are 18 girls, so \( G = 18 \).
We know that the ratio of boys to the total number of pupils (boys and girls) is given by:
\[ \frac{B}{B + G} = \frac{4}{7} \]
Substituting \( G = 18 \) into the equation, we have:
\[ \frac{B}{B + 18} = \frac{4}{7} \]
To eliminate the fraction, we can cross-multiply:
\[ 7B = 4(B + 18) \]
Expanding the right side:
\[ 7B = 4B + 72 \]
Now, we isolate \( B \):
\[ 7B - 4B = 72 \]
\[ 3B = 72 \]
Dividing both sides by 3 gives:
\[ B = 24 \]
Now, we can find the total number of pupils:
\[ Total = B + G = 24 + 18 = 42 \]
Therefore, the total number of pupils is 42, and the number of boys is 24.