Compound data type: Difference between revisions
Content added Content deleted
(Removed TODO tags. Simplified Perl example; it was '''way''' more complicated than necessary. This isn't an obfuscation contest.) |
(→[[C plus plus|C++]]: Additional possibilities) |
||
Line 63: | Line 63: | ||
int y; |
int y; |
||
}; |
}; |
||
It is also possible to add a constructor (this allows the use of <tt>Point(x, y)</tt> in expressions): |
|||
struct Point |
|||
{ |
|||
int x; |
|||
int y; |
|||
Point(int ax, int ay): x(ax), y(ax) {} |
|||
}; |
|||
Point can also be parametrized on the coordinate type: |
|||
template<typename Coordinate> struct point |
|||
{ |
|||
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 }; |
|||
Of course, a constructor can be added in this case as well. |
|||
==[[C sharp|C#]]== |
==[[C sharp|C#]]== |