Introduction to abstract data types (ADT) (i)

Source: Internet
Author: User

Introduction to abstract data types (ADT) (i)
1. Abstract data type (Types,adt) and ADT implementation

Abstract data types: A collection of data elements and operations on those data.

An implementation of ADT includes storage structures for storing data elements and algorithms for implementing basic operations.

In this abstract idea of data, the definition of data type is separate from its implementation, which is an important concept in software design. This makes it possible to only study and use its structure without regard to its implementation details. In fact, this typically uses methods on predefined data types such as int, double, char, and bool, and programmers who use these data types do not need to worry about how these data types are implemented for most of the time.


2. Simple data types for C + + and how are they implemented?

The basic data types in C + +, such as int/char/double/float, are referred to as simple data types because the values of these data types are atomic, that is, it is made up of a separate entity that cannot be separated. But they can also be thought of as abstract data types, because these data types describe a series of values and provide implementations of operations on those values, each of which uses memory cells as the storage structure, and their basic operations are implemented by the hardware or software of the computer system.


(1), Integral type data:

unsigned integers : nonnegative integers, sometimes referred to as cardinality or full, whose collection is {0,1,2,3, ...}. In C + +, this ADT is modeled by 3 data types: unsigned short int (unsigned short), unsigned int (unsigned), unsigned long int (unsigned long), These types of values are stored in memory as a bit string. Their lengths must meet:

sizeof (unsigned short) ≦sizeof (unsigned) ≦sizeof (unsigned long)

Note: sizeof is a C + + operator that returns the number of bytes that a type occupies. Typically, the value of the unsigned short type is stored in 2 bytes, the value of the unsigned type is stored in 4 bytes, and the value of unsigned long is stored in 4 or 8 bytes.

Sample code is shown below:

#include <iostream>using namespace Std;int main () {cout<< "sizeof (unsigned short) =" <<sizeof ( unsigned short) <<endl;cout<< "sizeof (unsigned) =" <<sizeof (unsigned) <<endl;cout<< " sizeof (unsigned long) = "<<sizeof (unsigned long) <<endl;return 0;}
The results are as follows:



signed integer : integer set {...., -3,-2,-1,0,1,2,3, ...} And some of the familiar arithmetic operations also form an ADT. In C + +, this ADT is modeled by 3 data types: Short int (short), int, long int (long). Typically, a value of type short is stored in 2 bytes, the value of type int is stored in 4 bytes, and a long value is stored in 4 or 8 bytes.

Note: Positive and negative numbers of the original code is its own, just in the first place with a sign, a positive number of the anti-code is not changed, negative number of the anti-code is in addition to the symbol bit all the reverse, positive complement is unchanged, negative complement is the sign bit unchanged, all counter plus 1 can. The difference between them is as follows:





(2), real data:

Real values, also known as floating-point values, are modeled in C + + by a single-precision type of float, double type doubles, and extended precision type long double.


Where double is the default data type that handles real data, you can add a suffix f after a real number constant to treat the real constant as a float value, and if you add an l suffix, the real constant is treated as a long double value.


Note: Before a binary representation of a real number is stored in memory, it is usually converted to a floating-point form.


Note: There is a "rounding error" in the storage of real values, which can be reduced by storing a larger number of bits, but cannot be completely eliminated. Please understand the difference between the theoretical values and the value that the computer can store!!! This is also an example of an ADT that cannot be completely faithful to this ADT representation (mathematically) of real numbers and operations on real numbers.


(3), character data:

There are two main character sets used in programming languages. ASCII (America Standards Code for information Interchange, US Information Interchange Standard code) is the most common, but some languages use Unicode, such as Java. In C + +, characters are usually handled using a char type, which uses a numeric code that can be stored in one byte to represent each character. Unicode uses a 2-byte encoding.


C + + provides wide character type wchar_t to store characters in large character sets such as Unicode. In C + +, character values can be treated as integer values:

Integer code for INT (char_value) =char_value

In fact, a value of type char can be combined with a numeric value into an arithmetic expression, where the numeric encoding of the char type value is used.


(4), Boolean data:

In C + +, Boolean and Boolean operations are modeled by the BOOL data type. There are two Boolean constants: False (0) and True (1).


The conversions between these numeric values and the Boolean values are automatically performed when the bool type values are entered and output.


3, the implementation of ADT is not faithful to the example of an ADT presentation

For ADT of all finite multiple data elements, the limits on the set of data values are fixed, because the memory used to store the elements is limited.

The implementation of ADT is not faithful to the example of an ADT representation: overflow.

Two examples are given below as proof of this.

--Program to demonstrate the effects of overflow #include <iostream>  using namespace std; int main () {in    T number = 2;    for (int i = 1; i <=; i++)    {       cout << number << Endl;       Number *=;}  }
Results such as:


--Program to demonstrate the effects of Overflow#include <iostream> #include <climits>using namespace Std;i NT Main () {   int number = int_max-3;   for (int i = 1; I <= 7; i++)   {      cout << number << Endl;      number++;}   }

Results such as:



Another reason that the implementation of DT is not faithful to ADT is that the operation in the implementation does not necessarily perform exactly the same way as the corresponding ADT operation.



。。。。

The introduction to the 4, new data types (typedef and enumerations) that can be defined by programmers, and 5, pointers, will be described in more detail in the next blog entry, "Introduction to Abstract data types (ADT)".

Introduction to abstract data types (ADT) (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.