Step 1: Understanding the figure.
Let \( AB \) be the building and \( CD \) be the tower, where \( CD = 40 \, \text{m} \).
Let the horizontal distance between the building and the tower be \( x \, \text{m} \).
Let the height of the building be \( h \, \text{m} \).
Step 2: From the top of the building, angle of elevation to top of tower is \( 60^\circ \).
Hence, in triangle \( EBC \):
\[
\tan 60^\circ = \frac{CD - h}{x}
\]
\[
\sqrt{3} = \frac{40 - h}{x}
\Rightarrow x = \frac{40 - h}{\sqrt{3}} \quad \text{(i)}
\]
Step 3: From the top of the building, angle of depression to foot of tower is \( 45^\circ \).
In triangle \( EBD \):
\[
\tan 45^\circ = \frac{h}{x}
\]
\[
1 = \frac{h}{x} \Rightarrow x = h \quad \text{(ii)}
\]
Step 4: Equate (i) and (ii).
\[
h = \frac{40 - h}{\sqrt{3}}
\]
\[
h\sqrt{3} = 40 - h
\]
\[
h(\sqrt{3} + 1) = 40
\]
\[
h = \frac{40}{\sqrt{3} + 1}
\]
Rationalize the denominator:
\[
h = 40 \times \frac{\sqrt{3} - 1}{(\sqrt{3} + 1)(\sqrt{3} - 1)} = 40 \times \frac{\sqrt{3} - 1}{2}
\]
\[
h = 20(\sqrt{3} - 1)
\]
Step 5: Final Answer.
\[
\boxed{\text{Height of the building } = 20(\sqrt{3} - 1) \, \text{m}}
\]