Underlay device-to-device (D2D) communication is an attractive technology enabling nearby cellular users to communicate with each other directly in order to increase data rate and spectral efficiency. The current cellular heterogeneous networks consist of macrocell base stations and small cell base stations with different transmit powers and coverage areas. Femtocell is the most popular small cell which is expected to be utilized in dense and ultra-dense scenarios in the future. Network coding in relay-assisted multi-hop communications improves achievable transmission rate and coverage of D2D communications. In this paper, two-hop random linear network coding network in cooperative D2D communication (RLNC-CDC) is considered. We propose to use femtocell base station (FBS) as a relay. We assume that the D2D pair and relay operate in the frequency band which is allocated to femtocell network. Therefore, there would be interference from the relay node and the D2D communication on the femtocell network users. To reduce the interference, the sum of transmit powers of the D2D pair and selected relay FBS should be minimized in a way that the highest transmission rate for the D2D pair is achieved. The constraints on the bounds of transmit powers of the D2D and relay node as well as the minimum required transmission rate for D2D communication are considered and the optimum solution is obtained. Simulation results indicate that the proposed RLNC-CDC achieves higher data rate and smaller outage probability than the direct D2D transmission.