Friday, 22 January 2016

Low-Level Programming_Post12

Low-Level Programming: Post 12 21/01/2016

January 21st 2016 is the date of the thirteenth lesson on Low-Level Programming.

The reason as to why I skipped a post last time out on December 10th 2015, since that was the date of the twelfth lesson on Low-Level Programming, was due to the extremely low attendance in that lesson, which was most likely due to the coursework deadline for this course being on the same day. This resulted in the lesson being cancelled and we were sent on our ways.

Below are the points as to what we were taught in the lesson...


  • Template Functions/Classes: How to implement template functions and template classes.
  • Generics: Templates, what are they and how do they work?
  • Compiler Directives: How to implement template functions and template classes.
Directives

Directives
  • Compiler Instruction: Directives are designed to dictate the compiler’s behaviour and do not form part of the language’s syntax.
  • C++: C and C++ support a simple macro pre-processor. Lines which start with the # symbol indicate a directive.
  • Directive: They are responsible for ‘directing’ the compilation process.
Pre-Processor
  • Pre-Processor (1): The pre-processor is responsible for substituting these expressions into text that alters the source code that’s about to be compiled.
  • Abilities (1): They can replace tokens in the text, insert the content of other files into the source file or even remove lines of code that may otherwise had been compiled.
  • Placement (1): Pre-processor directives can appear anywhere in a source file, but they apply only to the remainder of the source file.
  • Supported #: The following pre-processor directives are supported; #define, #error, #import, #undef, #elif, #if, #include, #using, #else, #ifdef, #line, #endif, #ifndef, #pragma.
  • #Define/#Undef: Used to define or un-define a pre-processor tag.
  • #Include: Includes the source from another file.
  • #Pragma: Allows machine/OS specific code which retains overall compatibility.
  • #If/#Else/#Elif: IF / ELSE style clauses for the pre-processor.
  • #Ifdef: Checks to see if a tag is defined.
  • #Error: Emits a user-error at compile time and fails compilation process.
  • Pre-Processor (2): The pre-processor is responsible for substituting these expressions into text that alters the source code that’s about to be compiled.
  • Abilities (2): They can replace tokens in the text, insert the content of other files into the source file or even remove lines of code that may otherwise had been compiled.
  • Placement (2): Pre-processor directives can appear anywhere in a source file, but they apply only to the remainder of the source file.
Some Guidance...
  • Pre-Processor: Do not use #defines. They are text substitution and complicate debugging.
  • Macros: Do not use macros, they start with #define and often lead to complicated bugs.
  • Platform: Do use them for platform specific code.
Templates

Generics
  • Templates: As discussed previously it’s possible to write code that operates independently of the type it’s working with.
  • Blueprint: A template acts as a blueprint for creating a generic class of function. The STL is based around this principle.
  • Type Safe: Even though this is generic programming, the type safety is maintained when using templated code.
Template Code
  • Duplication: Function templates allow the creation of functions that can adapt to the type or class being worked with, without repeating the code for each type.
  • Parameters: This is achieved through the use of template parameters. These are special types of parameter, but the class can use them like any regular parameter.
  • Compile-Time: As discussed last year, it’s important to understand that the template code is evaluated at compile time.
  • Function Templates/Declaration: There are two ways of declaring a template function, the difference is the keyword. There are semantically identical.
  • Syntax: Functions are one of the most common usages in generic programming i.e. the idea that a function can use the same code on any given type.
  • Interface: When we use templates the interface is implicit. The templated object’s interaction determines it’s interface even though it’s type is unknown.
Classes
  • Template Classes: Just like templates, it’s possible to create templated classes. The typename defined can then be used throughout the class’s functions and variables.
  • Member Definitions: The advantage of the template class over template function is due to the fact member variables can be stored in the class of type T.
  • Type Specification: When using a class that utilise templates, it becomes necessary to provide the type we want to use with the class via the <> brackets.
After the first few slides, we were given a practical task. It was essentially what we had just learnt in the lesson. The stages in the practical were as followed...

1. Create a function called min that takes a template argument named T
2. The function will need to take two args of type T
3. Set the return type as type T
4. Return the result of the two args added together.
5. Test by printing to result to the console.
6. Create a function called max that determines the greater of the two.
7. Create a function called create that returns a newly allocated block of said type. Hint: the function does not require any parameters.

Unfortunately, due to not having enough experience and knowledge in this field, I didn't get very far in the task. Although I managed to add up the args together and show it, it was via a different method.

After a 5 minute break, we resumed the theoretical side of the lesson.

Varadic Templates
  • Arguments: Wouldn’t it be good if; templates could take a variable number of arguments.
  • C++11: C++ 11 standard provides the ability to use variadic templates, which allows this. Logic is resolved at compile-time.
Explicit Interfaces
  • Generic Code: Using templated functions allow the usage of the same code regardless of the types being used.
  • Specialization: Occasionally, it becomes necessary to handle certain types in an alternative way. We can explicitly define this in the codebase, thus creating an explicit specialization.
  • Partial or Full: If a class template is specialized by a subset of it’s parameters it’s partial otherwise it’s known as full specialization.
Inheritance
  • Template Classes: A template class that inherits from another template class. Inheritance from a template class. A template class that inherits from a non-template class. A template class that uses multiple inheritance.
  • Member Definitions: The advantage of the template class over template function is due to the fact member variables can be stored in the class of type T.
  • When using a class that utilise templates, it becomes necessary to provide the type we want to use with the class via the <> brackets.
After finishing the theoretical side of the lesson, we moved onto another practical session. This time, we had to create a UML diagram showing all the classes and their relationship to each other in the form of the game Snake, which was one of the parts of the assignment we were tasked to do.

I listed down all the features in the Snake game, such as the player, the walls, the points and so on and then extended it by adding the variables in each of their respective classes. I didn't have enough time left to connect them together, however.

1 comment:

  1. Very smartly describe about low level programming how to use its coding while describing it. This is very smart work of the blogger.

    ReplyDelete