50 must-know things about C++ language – PART 1

The idea of this post is to help myself but other programmers as well, in understanding the underlying aspects of C++ language. Before diving into the intricacies of the language I will briefly say few words about how C and C++ languages evolved.

C language appeared in 1972 when Dennis Richie, working at Bell laboratories thought of creating a programming language free of any constraints that assembly language had. There were three main aspects that this new programming language had to take into consideration:

  • first and most important, to be independent of any hardware platform, it was very difficult to use assembly language because each time when you want to test your code on a different machine you’ll have to rewrite it
  • second thing was to be modular or to support what we use to call in C – function, to be able to insulate under a common label, sections of code and just to call corresponding label, this thing was possible in assembly (you can group in a single routine many instructions which are more frequently executed) also but the drawback was that you could not transmit parameters
  • and the last aspect is strongly related to the first one, the new language has to be more readable and easier to learn, its syntax must be easier to understand and closer to the human language.

C language being born tied its first years of existence to UNIX development. In 1973 Dennis Richie and Ken Thompson decided to rewrite UNIX operating system in C. Five years later they published a book, which remained by today a reference on C, called “The C programming language”, in order to make it known for the large public. In 1983 ANSI formed a committee and established a formal standard. In 1989 they released the first C language standard, which was adopted one year later by the International
Organization for Standardization, becoming well-known C90. This one was revised and enhanced much later, in 1999 , and a new C99 standard was defined, which is
still the one used nowadays.

In the meantime, Bjarne Stroustrup, doing his PhD project at AT&T Bell, faced some issues in analyzing UNIX kernel due to the lack of support of C language for large software development. He had the idea of enhancing the C language with support for classes borrowing some functionalities from Simula languages and so appeared “C with Classes” in 1979.  Four years later the mane of the language changed to C++ (the new language added more functionalities than support for classes, so “C with Classes” was no more appropriate). Language was standardized in 1998 and then, afterwards in 2003 a new corrected release appeared which is still the one used today. It is important to have in mind that after C++ was birth, C and C++ languages evolved in parallel, C continued to have its own specifications released.

Let’s get back to work and briefly explore some of the C++ essentials:

  1. C++ is not purely Object Oriented, or put it in other words, you can write C++ code without using classes and this will compile OK.  The C++ language contains some language extensions over C, that make object oriented programming and generic programming more convenient. C++ does not force object oriented design anywhere, it is not like Java, even if Java also is not purely object oriented, but at least it cannot be global variables, everything must be encapsulated within a class. Any C++ compiler would compile C code, this is as a consequence of C++ being a superset of C language, also C code can be inserted into C++ programs.
  2. Dynamic memory allocation and I/O are different from C . In C I/O operations are done through printf and scanf functions defined in stdio.h header in standard library, in C++ this is done through streams defined in C++ standard library, in C dynamic memory allocation is done through malloc, realloc, calloc and free functions, in C++ this is accomplished through new and delete operators Besides the Object Oriented features as Classes, Encapsulation, Inheritance and Polymorphism, that enhanced C++ and cannot be found in C syntax there are those two important aspects which are done differently in C++ with respect to C.
  3. Standard C++ library keeps old headers from C library but adds new ones . Standard C++ library, besides the new libraries, inherits also the old headers from Standard C library, newly inherited headers have a leading ‘c’ letter: if in C standard library name was “setjmp.h”, in Standard C++ library will become “csetjmp.h”
  4. Operator redefinition In C++ you can “overload operators”, you can give them new definitions apart from those already existing in the standard. The most common case of operator overloading is the one of >> and << operators, which are overloaded in istream, respectively ostream classes. cin and cout are also objects of type istream and ostream.
  5. C++ introduces the notion of String, on top of the one of character arrays inherited from C, which removes many of the problems introduced by C-style strings by putting the onus of memory ownership on the string class rather than on the programmer. Those are defined in the string header and one snapshot example of a “string programming” in C++ may look like this:#include <string>
    using namespace std;
    int main() {
    String a = "Hello";
    String b = "you!";
    String c;
    c = a + b;
    cout << c; // this will print Hello you!
    if (c == "Hello you!") a=b;
    return 0;
    }

    you may observe that some common operators as “+” (addition), “==” (equality), “=” (assignment) are already overloaded to replace some of the functionalities of old C style functions like strcmp, strcat or strcpy.

I will stop for the moment here, taking into account some friends’ advices and some common blogging tips, that posts need not to be overwhelmingly long and I will come back sooner with another set of “must know C++ features”. So …. to be continued … with PART2 .

Advertisements

One Response to 50 must-know things about C++ language – PART 1

  1. vasiauvi says:

    Iţm glad that you toked in to consideration my advice regarding the length of a blog post :D.

    Keep on going with this good work!
    See you at work!

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: