C++ for ObjC Programmers: Part II

In Part I, I talked about the differences between static typing and dynamic typing. I previously said I won’t be able to cover C++ from the very basics. However, for those who already know C, I can give a whirlwind tour of C++. This post is a shorter one,but hopefully useful.

1 Highlights

1.1 Some differences from C
  1. Including headers: #include<iostream> as opposed to #include <iostream.h>
  2. Reference parameters instead of “Pointer parameters”: In a C swap function, you would have:void swap(int *x, int *y)
    {
    int temp = *x;
    *x = *y;
    *y = temp;
    }In C++, you would use reference parameters. You would implement swap as follows:
    void swap(int &x, int &y)
    {
    int temp = x;
    x = y;
    y = temp;
    } Here, the & symbols in front denote that the addresses, and not the value of the parameters is passed in, so the original variables(and not copies of the variables) are modified. You can also define standalone references(although this is not widely useful). You can do
    int t = 0;
    int &tref = t;
    tref++; //changes t
  3. You do not have to use preprocessor directives like #define PI 3.14159. Much better is the use of true constants like const float PI = 3.14159; You can also use enum(I have a post on enum), but usually the const float PI method is more convenient.
  4. Use inline functions instead of the dangerous macro function expansions. The compiler can choose to ignore you if it thinks the function is better left as a real function(trust the compiler if you set the optimization level high!). Example of inline function:
    inline square(double x){ return x*x;}.
    Note that this is not inline because I put it all in one line, but because the compiler “search/replaces” all function calls to square in our other code by x*x if it decides to follow our guideline. WIth inline functions, you tradeoff better performance for an increase in code size.
  5. Pointers can be used just like in C. However, when working with the STL(Standard Template Library), one often uses iterators instead of pointers.
  6. Use new and delete rather than malloc and free for dynamic memory allocation. New and delete are much easier to use, and C++ programmers can bid malloc and free a “fond farewell”–Scott Meyers. Example:int *int_ary = new int[100];
    /* more code here */
    delete[ ] int_ary; The above snippet was for dynamically allocating an array of ints(same type of syntax for array of objects). What if we just want to dynamically allocate just one object? Example:int *ptr = new int; delete ptr;

Next time: basics of C++ classes, inheritance, polymorphism, virtual functions, and smart pointers.

Meanwhile, other C++ resources: C++ Reference, Cplusplus.com (this last one is not necessary the most accurate source, but useful for a quick reference).

Advertisements

One response to “C++ for ObjC Programmers: Part II

  1. Pingback: C++ for ObjC Programmers Part III: Smart Pointers « Mac OS X coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s