Jump to content

Compound data type: Difference between revisions

m
→‎{{header|C++}}: pretty-printing of code
(+ Pascal)
m (→‎{{header|C++}}: pretty-printing of code)
Line 56:
 
=={{header|C++}}==
<cpp>
 
struct Point
{
int x;
int y;
};
</cpp>
 
It is also possible to add a constructor (this allows the use of <tt>Point(x, y)</tt> in expressions):
<cpp>
 
struct Point
{
int x;
int y;
Point(int ax, int ay): x(ax), y(ax) {}
};
</cpp>
 
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.
 
973

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.