PPL-Unit 2 Part 5
PPL-Unit 2 Part 5
Design issues:
• What is the syntactic form of references to fields?
• Are elliptical references allowed?
record
Definitions of Records
The COBOL form of a record declaration
01 EMPLOYEE-RECORD.
02 EMPLOYEE-NAME.
05 FIRST PICTURE IS X(20).
05 MIDDLE PICTURE IS X(10).
05 LAST PICTURE IS X(20).
02 HOURLY-RATE PICTURE IS 99V99.
•The EMPLOYEE-RECORD record consists of the
EMPLOYEE-NAME record andthe HOURLY-RATE field.
•The numerals 01, 02, and 05 that begin the lines of
•the record declaration are level numbers, which
indicate by their relative values the hierarchical
structure of the record.
•Any line that is followed by a line with a higher-level
number is itself a record.
record
The PICTURE clauses show the formats of the field
storage locations, with X(20) specifying 20 alphanumeric
characters and 99V99 specifying four decimal digits with
the decimal point in the middle
In c declaration
struct Person
{
char name[50];
int citNo;
float salary;
};
Ada declaration:
type Employee_Name_Type is record
First : String (1..20);
Middle : String (1..10);
Last : String (1..20);
record
References to Record Fields:
Reference Types:
A reference type variable is similar to a pointer. A pointer
refers to an address in memory, while a reference refers to
an object or a value in memory.
C++,JAVA, C# are support reference type.
Reference type variables are specified in definitions by
preceding their
names with ampersands (&). For example,
int result = 0;
int &ref_result = result;
...
ref_result = 100;
Type Checking
•Type checking is the activity of ensuring that the
operands of an operator are of compatible types.
•A compatible type is one that either is legal for the
operator or is allowed under language rules to be implicitly
converted by compiler-generated code (or the interpreter)
to a legal type. This automatic conversion is called a
coercion.
•For example, if an int variable and a float variable are
added in Java, the value of the int variable is coerced to
float and a floating-point add is done.
•A type error is the application of an operator to an
operand of an inappropriate type.
•For example, in the original version of C, if an int value
was passed to a function that expected a float value, a
type error would occur.
•Dynamic type binding requires type checking at run time,
Strong Typing
•A programming language is strongly typed if type
errors are always
detected. This requires that the types of all operands can
be determined, either at compile time or at run time.
•Ada is nearly strongly typed
•C and C++ are not strongly typed languages because
both include union types, which are not type checked.
•ML,JAVA,C# are strongly typed
Type Equivalence
•Two types are equivalent if an operand of one type in an
expression is substituted for one of the other type, without
coercion.
•Type equivalence is a strict form of type compatibility—
compatibility without coercion
•There are two approaches to defining type equivalence:
name type equivalence and structure type equivalence.
•Name type equivalence means that two variables have
equivalent types if they are defined either in the same
declaration or in declarations that use the same type
name.
•Structure type equivalence means that two variables have
equivalent types if their types have identical structures.
•Ada declarations:
type Celsius = Float;
Fahrenheit = Float;