Variant:9 Topic: Templates Purpose of work: • Studying the use of templates • Studying the advantages of templates • Learning how to define and use templates • Solving problems with using templates Condition: а) Creaţi o funcţie şablon, care schimbă după perechi elementele masivelor în felul următor: primul element va avea valoarea sumei perechii, dar al doilea diferenţei perechii. De exemplu: lista- 0 2 3 4 3 6, rezultatul 2 –2 7 –1 9 –3. b) Creaţi clasa parametrizată Tree – arbore binar. Clasa trebuie să conţină constructorii, destructorii şi funcţiile add, in, funcţia de eludare a arborelului şi operatorii de intrare/ieşire. [1] Short theory on the laboratory work: Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type. A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept. There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector or vector . [1] Templates are a way of making your classes more abstract by letting you define the behavior of the class without actually knowing what datatype will be handled by the operations of the class. In essence, this is what is known as generic programming; this term is a useful way to think about templates because it helps remind the programmer that a templated class does not depend on the datatype (or types) it deals with. To a large degree, a templated class is more focused on the algorithmic thought rather than the specific nuances of a single datatype. Templates can be used in conjunction with abstract datatypes in order to allow them to handle any type of data. For example, you could make a templated stack class that can handle a stack of any datatype, rather than having to create a stack class for every different datatype for which you want the stack to function. The ability to have a single class that can handle several different datatypes means the code is easier to maintain, and it makes classes more reusable. [2] Function template Syntax
Comentariul tau va fi primul
23:40Laborator: Topic: Templates Profesor: Kulev