Abstract Data Types A variable in a procedural programming language such as Fortran , Pascal , C , and C++ , is an abstraction.

The abstraction comprises a number of attributes --name , address , value , lifetime , scope , type , and size . Each attribute has an associated value. E.g., if we declare an integer variable in C++, int x, we say that the name attribute has value ``x'' and that the type attribute has value ``int''. Unfortunately, the terminology can be somewhat confusing: The word ``value'' has two different meanings--in one instance it denotes one of the attributes and in the other it denotes the quantity assigned to an attribute. E.g., after the assignment statement x = 5, the value attribute has the value five. The name of a variable is the textual label used to refer to that variable in the text of the source program. The address of a variable denotes is location in memory. The value attribute is the quantity which that variable represents. The lifetime of a variable is the interval of time during the execution of the program in which the variable is said to exist. The scope of a variable is the set of statements in the text of the source program in which the variable is said to be visible . The type of a variable denotes the set of values which can be assigned to the value attribute and the set of operations which can be performed on the variable. Finally, the size attribute denotes the amount of storage required to represent the variable. The process of assigning a value to an attribute is called binding . When a value is assigned to an attribute, that attribute is said to be bound to the value. Depending on the semantics of the programming language, and on the attribute in question, the binding may be done statically by the compiler or dynamically at run-time. E.g., in C++ the type of a variable is determined at compile time--static binding . On the other hand, the value of a variable is usually not determined until run-time--dynamic binding . In this chapter we are concerned primarily with the type attribute of a variable. The type of a variable specifies two sets:
• •

a set of values; and, a set of operations.

For example, when we declare a variable, say x, of type int, we know that x can represent an integer in the range (assuming 32-bit integers) and that we can perform operations

on x such as addition, subtraction, multiplication, and division.

Compare this with the struct construct of C or Pascal's record . the programmer must be conscious of the fact that defining an abstract data type requires the specification of both a set of values and a set of operations on those values.g.The type int is an abstract data type in the sense that we can think about the qualities of an int apart from any real thing having that quality. Whereas data structure is implementation dependent. one of the primary concerns of the programmer is to develop an appropriate collection of abstractions for the application at hand. Base and Derived class: Base class and Derived class having two same functions – void fun() and void fun1(). it has been only since the advent of the so-called object-oriented programming languages that the we see programming languages which provide the necessary constructs to properly declare abstract data types. As you can see. For example. Ultimately. as in the same example. Indeed. but it has no information about how the List is actually implemented. and then to define suitable abstract data types to represent those abstractions. it only describes what a data type List consists (data) and what are the operations it can perform. using array or linked list.. ADT is the logical picture of the data and the operations to manipulate the component elements of the data. ADT is implementation independent. it is about how the List implemented ie. In so doing. In other words. we don't need to know how ints are represented nor how the operations are implemented to be able to be able to use them or reason about them. the class construct is the means by which both a set of values and an associated set of operations is declared. which only allow the specification of a set of values! ******************************** ADT is a logical description and data structure is concrete.. ADT is in the logical level and data structure is in the implementation level. Data structure is the actual representation of the data during the implementation and the algorithms to manipulate the data elements. In designing object-oriented programs. data structure is how we implement the data in an abstract data type. in C++. . E.

// Explicit call. The fun1() of the Derived class will never be executed. Functions have to be called explicitly. members of a structure are public while that of class are private. Why? Constructors are called whenever an object is created.In main(). Constructors  Constructor name should always be same as the class name. fun1() of the Base class will be executed. public: Test ( int x=0 ){} }. a nameless object is created. Functions always have some return type including void. The fun() of the Derived class gets higher priority. // Implicit call t = Test ( 5 ). Structure and class By default. If fun() is not present in the Derived class. void main(){ Test t. During an explicit call to a constructor. then only the fun() of Base class will be executed. nameless object is created & assigned to t.fun(). create Derived d and d. A function name can be any legal identifier. } Constructor gets called automatically whenever an object gets created. The fun() of the Derived class will be executed. The only way to reach fun1() of the Derived class is to make the fun1() of Base class virtual. If fun1() is called from Base class. . There can never be a situation where we want to return a value at the time of creation of an object. class Test{ int x.  Constructor does not have any return type.

Sign up to vote on this title
UsefulNot useful