Tuesday, 29 September 2015

Low-Level Programming_Post1

Low-Level Programming: Post 1 24/09/2015

24th September 2015 is the date of the first lesson on Low-Level Programming. From the lesson, I have split it up into two segments as to the important aspects that were taught in the lecture.

The first segment that I took out from the lesson was that, in parts of the lesson, we were given a 'refresh' of some of the features that we learned last year. For example, Constructors/Destructors, Object Orientation etc were some of the elements that we learnt towards the end of last year. Below are some notes/details of the features that were reintroduced into our minds.

Constructor: A constructor is a kind of member function that initializes an instance of its class. A constructor has the same name as the class and no return value. A constructor can have any number of parameters and a class may have any number of overloaded constructors. Constructors may have any accessibility, public, protected or private. If you don't define any constructors, the compiler will generate a default constructor that takes no parameters; you can override this behavior by declaring a default constructor as deleted. 

Below is an example of a Constructor:

#include <iostream>
using namespace std;

class Constructor1
{
   public:
      Constructor1()
      {
         cout << "Contains Constructor1" << endl;
      }
};

class Constructor2
{
   public:
      Constructor2()
      {
         cout << "Contains Constructor2" << endl;
      }
};

class Constructor3
{
   public:
      Constructor3()
      {
         cout << "Contains Constructor3" << endl;
      }
};

class MainConstructor
{
   public:
      MainConstructor()
      {
         cout << "Contains MainConstructor" << endl;
      }
   private:
      Constructor1 C1;
      Constructor2 C2;
};

class SecondaryConstructor : public MainConstructor
{
   public:
      SecondaryConstructor() : MainConstructor
      {
         cout << "Contains SecondaryConstructor" << endl;
      }
   private:
      Constructor C3;
};

int main()
{
   SecondaryConstructor SC;
   int x = 3;
}


Destructor: "Destructor" functions are the inverse of constructor functions. They are called when objects are destroyed (deallocated). Designate a function as a class's destructor by preceding the class name with a tilde (~). For example, the destructor for class String is declared: ~String().


Below is an example of a Destructor (The below example was used from the website 'MSDN'):

#include <string.h>

class String 
{
public:
   String(char *ch); //Declare constructor
   ~String(); //and destructor.
private:
   char *_text;
   size_t sizeOfText;
};

//Define the constructor.
String::String(char *ch) 
{
   sizeOfText = strlen(ch) + 1;

   //Dynamically allocate the correct amount of memory.
   _text = new char[sizeOfText];

   //If the allocation succeeds, copy the initialization string.
   if(_text)
      strcpy_s(_text, sizeOfText, ch);
}

//Define the destructor.
String::~String() 
{
   //Deallocate the memory that was previously reserved
   //for this string.
   if (_text)
      delete[] _text;
}

int main() 
{
   String str("The piper in the glen...");
}

Object Orientation: The core of the Object-Oriented programming is to create an object, 
in code, that has certain properties and methods. While designing C++ modules, 
we try to see whole world in the form of objects. For example, a car is an object which has certain
properties such as colour, number of doors, and so on. It also has certain methods such as 
accelerate, brake, and so on.

There are a few principle concepts that form the foundation of object-oriented programming:

Object: This is the base unit of object-oriented programming. That is both data and function
that operate on data are bundled as a unit called as object.

Class: When you define a class, you define a blueprint for an object. This doesn't actually define 
any data, but it does define what the class name means, that is, what an object of the class will
consist of and what operations can be performed on such an object.

Abstraction: Data Abstraction refers to, providing only essential information to the outside world
and hiding their background details, i.e, to represent the needed information in program without 
presenting the details. For example, a database system hides certain details of how data is stored and
created and maintained. Similar way, C++ classes provides different methods to the outside world
without giving internal detail about those methods and data.

Encapsulation: Encapsulation is placing the data and the functions that work on that data in the
same place. While working with procedural languages, it is not always clear which functions work
on which variables but object-oriented programming provides you framework to place the data and
the relevant functions together in the same object.

Inheritance: One of the most useful aspects of object-oriented programming is code reusability. As
the name suggests, Inheritance is the process of forming a new class from an existing class that is
from the existing class called as base class, new class is formed called as derived class. This is a
very important concept of object-oriented programming since this feature helps to reduce the code
size.

Polymorphism: The ability to use an operator or function in different ways in other words giving
different meaning or functions to the operators or functions is called polymorphism. Poly refers to
many. That is a single function or an operator functioning in many ways different upon the usage is
called polymorphism.

Overloading: The concept of overloading is also a branch of polymorphism. When the exiting 
operator or function is made to operate on new data type, it is said to be overloaded.

The second segment that I took from the exercise was the practical side of the lesson, the two exercises. The first as to recode the 'Dungeon Game' that was provided on Microsoft Drive. This exercise was split into three tasks, Fix compiler errors, create a class for equippable objects and add a player attack function. In the group that I worked in, we managed to fix the compiler errors (with the majority of fixing the errors by moving parts of code from private into public) and add a attack function on the player. Unfortunately, the enemy would attack first, which meant the player always died first and before we could change it, we ran out of time.



The second exercise was to design a OPP paradigm on one of the following objects, An Apache Helicopter, A Cybernetic Based Enemy, A Killer Rabbit With Sharp Teeth or An Equippable Magic Staff. We chose the Apache Helicopter as the object to work on. We tried to list all the features that the Apache Helicopter has, such as the rotors, weaponry and so on and split them into their various classes.