Misplaced Pages

Comparison of ALGOL 68 and C++: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content addedVisualWikitext
Revision as of 03:16, 6 June 2013 editAnomieBOT (talk | contribs)Bots6,590,028 editsm Dating maintenance tags: {{Inline citations}}← Previous edit Latest revision as of 22:05, 8 January 2024 edit undoZundark (talk | contribs)Extended confirmed users, File movers, Pending changes reviewers29,661 edits Code Examples: This is not C (and the article isn't about C anyway). 
(29 intermediate revisions by 22 users not shown)
Line 1: Line 1:
{{inline citations|date=June 2013}} {{no footnotes|date=June 2013}}
{{ProgLangCompare}} {{ProgLangCompare}}
The nearest living sibling to ''']''' may be ''']''', making this a good comparison candidate:


C++ doesn't have: C++ doesn't have:
* ] - first class ]s (emulation due to local definitions of class-types, which then could be ], also new ] has ]), * ] first class ]s (emulation due to local definitions of class types, which could be ]; also, ] has ]),
* ] - definable operator symbols and priorities, * ] definable operator symbols and priorities,
* garbage collection (could be emulated with help of ]), * garbage collection (could be emulated with help of ]),
* ''use before define'', * ''use before define'',
* ] using complex formatting declarations, * ] using complex formatting declarations,
* ] - assignment operation symbol (to avoid confusion with equal sign), * ] assignment operation symbol (to avoid confusion with equal sign),
* ] (and slice operations on them, but in layered libraries), * ] (and slice operations on them, but in layered libraries),
* automatic ]s, * automatic ]s,
* ], * ],
* nonlocal ] * nonlocal ]
* intuitive declaration syntax due to its origin from ]. * intuitive declaration syntax due to its origin from ].


Line 23: Line 22:
* textual ] (e.g. macros), <!-- std ALGOL 68 did have "library prelude option" --> * textual ] (e.g. macros), <!-- std ALGOL 68 did have "library prelude option" -->
* distinct reference and pointer types, * distinct reference and pointer types,
* ] lines (only bracketed comments), * ] lines (only bracketed comments),
* ]. * ].
* destructors, exceptions, templates, namespaces, structured loop exits * destructors, exceptions, templates, namespaces, structured loop exits


==Comparison of the assignment and equality operators== ==Comparison of the assignment and equality operators==
{|class="wikitable"
{|border="1" style="border-collapse: collapse;"
!bgcolor=#cccccc|Intent ||bgcolor=#cccccc|ALGOL 68 ||bgcolor=#cccccc|C++ !Intent !! ALGOL 68 !! C++
|- |-
|Assign a value 888 to a variable ''x'' || x:=888; || x = 888; |Define a constant || <code>'''int''' x=888;</code> || {{cpp|1=const int x = 888;}}
|- |-
|Compare two values || '''if''' x = 888 '''then''' ... '''fi''' || if (x == 888) { ... } |Initialise a variable || <code>'''int''' x:=888;</code> || {{cpp|1=int x = 888;}}
|- |-
|Define a constant || '''int''' x=888; || const int x = 888; |Assign a value 888 to a variable ''x'' || <code>x:=888;</code> || {{cpp|1=x = 888;}}
|- |-
|Initialise a variable || '''int''' x:=888; || int x = 888; |Compare two values || <code>'''if''' x = 888 '''then''' ... '''fi'''</code> || {{cpp|1=if (x == 888) { ... } }}
|- |-
|Allocate a variable from the '''heap''' || '''ref''' '''int''' x = '''heap''' '''int''';<br>or simply:<br>'''heap''' '''int''' x; || int* x = new int; |Allocate a variable from the '''heap''' || <code>'''ref''' '''int''' x = '''heap''' '''int''';</code><br>or simply:<br><code>'''heap''' '''int''' x;</code> || {{cpp|1=int* x = new int;}}
|- |-
|Compare address of two pointers || '''ref''' '''int''' x, y; <br>'''if''' x :=: y '''then''' ... '''fi''' || int* x; int* y; <br>if (x == y) { ... } |Compare address of two pointers || <code>'''ref''' '''int''' x, y;</code><br><code>'''if''' x :=: y '''then''' ... '''fi'''</code> || {{cpp|1=int* x; int* y;}}<br>
{{cpp|1=if (x == y) { ... } }}
|- |-
|Compare value referenced by two pointers || '''ref''' '''int''' x, y; <br>'''if''' x = y '''then''' ... '''fi''' || int* x; int* y;<br>if (*x == *y) { ... } |Compare value referenced by two pointers || <code>'''ref''' '''int''' x, y;</code><br><code>'''if''' x = y '''then''' ... '''fi'''</code> || {{cpp|1=int* x; int* y;}}<br>
{{cpp|1=if (*x == *y) { ... } }}
|- |-
|Name a new type || '''mode''' '''longreal''' = '''long''' '''real'''; || typedef double longreal; |Name a new type || <code>'''mode''' '''longreal''' = '''long''' '''real''';</code> || {{cpp|1=typedef double longreal;}}<br>or (as of C++11):<br>{{cpp|1=using longreal = double;}}
|- |-
|Name a new record type || '''mode''' '''cust''' = '''struct'''('''string''' name, address); || struct cust { std::string name, address; }; |Name a new record type || <code>'''mode''' '''cust''' = '''struct'''('''string''' name, address);</code> || {{cpp|1=struct cust { std::string name, address; }; }}
|- |-
|Name a new union type || '''mode''' '''taggedu''' = '''union'''('''string''' s, '''real''' r); || union u { std::string s; float f; }; |Name a new union type || <code>'''mode''' '''taggedu''' = '''union'''('''string''' s, '''real''' r);</code> || {{cpp|1=union u { std::string s; float f; }; }}
|- |-
|Name a procedure or function || '''proc''' f = ('''real''' x) '''real''': ( code; result ); || float f(float x) { code; return result; } |Name a procedure or function || <code>'''proc''' f = ('''real''' x) '''real''': ( code; result );</code> || {{cpp|1=float f(float x) { code; return result; } }}
|- |-
|Procedure default parameters|| '''proc''' p = ('''union''' ('''real''', '''void''') in x)'''void''': |Procedure default parameters|| <code>'''proc''' p = ('''union''' ('''real''', '''void''') in x)'''void''': </code>
&nbsp;&nbsp;&nbsp;&nbsp;( '''real''' x = (in x|('''real''' x):x|888); code ); &nbsp;&nbsp;&nbsp;&nbsp;<code>( '''real''' x = (in x|('''real''' x):x|888); code );</code>
| void p(float x=888) { code; } | {{cpp|1=void p(float x=888) { code; } }}
|- |-
|Name a new operator || '''op''' ↑ = ('''real''' x,y) '''real''': x**y; || ''n/a'' |Name a new operator || <code>'''op''' ↑ = ('''real''' x,y) '''real''': x**y;</code> || {{n/a}}
|- |-
|Set priority on a new operator || '''prio''' ↑ = 9; || ''n/a'' |Set priority on a new operator || <code>'''prio''' ↑ = 9;</code> || {{n/a}}
|- |-
|Duplication by assignment || a:=b:=c:=d; || a = b = c = d; |Chain variables assignment || <code>a:=b:=c:=d;</code> || {{cpp|1=a = b = c = d;}}
|- |-
|Displacement operator - ] only || a:=:=b:=:=c:=:=d; || a = b; b = c; c = d; |Displacement operator - ] only || <code>a:=:=b:=:=c:=:=d;</code> || {{cpp|1=a = b; b = c; c = d;}}
|- |-
| Append "substr" to a variable ''str'' || str +:= "substr"; || str += "substr"; | Append "substr" to a variable ''str'' || <code>str +:= "substr";</code> || {{cpp|1=str += "substr";}}
|- |-
| Prefix "substr" to a variable ''str'' || "substr" +=: str; || str = "substr" + str; | Prefix "substr" to a variable ''str'' || <code>"substr" +=: str;</code> || {{cpp|1=str = "substr" + str;}}
|- |-
|} |}
Line 74: Line 75:


===Union declaration and use=== ===Union declaration and use===
Assigning values into an A68 '''union''' variable is automatic, Assigning values into an A68 <code>'''union'''</code> variable is automatic,
the type is "tagged" to the variable, but pulling the value back out is the type is "tagged" to the variable, but pulling the value back out is
syntactically awkward as a ''conformity-clause'' is required. syntactically awkward as a ''conformity-clause'' is required.


'''ALGOL 68 example:''' '''ALGOL 68 example:'''
<code>
'''union'''('''int''', '''char''') x:=666; '''union'''('''int''', '''char''') x:=666;
printf(($3d l$, (x|('''int''' i):i) ))</code> printf(($3d l$, (x|('''int''' i):i) ))
'''C/C++ example:''' '''C++ example:'''
<source lang="cpp"> <syntaxhighlight lang="cpp">
union { int i; char c; } x = { 666 }; union { int i; char c; } x = { 666 };
std::cout << x.i << std::endl; std::cout << x.i << std::endl;
</syntaxhighlight>
</source>
The net effect of "type-tagging" is that Algol68's strong typing The net effect of "type-tagging" is that Algol68's strong typing
"half" encroaches into the '''union'''. "half" encroaches into the <code>'''union'''</code>.


===Mode declaration=== ===Mode declaration===
A new mode (type) may be declared using a <u>mode</u> declaration: A new mode (type) may be declared using a <code>mode</code> declaration:


<code>'''int''' max=99; '''int''' max=99;
'''mode''' '''newtype''' = '''struct''' ( '''mode''' '''newtype''' = '''struct''' (
'''long''' '''real''' a, b, c, '''short''' '''int''' i, j, k, '''ref''' '''real''' r '''long''' '''real''' a, b, c, '''short''' '''int''' i, j, k, '''ref''' '''real''' r
);</code> );


This has the similar effect as the following C++ code: This has the similar effect as the following C++ code:
<source lang="cpp"> <syntaxhighlight lang="cpp">
const int max=99; const int max=99;
typedef struct { typedef struct {
double a, b, c; short i, j, k; float& r; double a, b, c; short i, j, k; float& r;
} newtype; } newtype;
</syntaxhighlight>
</source>
Note that for ALGOL 68 only the newtype name appears to the left of the equality, and most notably the construction is made - and can be read - from left to right without regard to priorities. Note that for ALGOL 68 only the newtype name appears to the left of the equality, and most notably the construction is made - and can be read - from left to right without regard to priorities.


== External references == == External links ==
* - ] - June 1977. * - ] - June 1977.
* - Apr 2004 - retrieved May 10, 2007 * - Apr 2004 - retrieved May 10, 2007
* - Apr 2004 - retrieved May 10, 2007 * - Apr 2004 - retrieved May 10, 2007
* - Apr 2004 - retrieved May 10, 2007 * - Apr 2004 - retrieved May 10, 2007
* - Apr 2004 - retrieved May 10, 2007 * - Apr 2004 - retrieved May 10, 2007
* - Michael Walker - February 21, 2000 - retrieved December 21 2015


{{C++ programming language}}
{{DEFAULTSORT:Comparison Of Algol 68 And C}}

]
{{DEFAULTSORT:ALGOL 68 and C comparison}}
]
]
]
] ]
] ]

Latest revision as of 22:05, 8 January 2024

This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. Please help improve this article by introducing more precise citations. (June 2013) (Learn how and when to remove this message)
Comparison of
programming languages

Comparison of individual
languages

C++ doesn't have:

ALGOL 68 doesn't have:

Comparison of the assignment and equality operators

Intent ALGOL 68 C++
Define a constant int x=888; const int x = 888;
Initialise a variable int x:=888; int x = 888;
Assign a value 888 to a variable x x:=888; x = 888;
Compare two values if x = 888 then ... fi if (x == 888) { ... }
Allocate a variable from the heap ref int x = heap int;
or simply:
heap int x;
int* x = new int;
Compare address of two pointers ref int x, y;
if x :=: y then ... fi
int* x; int* y;

if (x == y) { ... }

Compare value referenced by two pointers ref int x, y;
if x = y then ... fi
int* x; int* y;

if (*x == *y) { ... }

Name a new type mode longreal = long real; typedef double longreal;
or (as of C++11):
using longreal = double;
Name a new record type mode cust = struct(string name, address); struct cust { std::string name, address; };
Name a new union type mode taggedu = union(string s, real r); union u { std::string s; float f; };
Name a procedure or function proc f = (real x) real: ( code; result ); float f(float x) { code; return result; }
Procedure default parameters proc p = (union (real, void) in x)void:

    ( real x = (in x|(real x):x|888); code );

void p(float x=888) { code; }
Name a new operator op ↑ = (real x,y) real: x**y;
Set priority on a new operator prio ↑ = 9;
Chain variables assignment a:=b:=c:=d; a = b = c = d;
Displacement operator - ALGOL 68C only a:=:=b:=:=c:=:=d; a = b; b = c; c = d;
Append "substr" to a variable str str +:= "substr"; str += "substr";
Prefix "substr" to a variable str "substr" +=: str; str = "substr" + str;

Code Examples

Union declaration and use

Assigning values into an A68 union variable is automatic, the type is "tagged" to the variable, but pulling the value back out is syntactically awkward as a conformity-clause is required.

ALGOL 68 example:

 union(int, char) x:=666;
 printf(($3d l$, (x|(int i):i) ))

C++ example:

  union { int i; char c; } x = { 666 };
  std::cout << x.i << std::endl;

The net effect of "type-tagging" is that Algol68's strong typing "half" encroaches into the union.

Mode declaration

A new mode (type) may be declared using a mode declaration:

int max=99;
mode newtype = struct (
   long real a, b, c, short int i, j, k, ref real r
);

This has the similar effect as the following C++ code:

const int max=99;
typedef struct { 
    double a, b, c; short i, j, k; float& r;
} newtype;

Note that for ALGOL 68 only the newtype name appears to the left of the equality, and most notably the construction is made - and can be read - from left to right without regard to priorities.

External links

C++
Features
Standard Library
Ideas
Compilers
IDEs
Superset languages
Dialects
Relative to
other languages
Designer
Category
Categories:
Comparison of ALGOL 68 and C++: Difference between revisions Add topic