# Thomas Sampson

## Extracting data from float

Earlier this week i was presented with the simple problem of checking if floating point number was a whole number. I stumbled upon the useful function modf and thought it was worth a quick post. the modf function requires <stdio.h> and <math.h>, and allows you to separate the integral and fractional part of a floating point number.

Taking the value 5.24f as an example…

• Integral part = 5
• Fractional part=0.24

modf is fully documented here but is rather simple. modf takes two arguments, the first is the value you wish to query (in the case above this would be 5.24). The second argument is a pointer to where the integral part should be placed. The return value of the method is the fractal part. Therefore the solution to my original problem (testing if a float is infact an integer value) is as follows…

Solution

int f;

if(modf(myValue,&f)==0)
{

//myValue is a whole number

}

## Miro version 2.0 is here!

Not downloaded just yet but from the screenshots looks like a major improvement on version 1 which was already great! The main improvements for me are the reduced memory footprint, and finally the option to pop out video! A must for anyone who watches video online! Get it here

Not convinced? Vid below….

## Spotify!

I have amazed by this service ever since I signed up for it a couple of days ago. Basically, Spotify is a free service for streaming a gigantic catalogue of streaming music (supported by the occasional unobtrusive audio ad). The interface looks very similar to that of iTunes and is very easy to work with. Simply search for the album, artist or track you want, and listen away. Playback is so seamless you almost forget your not actually in iTunes listening to local content, and with added features such as “collaborative playlists”, allowing users to build and share playlists in real time, this software is definitely worth a try.

UPDATE: Today Spotify announced that a small section of their catalogue will now be restricted to certain geographical areas. This is something I have noticed but not to any great extent. Read more here.

## Understanding the virtual keyword in C++

The virtual keyword is used to allow for true polymorphism to take place in your c++ application.

A real world example

Let’s create a situation which requires both a base and specialised (derived) class architecture. We will use the real world example of Transport and Car, where Car is derived from transport (as would be a bike, or a plane for that matter). Here are the defenitions for each object

The Transport Object

```class Transport

{

private:

int topSpeed;

public:

void IdentifyMyself()

{
cout << "I am a Transport Object";
}

};```

The Car Object

```class Car : public Transport

{

private:

public:

void IdentifyMyself()

{

cout << "I am a Car Object" ;

}

};```

Simple enough?

So, lets examine what actually goes on here

1. The Transport class is created as our base class.

2. The Transport class has one private data member.

3. The Transport class has one public method named IdentifyMyself() which outputs a message.

4. The Car object is created as our specialised/derived class.

5. The Car publicly inherits from the base class Transport, obtaining its private data member topSpeed.

6. The Car also defines a public method named IdentifyMyself() which also outputs a message.

What next?

The important part to take from above is that Car inherits from Transport, and that they both contain a defenition for the method IdentifyMyself().

So let’s use our objects! Here is a snippet of code which uses our objects and their methods…

Transport t;

Car c;

t.IdentifyMyself();

c.IdentifyMyself();

Output

I am a Transport ObjectI am a Car object

So, the Car’s method overrides the base class method and the two objects print a different message, simple. Now consider the following….

Test code

Transport* t

Car c;

t=&c;

c.IdentifyMyself();

t->IdentifyMyself();

We now have a car object as before, but this time we setup a pointer “t” (of the type Transport) to point to our Car, “c” . We are allowed to do so as “Car is a type of Transport”. However, take note of the output.

Output

I am a Car objectI am a Transport object

Even though our pointer “t” points to a Car object, we still call the IdentifyMyself() method of the base class. This is because there is no overriding, and as the pointer is of the type Transport, we simply call the Transport’s method, no longer the one overridden by Car. This is where the virtual keyword comes into play.

Let’s redefine the base class Transport to unclude a virtual method…

The New Transport Object

```class Transport

{

private:

int topSpeed;

public:

virtual void IdentifyMyself()

{
cout << "I am a Transport Object";
}

};```

Now lets run the same code again and notice the difference in output….

Test code

Transport* t

Car c;

t=&c;

c.IdentifyMyself();

t->IdentifyMyself();

Output

I am a Car objectI am a Car object

This time when we dereference the pointer “t”,  even though our pointer is of the type Transport, due to Transport using the virtual keyword before IdentifyMyself(), it now forces the program to downcast to our inherited object Car and call the overridden IdentifyMyself() method instead.

How does this work under the hood?

Let’s think back to when our Transport class  did not employ the virtual keyword. At compile time we can collect up all Transport’s class methods and know where they will reside in memory. However, when we use the virtual keyword, when accessing a pointer to a Transport object, these methods could be anywhere in memory (depending upon the layout of the derived type which overrides them).

The way to resolve this is for each object which uses virtual methods, to keep a vtable (virtual method lookup table) which keeps a list of virtual function pointers. This vtable is always kept at memory offset 0 and can be though of as a hidden datamember for classes which use the virtual keyword or override virtual methods. This can be seen when watching a pointer to our Transport instance, using the Visual Studio Debugger as shown below as __vfptr (virtual function pointers).

This is done automatically by the compiler for any base class which employ virtual methods, and inherently for those that derive from the base class. This vtable is always kept at memory offset 0, so we can always take a pointer to a Transport object, and check its vtable and call the right method. This becomes more complex with multiple inheritence.

Summary

Imagine increasing the scale of this program to include Planes, Trains and Motorbikes. We could now keep a list, 100 items long, of transport pointers…

Transport* mylist[100];

…and iterate through this list, calling ->IdentifyMyself() on each item. Now that the base class implements the virtual keyword, true polymorphism takes place and any derived classes which choose to override this method, get their chance to output a custom message. For those classes that dont override the IdentifyMyself() method, the base class method will be called.

## PDF Merging

I am currently in the process of merging all the chapters of “Graphics Programming Black Book” by Michael Abrash, I will be adding this to my site later this evening (I assume I am breaking no laws by doing so). Anyway, I expected to have to pay for a pdf merger but found some neat freeware software by Adolix named “Split and Merge PDF 1.3″, available for download here.

The software is capable of merging and splitting pdf documents and appears to be working well so far!

UPDATE: Seems that the software can only handle merging 5 documents at a time, however reccursive merging appears to be working well.

## SQL Generator

I found this tool today which allows you to use a webUI to design a database table, and will then generate you the SQL code to create that table at runtime.

http://ondras.zarovi.cz/sql/demo/

I used this to design a table to use with sqlite3 and although the code generated is intended for use with mysql, a few minor adjustments allowed me to execute this on a sqlite3 database with no problems.