Both are close relatives, belonging to the Curcubitaceae family, but the melon is considered a fruit and the cucumber, a vegetable. Botanists would say that a fruit refers to the part of the plant that contains seeds. Fruits can be juicy or hard and dry and they are used by plants as a way of scattering their seeds far away. By this definition tomatoes and peppers are fruits although we consider them vegetables. The distinction appears to be based more on culinary use than botanical definition.
Fruit often appear in desserts whereas vegetables generally are used in savoury dishes.
The term vegetable refers loosely to a large and varied number of plants and parts of plants that are suitable for human consumption and contribute vitamins, minerals, starch and vegetable protein to our diet. A vegetable is a utility term based purely on the usage of the particular plant or plant part. Vegetables may technically be derived from whole plants, or only the leaves, fruit, flowers, seeds, etc., or combinations thereof. In a nutshell, strictly speaking, the two terms cannot be compared as they belong to different classifications of word usage. Botanically, a fruit must come from the fertilization of a flower.