Sunday, August 26, 2007

polymorphism in C

Even polymorphism can by implemented in C. Polymorphism in Java and C++ is implemented by associating with each instance a table of functions. Polymorphic method names are converted to indexes into this function table. When a method call on a particular instance is executed, the function table for that instance (actually these tables are shared among the instances of a class), used to find the proper function to execute.

Since C has the ability to store pointers for functions as a basic data type, and the ability to calla function from such a function pointer. It is straightforward (though not always fun) to explicitly duplicate the function table mechanism and implement polymorphisms by hand.

Al

How to read C declarators PART 2

Function pointers and combinations

The C "forward" declaration:

 1.) extern int foo();

just says that foo is a function returning int. This follows the same pattern for reading declarators as you saw in previous post. Start at foo and look right. You see () so say function. You look left and see int. Say int.

        2.) extern int *foo();

Yep, you say foo is a function returning a pointer to int.

Now for the big leap. Just like we can make a pointer to an int or whatever, let's make a pointer to a function. In this case, we can drop the extern as it's no longer a function forward reference, but a data variable declaration. Here is the basic pattern for function pointer:

 3.) int (*foo)();

You start at foo and see nothing to the right. So, to the left, you say pointer. Then to the right outside you see function. Then left you see int. So you say foo is a pointer to a function returning int.

Here is an array of pointers to functions returning int:

 4.) int (*Object_vtable[])();

One last, incredibly bizarre declaration, for fun:

 5.) int (*(*vtable)[])();

This pointer to a vtable is set to the address of a vtable.

In summary:

 int *ptr_to_int;
int *func_returning_ptr_to_int();
int (*ptr_to_func_returning_int)();
int (*array_of_ptr_to_func_returning_int[])();
int (*(*ptr_to_an_array_of_ptr_to_func_returning_int)[])();

regards
Al

How to read C declarators PART 1

Reading C declarators in a precise manner.

The rule goes like this:

"Start at the variable name (or innermost construct if no identifier is present. Look right without jumping over a right parenthesis; say what you see. Look left again without jumping over a parenthesis; say what you see. Jump out a level of parentheses if any. Look right; say what you see. Look left; say what you see. Continue in this manner until you say the variable type or return type."


The degenerate case is:

int i;

Starting at i, you look right and find nothing. You look left and find the type int, which you say. Done.

Ok, now a more complicated one:

int *a[3];

Start at a. Look right, say array of size 3. Look left and say pointer. Look right and see nothing. Look left and say int. All together you say a is an array of size 3 pointers to int.

Adding parentheses is when it gets weird:

int (*a)[3];

The parentheses change the order just like in an expression. When you look right after a, you see the right parenthesis, which you cannot jump over until you look left. Hence, you would say a is a pointer to an array of 3 ints.


bye for now
Al

Friday, August 24, 2007

Object oriented modelling in C - PART 3 - Inheritance

Inheritance is a mechanism by which new and more specialized classes can be defined in terms of existing classes. When a child class (subclass) inherits from a parent class (superclass), the subclass then includes the definitions of all the attributes and methods that the superclass defines. Usually, the subclass extends the superclass by adding its own attributes and methods. Objects that are instances of the subclass contain all data defined by the subclass and its parent classes, and they are able to perform all operations defined by this subclass and its parents.

This kind of class relationship can be implemented in C by embedding the parent class attribute structure as the first member of the child class structure. This structuring results in such an attribute alignment that a pointer to the Child class can always be safely cast (upcast) on the pointer to the Parent class. In particular, this pointer can always be passed to any C function expecting a pointer to the Parent class. (To be strictly correct in C, you should explicitly upcast this pointer.) This means that all methods of parent classes are automatically available to child classes-in other words, they are inherited.

Object oriented modelling in C - PART 2 - Encapsulation

You can achieve packaging of data with functions in C by making each class attribute (instance variable) a field in the C struct. You implement class methods as C functions that take as their first argument the pointer to the structure of attributes (the this pointer). You can further strengthen the association between the attributes and methods by a consistent naming convention for method names. The most popular convention that I adopt is to concatenate the structure name (class name) with the operation name. This altering of function names is part of name decorating (also known as name mangling) and is performed implicitly by most C++ compilers. Because name decorating eliminates method name clashes between different classes, it effectively partitions the flat C function namespace into separate namespaces nested within classes.

The next aspect I address by a coding convention is access control. In C, you can only indicate your intention for the level of access permitted to a particular attribute or a method. Conveying this intention through the name of an attribute or a method is better than just expressing it in the form of a comment at the declaration point. In this way, unintentional access to class members in any portion of the code will be easier to detect. Most OO designs distinguish the following three levels of protection:

  • private-accessible only from within the class
  • protected-accessible only by the class and its subclasses
  • public-available anywhere (de-fault in C)
Optionally, a class may provide a destructor, which is a method responsible for releasing the resources allocated during the lifetime of an object. Whereas there may be many ways of instantating a class (different constructors taking different arguments), there should be only one way of destroying an object.


Object oriented modelling in C - PART 1

In spite of unquestioned advantages of OO languages, C still remains the best known and widespread implementation language. Most developers still exclusively use procedural programming techniques, and many are unaware that it's quite easy to implement key OO concepts directly in C. Promoting this awareness may be important for at least two reasons.

The first is leveraging OO technology. Most OO designs can be implemented in C, but many developers wouldn't consider them without the availability of an OO language. This unnecessarily limits application of the technology.

The second is smoothing transition from procedural to OO thinking. Migrating to OO technology can require quite a leap in your way of thinking. Implementing OO concepts in currently used and familiar languages gives you an opportunity to get exposed to the new programming paradigm right away and without major investment.


Al

Friday, August 17, 2007

Download video from youtube

It's strange that none of the video sharing sites provide a simple one-click button to download videos. So we will have to take the help of easy workarounds to download the videos. Here are three different ways to download any video from the internet though internally, they employ the same hack (Right-click -> "Save Target As" OR copy the link into your download manager. but change the file extension to .flv)

Approach 1: The easiest way is to copy your video URL and paste it on KeepVid Lite. When you click submit, you will be provided with a link to save the video as an FLV file.

Approach 2: Bookmark this Link or drag it your browser links toolbar. When you want to download a certain video, open the link in your browser and click the KeepVid button.

Approach 3: For Firefox Users - Install the VideoDownloader Extension from Mozilla Addons website - It downloads all embedded objects on a webpage including the video clips.

read more on
http://labnol.blogspot.com/2006/07/10-interesting-things-you-can-do-with.html

bye

Thursday, August 16, 2007

Embed Audio Files in HTML pages

To add the wav file to your html page use the following:

#EMBED src="file.wav" autostart=true loop=false volume=100
hidden=true@#NOEMBED@#BGSOUND src="file.wav"#/NOEMBED@


replace - @ with >
replace - # with <
Where the a file named file .wav has been uploaded to your site in the same directory as the page which contains the EMBED tags.

In the above example where autostart=true, the file will automatically start playing. Use this tag for background sound which should start immediately once it is downloaded.

You can also use loop=true where you want the sound to repeat over and over. By editing a short sound and using the loop=true option you can create continuous audio from a very short audio cut. For example: waves, applause, engine sounds and so on.

get more on
http://www.tips-tricks.com/sound.asp

bye
AL

Basic Information About Sound Files

So start with some audio formats, which might be helpful in adding audio features in my web pages.

The two most widely supported file formats for sound files are MIDI and WAV. As with graphic files, you'll want to keep the size of the file as small as possible while maintaining quality.

The differences between WAV and MIDI are that MIDI files are generally a great deal smaller, however, many types of audio, such as speech, don't work well as MIDI files. MIDI is a good file format for music. A one minute WAV file can be as large as 1MB in size, while a 1 minute MIDI file can be less than 25K. Using software convertors, it may be possible to resample WAV files to reduce their file size.

Some helpful and interesting links are -


  1. The Wav Place - offers movie audio clips, cartoon sounds, and music files
  2. MidiWeb - offers technical information about MIDI files and MIDI interfaces, a library of download able MIDI files.
  3. FileCity offers a huge archive of MIDI files, including movie theme songs, rock music, holiday songs, and more.
  4. MidiFarmalso offers a library of downloadable MIDI files.
  5. WavCentral offers a huge archive of audio files that you can download.
  6. MovieWavs offers...you guessed it! Wav files from popular films.
bye for now
AL