Let the height of the building be \( h_1 \) and the height of the tower be \( h_2 = 40 \, \text{m} \). Let the distance between the building and the tower be \( d \).
Triangle 1: Angle of elevation
In the first triangle, the angle of elevation from the top of the building to the top of the tower is \( 60^\circ \). Using the tangent function, we can write:
\[
\tan(60^\circ) = \frac{h_2 - h_1}{d} = \frac{40 - h_1}{d}.
\]
Since \( \tan(60^\circ) = \sqrt{3} \), we have:
\[
\sqrt{3} = \frac{40 - h_1}{d} \quad \Rightarrow \quad d = \frac{40 - h_1}{\sqrt{3}}.
\]
Triangle 2: Angle of depression
In the second triangle, the angle of depression to the foot of the tower is \( 45^\circ \). Using the tangent function, we can write:
\[
\tan(45^\circ) = \frac{h_1}{d} = \frac{h_1}{d}.
\]
Since \( \tan(45^\circ) = 1 \), we have:
\[
1 = \frac{h_1}{d} \quad \Rightarrow \quad d = h_1.
\]
Solving for \( h_1 \)
Substitute \( d = h_1 \) into the equation \( d = \frac{40 - h_1}{\sqrt{3}} \):
\[
h_1 = \frac{40 - h_1}{\sqrt{3}}.
\]
Now, solve for \( h_1 \):
\[
h_1 \sqrt{3} = 40 - h_1,
\]
\[
h_1 \sqrt{3} + h_1 = 40,
\]
\[
h_1(\sqrt{3} + 1) = 40,
\]
\[
h_1 = \frac{40}{\sqrt{3} + 1}.
\]
To simplify, multiply both the numerator and denominator by \( \sqrt{3} - 1 \):
\[
h_1 = \frac{40(\sqrt{3} - 1)}{(\sqrt{3} + 1)(\sqrt{3} - 1)} = \frac{40(\sqrt{3} - 1)}{3 - 1} = \frac{40(\sqrt{3} - 1)}{2}.
\]
Thus:
\[
h_1 = 20(\sqrt{3} - 1) \, \text{metres}.
\]
Conclusion:
The height of the building is \( 20(\sqrt{3} - 1) \, \text{metres}. \)