0% found this document useful (0 votes)
8 views

Chapter 4 - Data Types

Uploaded by

haftommikael12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter 4 - Data Types

Uploaded by

haftommikael12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

 A data type defines a collection of data values and a set of

predefined operations on those values .


 It is crucial that a language supports appropriate collection of data
types and structures.
 Data types can be
 Primitive data types are those not defined in
terms of other data types:
 User-defined data types: are created with flexible
structure defining operators
 Abstract data types (One step further from user-defined
types): separate the interface of a type (visible) from the
representation of that type (hidden)
 The user-defined and abstract data types Provide improved
readability through the use of meaningful names for types.
1
Introduction
y Data types are a fundamental concept in programming that
define the kind of data that variables or objects can hold.
y They play a crucial role in determining how data is stored,
processed, and manipulated within a program.
y 1. Properties of Types and Objects:
◦ Identity: Each object or variable has a unique identity
that distinguishes it from other objects or variables.
◦ Value : Objects or variables have a value associated with
their data type.
◦ Lifetime : The lifetime of an object or variable is the
duration during which it exists in memory.
◦ Scope: The scope of an object or variable defines where
it can be accessed and used within the program.

2
Elementary Data Types:
1. PRIMITIVE DATA TYPES: Data types which are not defined in
terms of other types.
◦ Boolean: Represents true or false values. It's often used for conditions
and logical operations.
◦ Integer : Represents whole numbers, positive or negative, without
fractional parts. Different programming languages provide various sizes
of integer types (e.g., int, long, short).
◦ Floating -Point : Represents real numbers with fractional parts. It
includes single-precision (float) and double-precision (double) types.
◦ Character : Represents individual characters, often using ASCII or
Unicode encoding. Examples include char in C/C++ and char in Java.
◦ String : Represents sequences of characters. Strings can vary in length
and are often used for text manipulation.
◦ Enumeration (Enum ): Defines a set of named integer constants.
Enums are used to represent a set of distinct values.

3
y Data types which are not defined in terms of other
types.
1. Numeric types
 Integers
x byte, short, int and long
x signed and unsigned
Integer representation
x Sign-magnitude
x A negative integer could be stored in sign-
magnitude notation, in which the sign bit is set
to indicate a negative and the remainder of the
bit string represents the absolute value of the
number.
5
y Ones-complement
◦ In ones-complement notation, the negative of an
integer is stored as the logical complement of its
absolute value

y Twos-complement
◦ In twos-compIement notation, the representation of a
negative integer is formed by taking the logical
complement of the positive version of the number and
adding one.

6
y Example: Sign-magnitude
57 could be represented by 00111001
-57 could be represented by 10111001
y Example: Ones-complement
57 is represented by 00111001
-57 is represented by 11000110
y Example: Twos-complement
57 is represented by 00111001
-57 is represented by 11000110
+ 1

11000111 7
y Floating-points
y Computer implementation of real numbers
y Floating point numbers are commonly represented as a
sign, a significand, or fractional part, and an exponent.
y The IEEE has defined formats for 32 bit and 64 bit
floating point implementations
y Most computers and computer languages support
these IEEE 754 implementations.

8
PRIMITIVE DATA TYPES
y Decimal
◦ Decimal types are stored using binary codes for the
decimal digits.
◦ These representations are called binary coded decimal
(BCD)
◦ In some cases, they are stored one digit per byte, but
in others they are packed two digits per byte
◦ Either way, they take more storage than binary
representations.

9
PRIMITIVE DATA TYPES
y Character types
◦ Character data are stored in computers as numeric codings
◦ Traditionally, the most commonly used coding was the 8-bit
code ASCII (American Standard Code for Information
Interchange), which uses the values 0 to 127 to code 128
different characters.
◦ Because of the globalization of business and the need for
computers to communicate with other computers around
the world, The ASCII character set is rapidly becoming
inadequate.
◦ A I6-bit character set named Unicode has been developed
as an alternative
x Example: char *str = “computer”;
x char country[50]; country=“Gini”;
country=“Ethiopia”;
11
ARRAY TYPES
y An array is a homogeneous aggregate of data elements in
which an individual element is identified by its position in the
aggregate, relative to the first element.
y References to individual array elements are specified using
subscript expressions
y Some design issues
◦ What type is legal for subscript or index?
◦ Are subscripting expressions in element references range
checked?
◦ When are array allocations take place?
◦ Are ragged or rectangular multidimensioned arrays
allowed, or both?
◦ Can arrays be initialized when they have their storage
allocated?
12
ARRAY TYPES
y Arrays and Indices
◦ Specific elements of an array are referenced by means of a two-
level syntactic mechanism: aggregate name and index.
◦ Syntax: The array name is followed by the list of subscripts,
which is surrounded by either parentheses or brackets.
◦ In some PLs, array indices were enclosed in parentheses 
problem of identifying array element accessing and subprogram
calling.
◦ Consider the following Ada assignment statement

13
RECORD TYPES
y A record is an aggregate of data elements in which the
individual elements are identified by names and accessed
through offsets from the beginning of the structure.
y Used to model a collection of data in which the individual
elements are not of the same type or size
y Example: Information about a college student might include
name, student number, grade point average, and so forth
x A data type for such a collection might use a character
string for the name, an integer for the student number, a
floating point for the grade point average, and so forth
y In C, C++, and C#, records are supported with the struct
data type

14
EXERCISES

y Define a primitive data type


y What is the difference between single-precision
and double-precision floating points
y Discuss how floating points are represented in
computers
y State the reason that a boolean value is
represented using a byte instead of a bit
y Show clearly that BCD takes more space than
binary representation

17
18
Chapter Four
Fundamental Object
oriented programming
concepts

19
Fundamental oop concepts
y Object; a thing from real word, a thing that you want to store
and process data about , an entity which has properties and task
to be performed eg book, person , #person(name, sex, age,
salary, profession)
y Class: template to create object, a code written by programmer,
consists a number of objects.
1. Abstraction in programming refers to the process of simplifying
complex systems by breaking them down into smaller, more
manageable components while hiding unnecessary details.
◦ Abstraction enables programmers to focus on essential
aspects of a system or data structure, making code more
understandable and maintainable.
◦ abstraction is simplification of facts
◦ : Consider a personnel file of an employer. This set may
include Name, Age, Salary but it will most probably not include
irrelevant data such as the hair color, weight, and height.
24

You might also like