Good ol' C++

in #programming7 years ago (edited)

Hi there Steemians,
Gioia has finally left the keyboard free, so I can start the adventure in this new world with something richer than a picture of my adorable (and fluffy) kitten.

I have decided to start with a topic that concerns my job, that is C++11.

First of all, I would like to make someone smile; I live in Italy, where it is not uncommon to find business executives and project manager who are convinced that upgrade and training course are something absolutely not indispensable.
The result is that for the vast majority of people, C++ means C++98, the one learned at school by them -or more likely- by their children.

So when I talk about C++11 I often get strange glances, sometimes even corrections: C++ was born in 1998, actually.

If this happens also to you, you know how frustrating it is, and I'm sure you stumbled upon sentences like the following ones, too:

- C++? But nowadays nobody uses it anymore!
- Yeah, well, but it's a 20 years old language!
- It's uncomfortable to use, it does not help you at all!

Actually, C++ has changed significantly in recent years, and today it is a much more user friendly language; the way it handles objects is also much more similar to other more "trendy" languages - far more than its own detractors can believe.

So with this article, I would like to start showing how C++ has changed; obviously I'll start with some of the most simple and evident evolutions, and then proceed towards most complex (and powerful) ones if the matter raises enough interest.


The Star Trek Races.

Ok, let's say we're in 1998 and we want to write a silly, small program in C++ that prints on console the four main races of the Star Trek Classic Series.

The program we're talking about could be something very similar to the following one:


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char **argv)
{
  vector<string> races;

  races.push_back("Humans");
  races.push_back("Vulcans");
  races.push_back("Klingons");
  races.push_back("Romulans");

  for (vector<string>::const_iterator it=races.begin(); it!=races.end(); ++it)
    cout << *it << endl;

  return 0;
}

I already see all those guys who consider C++ an old unpractical language pointing their finger on the difficult initializations and the uncomfortable statements needed for a simple loop.

But wait, it was 1998. Today, waiting for C++17, we have C++11.
What happens if we write the same program using C++ as it is now?.
Here it is:


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char **argv)
{
  auto races = { "Humans", "Vulcans", "Klingons", "Romulans" };

  for (auto race: races)
    cout << race << endl;

  return 0;
}

Yes, it's always C++. It's modern C++11, not the old C++ of 20 years ago.

With a small, silly sample I showed you 3 important changes C++11 brought:

- Automatic Type Deduction
- Initializer List
- New for (foreach) construct

Automatic Type Deduction

Like many modern and fashionable languages, C++ can now determine the type of a variable, provided there is an initializer:


auto i=5;         // 5 is an int value, so i will have int type
auto c='a';       // char
auto s="string";  // string
auto d=0.5;       // double

Automatic type deduction is mainly born to handle automatically generated types in templates, but it is still dramatically useful for "verbose" types likes iterators:


vector<string>::const_iterator it=v.begin();

can declared simply with:


auto it=v.begin();

Initializer List

Since 2003, C++ allowed initializer lists for basic types:


int buff[4] = {1, 2, 3, 4};  // Initializer list

C++ extents the support to all relevant STL containers, so it is no longer necessary a long (and annoying) list of push_back in order to init a vector:


// Initializer list for vector
vector<string> races = {"Humans","Vulcans","Klingons","Romulans"};  

Yes, the first line of our C++11 program uses an initializer list and type deduction ;-)

Foreach construct

C++03 already included lots of new libraries (regex management, tuples, unordered_map, set etc); all of them are now officially part of C++11, and other new algorithm have been added. One is the for(foreach) construct, which consists of a loop on a data container.

In order to work, the container must define .begin() and .end()


for (int i: v) {  // Extract values from v into i
  cout << i;
}

Again yes, we used a foreach with type deduction also on the second statement of our C++11 program ;-)

And talking about our program, looking at it we discover that it has a number of lines and complexity not so different from a Python or C# program, don't you think?

Not bad for an old unusable language!

If you've found the matter interesting let me know, I'll be happy to deepen the subject ;-)

Thank you.

Sort:  

Congratulations @fultonjoy! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:

SteemFest3 and SteemitBoard - Meet the Steemians Contest

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @fultonjoy! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

SteemFest Meet The Stemians Contest - The mysterious rule revealed
Vote for @Steemitboard as a witness to get one more award and increased upvotes!