Compound data type: Difference between revisions

Content added Content deleted
(+ Pascal)
m (→‎{{header|C++}}: pretty-printing of code)
Line 56: Line 56:


=={{header|C++}}==
=={{header|C++}}==
<cpp>

struct Point
struct Point
{
{
int x;
int x;
int y;
int y;
};
};
</cpp>


It is also possible to add a constructor (this allows the use of <tt>Point(x, y)</tt> in expressions):
It is also possible to add a constructor (this allows the use of <tt>Point(x, y)</tt> in expressions):
<cpp>

struct Point
struct Point
{
{
int x;
int x;
int y;
int y;
Point(int ax, int ay): x(ax), y(ax) {}
Point(int ax, int ay): x(ax), y(ax) {}
};
};
</cpp>


Point can also be parametrized on the coordinate type:
Point can also be parametrized on the coordinate type:
<cpp>
template<typename Coordinate> struct point
{
Coordinate x, y;
};


// A point with integer coordinates
template<typename Coordinate> struct point
Point<int> point1 = { 3, 5 };
{
Coordinate x, y;
};
// A point with integer coordinates
Point<int> point1 = { 3, 5 };
// a point with floating point coordinates
Point<float> point2 = { 1.7, 3.6 };


// a point with floating point coordinates
Point<float> point2 = { 1.7, 3.6 };
</cpp>
Of course, a constructor can be added in this case as well.
Of course, a constructor can be added in this case as well.