how to detect open and closed shapes in opencv.

These are simple sampl
While correct for the problem posed, @Haris helpful answer should not be taken as a general solution for identifying closed contours using findContours().
One reason is that a filled object will have no internal contour and so would return hierarchy[i][2] = -1, meaning this test on its own would wrongly label such contours as 'open'.
The contour of a filled object should have no child or parent in the contour hierarchy, i.e. be at top level. So to detect for closed contours of filled objects would at least require an additional test: if(hierarchy[i][2] < 0 && hierarchy[i][3] < 0).
I think @Haris answer may have made this point obliquely but I thought it worth clarifying for people, like myself, who are learning how to use opencv.