Langbahn Team – Weltmeisterschaft

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

Content deleted Content added
m Typo fix, replaced: External references → External links using AWB
Line 1: Line 1:
{{inline citations|date=June 2013}}
{{no footnotes|date=June 2013}}
{{ProgLangCompare}}
{{ProgLangCompare}}


C++ doesn't have:
C++ doesn't have:
* [[ALGOL_68#proc:_Procedures|PROC]] - first class [[nested function]]s (emulation due to local definitions of class-types, which then could be [[Function object|functors]], also new [[C++11]] has [[C++11#Lambda_functions_and_expressions|lambda functions]]),
* [[ALGOL 68#proc: Procedures|PROC]] - first class [[nested function]]s (emulation due to local definitions of class-types, which then could be [[Function object|functors]], also new [[C++11]] has [[C++11#Lambda functions and expressions|lambda functions]]),
* [[ALGOL_68#op: Operators|OP and PRIO]] - definable operator symbols and priorities,
* [[ALGOL 68#op: Operators|OP and PRIO]] - definable operator symbols and priorities,
* garbage collection (could be emulated with help of [[Smart_pointer#C.2B.2B_smart_pointers|smart pointers]]),
* garbage collection (could be emulated with help of [[Smart pointer#C.2B.2B smart pointers|smart pointers]]),
* ''use before define'',
* ''use before define'',
* [[ALGOL_68#formatted transput|formatted transput]] using complex formatting declarations,
* [[ALGOL 68#formatted transput|formatted transput]] using complex formatting declarations,
* [[ALGOL 68#Dyadic operators with associated priorities|:=]] - assignment operation symbol (to avoid confusion with equal sign),
* [[ALGOL 68#Dyadic operators with associated priorities|:=]] - assignment operation symbol (to avoid confusion with equal sign),
* [[ALGOL_68#struct.2C_union_.26_.5B:.5D:_Structures.2C_unions_and_arrays|array]] (and slice operations on them, but in layered libraries),
* [[ALGOL 68#struct.2C union .26 .5B:.5D: Structures.2C unions and arrays|array]] (and slice operations on them, but in layered libraries),
* automatic [[ALGOL_68#struct.2C_union_.26_.5B:.5D:_Structures.2C_unions_and_arrays|UNION]]s,
* automatic [[ALGOL 68#struct.2C union .26 .5B:.5D: Structures.2C unions and arrays|UNION]]s,
* [[ALGOL_68#Expressions_and_compound_statements|CASE expressions]],
* [[ALGOL 68#Expressions and compound statements|CASE expressions]],
* nonlocal [[ALGOL_68#Code sample|GOTO]]
* nonlocal [[ALGOL 68#Code sample|GOTO]]
* intuitive declaration syntax due to its origin from [[C Programming Language|C]].
* intuitive declaration syntax due to its origin from [[C Programming Language|C]].


Line 22: Line 22:
* textual [[Preprocessor|preprocessing]] (e.g. macros), <!-- std ALGOL 68 did have "library prelude option" -->
* textual [[Preprocessor|preprocessing]] (e.g. macros), <!-- std ALGOL 68 did have "library prelude option" -->
* distinct reference and pointer types,
* distinct reference and pointer types,
* [[ALGOL_68#pr_.26_co:_Pragmats_and_Comments|comment]] lines (only bracketed comments),
* [[ALGOL 68#pr .26 co: Pragmats and Comments|comment]] lines (only bracketed comments),
* [[C%2B%2B#Objects|struct inheritance, struct member functions, virtual functions]].
* [[C++#Objects|struct inheritance, struct member functions, virtual functions]].
* destructors, exceptions, templates, namespaces, structured loop exits
* destructors, exceptions, templates, namespaces, structured loop exits


Line 107: Line 107:
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 ==
* [http://comjnl.oxfordjournals.org/cgi/content/abstract/21/4/316 A comparison of PASCAL and ALGOL 68] - [[Andrew S. Tanenbaum]] - June 1977.
* [http://comjnl.oxfordjournals.org/cgi/content/abstract/21/4/316 A comparison of PASCAL and ALGOL 68] - [[Andrew S. Tanenbaum]] - June 1977.
* [http://remus.rutgers.edu/cs314/s2004/ryder/lectures/intro-1-2upNew.pdf Orthogonal language design] - Apr 2004 - retrieved May 10, 2007
* [http://remus.rutgers.edu/cs314/s2004/ryder/lectures/intro-1-2upNew.pdf Orthogonal language design] - Apr 2004 - retrieved May 10, 2007

Revision as of 15:54, 3 August 2015

C++ doesn't have:

ALGOL 68 doesn't have:

Comparison of the assignment and equality operators

Intent ALGOL 68 C++
Assign a value 888 to a variable x x:=888; x = 888;
Compare two values if x = 888 then ... fi if (x == 888) { ...}
Define a constant int x=888; const int x = 888;
Initialise a variable int x:=888; int 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;
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;
Duplication by 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/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 = [0:9][0:max]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[9+1][max+1];

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.