Compound data type
You are encouraged to solve this task according to the task description, using any language you may know.
Create a complex data type Point(x,y)
Ada
Tagged Type
Ada tagged types are extensible through inheritance. The reserved word tagged causes the compiler to create a tag for the type. The tag identifies the position of the type in an inheritance hierarchy.
type Point is tagged record X : Integer := 0; Y : Integer := 0; end record;
Record Type
Ada record types are not extensible through inheritance. Without the reserved word tagged the record does not belong to an inheritance hierarchy.
type Point is record X : Integer := 0; Y : Integer := 0; end record;
Parameterized Types
An Ada record type can contain a discriminant. The discriminant is used to choose between internal structural representations. Parameterized types were introduced to Ada before tagged types. Inheritance is generally a cleaner solution to multiple representations than is a parameterized type.
type Person (Gender : Gender_Type) is record Name : Name_String; Age : Natural; Weight : Float; Case Gender is when Male => Beard_Length : Float; when Female => null; end case; end record;
In this case every person will have the attributes of gender, name, age, and weight. A person with a Male gender will also have a beard length.
BASIC
Interpeter: QuickBasic 4.5, PB 7.1
TYPE Point x AS INTEGER y AS INTEGER END TYPE
C
Compiler: GCC, MSVC, BCC, Watcom
Libraries: Standard
typedef struct Point { int x; int y; } Point;
C++
Compiler: GCC, Visual C++, BCC, Watcom
struct Point { int x; int y; };
It is also possible to add a constructor (this allows the use of Point(x, y) 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#
struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }
IDL
point = {x: 6 , y: 0 } point.y = 7 print, point ;=> { 6 7}
Java
// The byte structure format does not exist natively --> TODO. public class Point { public int x, y; public Point() { this(0); } public Point(int x0) { this(x0,0); } public Point(int x0, int y0) { x = x0; y = y0; } public static void main(String args[]) { Point point = new Point(1,2); System.out.println("x = " + point.x ); System.out.println("y = " + point.y ); } }
JavaScript
var point = new Object(); point.x = 1; point.y = 2;
JSON
var point = { x:1, y:2 };
PHP
# Using pack/unpack $point = pack("ii", 1, 2);
$u = unpack("ix/iy", $point); echo $x; echo $y;
list($x,$y) = unpack("ii", $point); echo $x; echo $y;
Tcl
This appears to be a sub-functionality of a proper associative array:
array set point {x 4 y 5} set point(y) 7 puts "Point is {$point(x),$point(y)}" # => Point is {4,7}
OCaml
type json point = < x: int; y: int >