Coursewww/ 0040755 0066576 0066562 00000000000 06377070045 0013271 5 ustar 00rjp cs_lec 0000255 0004314 Coursewww/CPPwww/ 0040755 0066576 0066562 00000000000 06566046536 0014470 5 ustar 00rjp cs_lec 0000255 0004314 Coursewww/CPPwww/array.html 0100644 0066576 0066562 00000001530 06413674344 0016463 0 ustar 00rjp cs_lec 0000255 0004314
In C++, as well as declaring fixed size arrays in the same way as in ANSI C, an array of a required type and size can be generated dynamically by using the new operator and may be returned to the heap by the delete operator, as follows.
int main() { int *a1; ... a1 = new int[12]; return 1; }
Back to index Coursewww/CPPwww/binding.html 0100644 0066576 0066562 00000004165 06413674344 0016766 0 ustar 00rjp cs_lec 0000255 0004314
Statically bound definitions are hidden in the sub-class by a new definition with the same name. This is the familiar scope rule used in Pascal when nested procedures are declared. Any use of the name in the parent class is still bound to the definition within that class, which means that its use must be as was intended by the writer of that class. Uses within a sub-class where it is redefined and within any classes which are sub-classes of that are bound to the definition in the sub-class.
Dynamically bound definitions (virtual definitions) are also hidden in the sub-class by a redeclaration at that level. However, they are replaced in the parent class as well. This is very useful where, for instance, a family of object types all have the ability to be displayed but the effect of this differs for each of them. By noting in the parent class that such an operation is mandatory and that it has a certain form, we are free to use it in the parent class and assume each specialisation will provide an over-riding definition which replaces it when used. In some languages the parent class need not provide a default implementation, in others it must always do so.
Most languages in common use have adopted a variation of SIMULA's combination of default static binding with an explicit option to make certain operations dynamically bound. C++ is one of these and uses the virtual specifier to indicate dynamic binding.
Back to index Coursewww/CPPwww/classdecl.html 0100644 0066576 0066562 00000003220 06566046536 0017305 0 ustar 00rjp cs_lec 0000255 0004314
An example of a class declaration.
// definition file named listhead.h for list package typedef struct List_Item { int val; List_Item * next; } List_Item; class List_Head { List_Item* next; public: void insert(List_Item *); void printout(); }; // implementation file for the List_Head class #include "listhead.h" void List_Head::insert(List_Item * Item) { Item->next = next; next = Item; } void List_Head::printout() { List_Item * curr; curr = next; while(curr) cout << curr->val << '\n'; }
The implementation matches function declarations to the prototypes given inside the class body. It prefixes the name of each function with the class name to which it is bound followed by a double colon. Before a class object can be created, implementations must be provided for all its functions.
Back to index Coursewww/CPPwww/classdecl.html~ 0100644 0066576 0066562 00000003204 06413674345 0017501 0 ustar 00rjp cs_lec 0000255 0004314
An example of a class declaration.
// definition file named listhead.h for list package typedef struct List_Item { int val; List_Item * next; } List_Item; class List_Head { List_Item* next; public: void insert(List_Item *); void printout(); }; // implementation file for the List_Head class #include "listhead.h" void List_Head::insert(List_Item * Item) { Item->next = next; next = Item; } void List_Head::printout() { List_Item * curr; curr = next; while(curr) cout << curr->val << '\n'; }
The implementation matches function declarations to the prototypes given inside the class body. It prefixes the name of each function with the class name to which it is bound followed by a double colon. Before a class object can be created, implementations must be provided for all its functions.
Back to index C++ in the same way as a struct, except that it also contains function declarations. Such functions may either be declared in full or as prototypes. It is normal to only give prototypes in the class declaration and to provide their implementation separately, unless they are requested to be expanded inline. This allows more control over the building and use of libraries incCoursewww/CPPwww/classes.html 0100644 0066576 0066562 00000002562 06413674345 0017011 0 ustar 00rjp cs_lec 0000255 0004314
Note that the object is defined on the assumption that some of its operations can depend on operations to be supplied by other objects. This is the notion of delegation and allows us to treat objects as interacting components.
When we come to implementation of the system, the description above is fleshed out with concrete decisions about how the internal state of each type of object will be maintained. Typically this involves defining instance variables and the implementation of both the externally visible (public) operations used in the design and of any new internally restricted (private) operations.
Back to index Coursewww/CPPwww/classtat.html 0100644 0066576 0066562 00000001112 06413674346 0017161 0 ustar 00rjp cs_lec 0000255 0004314
Back to index Coursewww/CPPwww/classtemp.html 0100644 0066576 0066562 00000004555 06413674346 0017354 0 ustar 00rjp cs_lec 0000255 0004314
The use of inheritance in this way allows a form of polymorphism quite different from that used in functional programming languages such as Standard ML. In SML a function can be defined to handle values of unspecified type. Once the function is used, that use is bound to the type given as actual parameter to match the unspecified type of the formal parameter. This depends on the inbuilt features such as list handling, which are defined over any collection of values of one type.
In C++ a similar form of polymorphism is achieved through a feature known as templates. The most widely used templates are, probably, class templates. These are essentially classes with a second list of parameters, specifying formal type names, which will be bound to actual types when an object is generated from this template.
In addition to types, constant expressions, object addresses and function addresses (not members unless static) may be passed as arguments to templates. In effect a textual substitution is performed when an actual argument is passed.
templateclass buffer{ E * buf[size]; public: buffer(); void store(E*); E* remove(); };
This defines a class of buffers, where the items to be buffered are not defined. The E* pointer buf will be set to refer to an array holding size E* elements. Examples of uses of this template are:
bufferB1; // Create buffer of ints, size 1024; buffer * O_Buf; //Create pointer to buffer of O* size 32; O_Buf = new buffer ; // Types and args must match;
To avoid clumsy repetition of template arguments, a typedef may be used for any type, e.g.:
typedef bufferchar_buf;
Back to index Coursewww/CPPwww/compound.html 0100644 0066576 0066562 00000003673 06413674347 0017206 0 ustar 00rjp cs_lec 0000255 0004314
AS in the ANSI C notes, we use an extended form of BNF notation to describe the syntax of our language. If you are not familiar with the format we use see our definition of EBNF.
compound-statement { statement-list ? } statement-list statement statement-list statement statement declaration-statement expression-statement function-statement flow-control-statement compound-statementA declaration-statement is any declaration of a variable. As in C, variables can be declared along with an initialsation. In C++ this can include a value computed in earlier statements in that block. In C this is impossible, as all declarations come at the beginning of the block.
An expression-statement is an expression, including assignments.
A function-statement is a function call.
A flow-control statement is a conditional, switch or loop statement, as in C.
Example 2
int main() { int j = 4; j++; int k = ++j; return 0; }
Back to index Coursewww/CPPwww/constrinher.html 0100644 0066576 0066562 00000001073 06413674347 0017710 0 ustar 00rjp cs_lec 0000255 0004314
Dad::Dad(int x) { } Fred::Fred(int x, int y) : Dad(x) { }
Where Fred is a class derived from Dad. We shall use this frequently in our case studies.
Back to index Coursewww/CPPwww/databs.html 0100644 0066576 0066562 00000007641 06413674347 0016617 0 ustar 00rjp cs_lec 0000255 0004314
Data abstraction with conventional imperative languages, like C, lets us define operations such as insertion and deletion for lists within the body of a procedure and then to program without regard to how they are achieved internally. The definition of these operations is still separate from the definition of the structures to which they are applied, however. Nothing prevents accidental use with the wrong instance of a data structure, so that an array representing a first in first out queue could accidentally be used by the push operation intended for a similar array used to represent a stack.
Object based abstraction says that the definition of a data structure and of the operations upon it should be as a single unit. Thus a list is no longer an array plus a set of operations on that array and the values which it can store. Instead a list is an object with a state and a set of operations. The state tells us what the effect of each operation will currently be. The operations modify and report on the state of the object to which they belong. Only operations defined for a data type are allowed. Information hiding implements Parnas' rules for modularity in programs [1]. He said that:
Data abstraction and information hiding are the goals we wish to achieve. One approach would be to regard them as disciplines which should be followed in programming. Encapsulation says that they should be enforced by the language and forbids direct access to data within an object, from outside it, and to data outside an object, from within it. Most object oriented languages allow this to be enforced for both data (instance variables) and for private operations. Only those operations explicitly made visible should be accessible outside the object and all such operations should apply to a particular instance of an object type or class. Only data passed in as parameters should be allowed to enter an object from outside.
Back to index
Coursewww/CPPwww/examples/ 0040755 0066576 0066562 00000000000 06413674355 0016303 5 ustar 00rjp cs_lec 0000255 0004314 Coursewww/CPPwww/examples/functemp.cpp 0100644 0066576 0066562 00000001116 06413674350 0020617 0 ustar 00rjp cs_lec 0000255 0004314 #include