Low-Level Programming: Post 2
01/10/2015
1st October 2015 is the date of the second lesson on Low-Level
Programming.
The start of the lesson was essentially a recap of some of the features
learnt from the first lesson. I have already gone into some detail about some of
the features learnt from the first lesson on my first post. The next paragraph
will show what else was taught in the lesson, from Static Member Variables,
Public/Private Inheritance, going into detail on Constructors and
so on. The first part explains about Static Member Variables, with some of the
following notes used from the lessons mixed together.
- Static Member Variables
force a single shared instance amongst all objects instantiated of that
class.
- They belong to the class
itself and are shared amongst instances.
- As they are treated
independently of class instancing they must be explicitly defined to a
non-zero value (where a non-zero value is desirable) outside of the class
definition.
- To access them we use the
scope operator '::'.
- The static variable resides
in the class scope but does not belong to objects of that class. It
belongs to the class.
- Class definitions are often
included in multiple translation units i.e. CPP. The static variable
should be shared amongst all and not stored as copies.
- As it resides within the
class scope (similar to namespace) we access it using the :: operator. We
can access it through instances of that class, but it then becomes unclear
that it's static access.
- Static values are stored in
the data segment which is a portion of the object file or the virtual
address space of a program that contains variables. Const statics are
stored in the rodata segment.
The next paragraph is about Public/Private Inheritance. The following info was found on a website about Inheritance.
One of the most
important concepts in object-oriented programming is that of inheritance.
Inheritance allows us to define a class in terms of another class, which makes
it easier to create and maintain an application. This also provides an
opportunity to reuse the code functionality and fast implementation time.
When creating a
class, instead of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members of an
existing class. This existing class is called the baseclass, and
the new class is referred to as the derived class.
The idea of
inheritance implements the is a relationship. For example,
mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Public Inheritance: When deriving a base class from a public base class,
public members of that base class become public members of the derived class
and protected members of that base class become protected members of the
derived class. A base class's private members are never accessible through
calls to the public and protected members of the base class.
Private Inheritance: When deriving from a private base class, public and
protected members of the base class become private members of the derived class.
The following line(s) is some more information based on Constructors.
Initialisation
- Member Variables need some 'Sane' values. (int maxBoost = 50).
- Use Initialiser lists and not the body of the constructor.
Viral Functions
- Don't call virtual functions from constructors or destructors. They don't exist.
- The most derived function will never get called, as that part of the class doesn't exist yet.
- As construction happens at the lowest level first, the derived member variables won't have been initialised.
- Compliers may warn.
- The program may compile but not link.
- If it makes it to runtime, it'll either crash or perform unintended behaviour.
The next part of this page are some of the new features that we learnt from the lesson.
Default and Delete
- If we don't define default constructors and assignment operators the complier will do this for us.
- We can use the default keyword to acknowledge we intend for the compliers default version to be used.
- Use 'Delete' when we don't want to allow usage of functions.
Delete
- Traditionally we use private non-defined functions.
- We can use delete to invalidate the function call both to automatically constructed functions and ones of our choosing.
- Will also use template instantiations.
After the lectural side of the lesson, we moved onto the practical side of it. The exercise given to use was to work on the DungeonGame from the last lesson and refactor the code using OOP and how we would handle the animations per object. We quickly split the various components into categories, such as the bat having a render, the 'Newton's Cradle' having an animation (as if the game was loading) and the player itself. The Newton's Cradle and the Bat were put into the Redner category as they both get displayed after some time when the program runs, but an Animation feature was added into the Newton's Cradle object as it was the only object in the game to have an animation. The player and the bat shared the health and attack functions, however, and so were linked together as a result. Sadly, we ran out of time to finish the task(s) and couldn't continue further.
No comments:
Post a Comment