50-must know things in C++…. PART3

Just before diving into real Object Oriented stuff I have to mention several important things about C++, with respect to C:

13. In C++ are references ! 🙂
Oh no , pointers again, or maybe worst ! Actually it is easier with references than with pointers or maybe not, in any case references are much more limited … let’s see some basic differences in between them

    when you assign a reference you tie that reference to that variable and you are not allowed to reassign it
    there is no reference arithmetics (you cannot add or subtract two references, you cannot increment them)
    there is no NULL reference, as for the pointers, a reference has to be bound on some variable, you cannot create an uninitialized ref
    you cannot chain references, a reference must “refer” to a variable, it is not like with the pointers, a pointer can point to another pointer
    you cannot create an array of references

Besides the enumerated things there is the classical example with swap() function when three types of passing arguments are presented: pass by value, by pointer and by reference. Passing by value do not achieve the desired result, corresponding function makes some copies of the values to be swapped and when is called it has no effect because it acted over its local variables. Passing by pointer and by reference (only in C++) affects the variable which has to be changed, not a local copy of it. We can say that the using reference is a more elegant way to write code but in this case the outcome is the same for both, pointers and references.

14. In C++ structs and enums are  separate data types … but you will have to be careful when using them, because C++ is little more strongly typed than C (here’s one point where C++ gives you the freedom to program how do you like it but you’ll have to be aware how you’ll program)

It worth mentioning that the C language developped by Ritchie and Kernigham did not have enumerated types(enums are ints in C), this was later included
in ANSI C standard from 1989.
A simple definition of an enum does not allocate storage, but when this is instantiated. As I said, C+ is also slightly stronger typed than C because it does not allow
to assign to an enum type another value than defined:

enum Numbers{

    ONE,
    TWO,
    THREE

};
Numbers a = 2; // this is illegal in C++, it must be one of ONE, TWO, THREE

15. In the context of classes the notions of variable lifetime and scope slightly changes

The scope of an object or variable is simply that part of a program in which the variable name exists or is visible to the compiler.

Generally in older languages the two are the same, so lifetime equals scope, it worth mentioning here the notorious exception of static variables in C.  Not only is it difficult, it is not all that useful in the general case when it does occur. In recently created computer languages like C++ however, the idea of having variables that are out of scope but still alive is heavily exploited and creates the principle distinguishing feature of C++:  the C++ class.  All members of a class, except static ones as we discussed on point 12, have as a scope the class domain, I mean everything in between classes brackets. Besides the local and global scopes, borrowed from C, C++ languages adds the class scope. Every member, even it is private, as we’ll soon see, it is visibile to the other members of the same class. Maybe the exemple below will be more helpful:

#include <iostream>
using namespace std;


class StatisticsPackage{
private:

    float aa[20];       /* aa scope start*/
    int length;             /* length scope start*/

public:

    float average(){

      float result = 0.0;   /* result scope start*/
      for(int i = 0; i < length; ++i)
      /* i scope start*/
      result += aa[i];
      return result/length;
      }

    void get_data(){

      length = 0;
      while(cin >> aa[length++]);
      --length;
      }

                       /* result and i scope end*/

}};    /* aa and length scope end*/

int main(){

    StatisticsPackage sp;
    /* aa and length lifetimes start */
    /* aa and length are outside their scope*/
    sp.get_data();
    float av = sp.average();
    /* av scope and lifetime start*/
    cout << av << endl;
    return 0;

}      /* av scope and lifetime end*/

16. As you probably may observed main function must return an integer and …  cannot be called by user code, only the system may call main. This generates another consequence: main cannot be recursive. In the C++ standard there is specified that 0 signifies a succesful execution and everything non zero represents a failure.

17. C++ has internal default linkage unlike C, which  has external default linkage: What do I mean by this ? In C you have to explicitly restrict a global variable’s scope to the current file by declaring it static, unless its scope is spread among all files of the program. In C++ unless you have to declare explicitly a global variable to be extern it has by default a file scope.

Advertisements

2 Responses to 50-must know things in C++…. PART3

  1. Luc says:

    Cool this article. But where are parts 1 and 2?
    Thanks.

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

%d bloggers like this: