In this paper, an efficient and accurate computational method based on the hat functions (HFs) is proposed for solving a class of fractional optimal control problems (FOCPs). In the proposed method, the fractional optimal control problem under consideration is reduced to a system of nonlinear algebraic equations which can be simply solved. To this end, the fractional state and control variables are expanded by the HFs with unknown coefficients. Then, the operational matrix of fractional integration of the HFs with some properties of these basis functions are employed to achieve a nonlinear algebraic equation, replacing the performance index and a nonlinear system of algebraic equations, replacing the dynamical system in terms of the unknown coefficients. Finally, the method of constrained extremum is applied, which consists of adjoining the constraint equations derived from the given dynamical system to the performance index by a set of undetermined Lagrange multipliers. As a result, the necessary conditions of optimality are derived as a system of algebraic equations in the unknown coefficients of the state variable, control variable and Lagrange multipliers. Furthermore, the efficiency of the proposed method is shown for some concrete examples. The results reveal that the proposed method is very accurate and efficient.