Author

Topic: C++ Mentoring (Read 1220 times)

legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 21, 2013, 07:23:20 AM
#11
Now we'll extend the previous example to do something a bit more useful by introducing the vector container and the use of a couple of STL algorithms:

Code:
#include
#include
#include
#include

using namespace std;

void output_item( const string& s )
{
   cout << s << '\n';
}

int main( )
{
   string next;
   vector< string > items;

   while( cout << "\n> ", getline( cin, next ) )
   {
      if( next.empty( ) )
         continue;

      string::size_type pos = next.find( ' ' );
      string cmd( next.substr( 0, pos ) );

      string extra;
      if( pos != string::npos )
         extra = next.substr( pos + 1 );

      if( cmd == "quit" || cmd == "exit" )
         break;
      else if( cmd == "sort" )
         sort( items.begin( ), items.end( ) );
      else if( cmd == "list" )
         for_each( items.begin( ), items.end( ), output_item );
      else if( cmd == "add" )
      {
         if( !extra.empty( ) )
            items.push_back( extra );
      }
      else if( cmd == "clear" )
         items.clear( );
      else
         cout << "unknown command: " << next << endl;
   }
}

A vector is a "dynamic array" (so it can grow in size and be re-sized). New items can easily be added to this container via "push_back" (it does not implement a "push_front" as it cannot do this efficiently although you can use the "insert" function to achieve the same result).

The STL algos sort and for_each used in this sample are typical in that they use iterators rather than operating on the container itself (a fundamental difference between the STL and many other libraries that offer similar functionality).

So in under 50 lines we now have a console program that displays a prompt and will execute commands that apply to a dynamic array of items.
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 21, 2013, 01:11:20 AM
#10
Iterators and the STL

Continuing from canonical loops understanding the iterator concept (and note it is a concept not strictly a type or class) is essential to effective use of the STL (Standard Template Library) which is where std::string comes from (as well as very useful containers such as lists, vectors and maps).

An iterator can most simply be thought of as a "pointer" (and in fact pointers can be used with the STL), however, not all iterators are equal in their capabilities (so a pointer is actually considered to be the least restrictive type).

The most restrictive iterator types are Input Iterator and Output Iterator - they only allow movement in one direction and do not permit any sort of rewinding to occur. The easiest examples of this are the "std::cin" and "std::cout" objects (which are the C++ equivalents to C's stdin and stdout).

So a simple example of these objects in action is as follows:
Code:
#include
#include

using namespace std;

int main( )
{
   string next;
   while( cout << "\n> ", getline( cin, next ) )
   {
      if( next == "quit" || next == "exit" )
         break;
      cout << "unknown command: " << next << endl;
   }
}

This is a simple "command prompt" (output as a "> ") console program that accepts a line of input and then examines it as a "command" - if the command is "quit" or "exit" it finishes looping otherwise complains about the command being unknown.

You might find the look of the while loop to be a little strange as it includes a "comma" operator. This not so well known operator can be very useful for specific situations. In this case it is being used to output the "command prompt" before the STL function "getline" is being evaluated (the while loop condition is actually according to the return value of "getline").
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 19, 2013, 10:31:07 AM
#9
If you're writing an application in C++, of course it isn't valid C, because C is a subset of C++. But almost any C you can write is valid C++ (there are very few edge cases where one thing written in C means something else in C++, but this is quite rare). That's what I meant by C++ being a superset of C.

Unfortunately it isn't so simple - a good reference is here: http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

A quote from the first paragraph is as follows:

Quote
However, due to minor semantic differences, most non-trivial C programs will not compile as C++ code without modification—C++ is not a strict superset of C.

You will find if you try and compile a C program using g++ (or another C++ compiler) that you'll be likely to end up with many errors due to the subtle differences between the languages.

So - "it is possible to write C that will compile with a C++ compiler" but it would require you to write that C with C++ in mind (which would rarely be that case).

In point of case even fairly small algorithms such as SHA256 need to be changed from their C version to be able to be compiled in C++.

More importantly - if you are interesting in learning C++ I would *not* advise you to first learn C as that will just teach you a lot of "bad habits" that you will have to "unlearn".
legendary
Activity: 965
Merit: 1000
July 19, 2013, 09:08:51 AM
#8
Very cool thread! If this is ever done for Java, I'll contribute...
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 18, 2013, 08:09:48 AM
#7
Seeing as we are now focused on the differences between C and C++ then here is another good example of how the two languages differ - "canonical for loops":

Those who may be familiar with C would probably regard this to be a very "standard" for loop statement:
Code:
for( type i = begin; i < end; i++ )
   ; // do something here

If type is a just a primitive (such as an int) then of course the above would also equally apply to C++ *but* when the type is an iterator (the type most commonly used for algorithms in the standard library) then your for loop statement should look more like this one:
Code:
for( auto i = c.begin( ), e = c.end( ); i != e; ++i )
   ; // do something here

Q. What is the auto keyword doing here?
A. This is a relatively new (and clearly incompatible with C keyword) feature of C++ that lets you use the keyword "auto" instead of an explicit type (the compiler will work out the type for you saving you typing and making the code easier to read).

Q. Why create the second variable e (i.e. why not put i != c.end( ) instead of i != e)?
A. As end is a member function it can be expected to have some overhead when called so assuming that we expect the container to be unchanging during this loop it makes more sense (and is far more efficient) to only call the member function once.

Q. Why use != rather than A. Iterators are not integers and so are not expected to implement < operators that are useful for looping purposes.

Q. Why ++i rather than i++?
A. A post-increment ++ operator needs to create a temporary iterator object (the return value of the operator) whereas the pre-increment version does not. Thus ++i does not need to construct an object but i++ does (making it less efficient).
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 18, 2013, 06:14:05 AM
#6
I'm a very good C programmer, but I don't really like C++.

No language wars here please (partly why I noted the C post as being OT).

Also please note that the title of this topic is C++ Mentoring (not C/C++ Mentoring).

Actually, as C++ is almost a superset of C, his post really isn't off topic, is it?

If you are writing an application in C++ you must design it to be written in C++ (same for C). Any code that is C with say a couple of C++ bits thrown on top of it is not really C++ at all (although yes that much C support is kept in order to work with legacy libraries, etc.).

So if you are talking about the "keywords" of the language sure you could say that C++ is almost a superset of C but if you are talking about writing any serious amount of code then they are fundamentally different languages (in fact just look at the "hello world" example - apart from some language keywords there is nothing C in it at all).
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 18, 2013, 03:36:33 AM
#5
I want to learn  C programming.

Good for you - but as that is a very different language to C++ your post is OT here.

EDIT: I think I probably was a bit harsh with this reply (sorry) but another forum member has offered mentoring in C so hopefully that can help you out.
newbie
Activity: 24
Merit: 0
July 18, 2013, 03:31:34 AM
#4
I want to learn  C programming.
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
July 17, 2013, 08:33:27 AM
#3
Well I've had a few PM's but thought I might just start here with some very easy basics and in particular to show how C++ is *very different* to C.

Let's start with one of the simplest programs that any programmer would know and that is of course "hello world":

Code:
#include

using namespace std;

int main( )
{
   cout << "Hello World!" << endl;
}

Now for those who might remember how "hello world" looked in C you'll notice a few differences.

Firstly the "standard header" files in C++ do not have a file extension - so to #include iostream you use not . If you are wondering why - the reason is that unfortunately before C++ became "standardised" there were actually different extensions for header files being used by different vendors (such as .hpp or even .hxx instead of .h) so omitting the file extension became the "standard" to make things easier for compiler vendors and is actually nicer to read anyway (who cares what the file extension is when it is a "standard" header?).

The next thing that a C programmer might be confused by is the "using namespace std" statement. What on earth is that for? Well a problem that emerged from C and the development of libraries with their necessary #include's for usage was one of "name conflicts". So if I was using the "red_widget" library and the "blue_widget" library I could end up with a situation that I had a function called "destroy_widgets" in *each* library making it impossible to include both headers (and in particular to call one or the other function) without having to resort to nasty "pre-processor" tricks.

C++ solves this problem by allowing functions (as well as classes and even objects) to be placed in a "namespace" (they can be nested also) such that then provided that you place your "red_widget" library in a "red" namespace and your "blue_widget" library in a "blue" namespace you can avoid such ambiguities that cannot be avoided in C (and even call each explicitly via red::destroy_widgets and blue::destroy_widgets).

So without the "using namespace std" you would need to use std::cout and std::endl in the above code (as both "cout" and "endl" belong to the "std" namespace). Although not considered good practice to put a "using namespace" in a header file it is perfectly acceptable in a C++ source file and should never be a problem in particular to do so for the "std" namespace (being that of the "standard" library).

Again those more familiar with C might wonder about "cout", "endl" and what look like "bit shift" operators. In short "standard input", "standard output" and "standard error" are implemented as "iostream" objects in C++ called "cin", "cout" and "cerr" respectively. The << operator (as implemented by the underlying ostream class) is used to "write" to that stream (likewise a >> operator is used to "read" from an input stream). So already in such a short program we have to understand that C++ has "overloaded operators" something that might seem rather complex but it provides for some very powerful and neat features that make C++ quite different to most other languages around.

So now it makes sense that "cout << "Hello World!" is going to output "Hello World!" to "standard output" (or the "console" as we generally call it) but what does the output of "endl" do?

The answer to that is actually perhaps not exactly what you think (unless you know C++ well) as it actually does two things (most people I used to interview for C++ programming positions did not actually know this). I will leave this up to the reader(s) to investigate (and post an explanatory reply if you like).

Finally the one thing that might appear to be "missing" from our "main" is a return statement. The answer to that is that it is "optional" in C++ (when omitted the program will always return 0 to the OS).
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
May 12, 2013, 01:37:10 PM
#2
For those just interested in getting a Live OS to be able to compile Bitcoin please check out https://github.com/ciyam/safe (the version of Bitcoin included is not the latest but if people are interested in this then I'll fix that).

The distro also includes various tools for being able to sign tx's offline that do not require the offline PC to have a blockchain (and does the information transfer with QR codes to be 100% "air-gapped").
legendary
Activity: 1890
Merit: 1072
Ian Knowles - CIYAM Lead Developer
May 12, 2013, 12:16:36 PM
#1
I am considering to offer mentoring for those wishing to get into trying to understand the depths of the C++ programming language, its Standard Library and some of the most useful patterns and idioms.

Areas that I am thinking of covering (with detailed source code all being available in the CIYAM Open project) include the following:

Using Exceptions
Writing Portable Code
Type Safety and the advantages of Compilation
Dynamic Dispatch and Shared/Dynamic Libraries
Smart Pointers and the use of Scoped Objects
Recursive Descent and LL parsers
Multithreading Guidelines (especially in relation to doing DB coding)
Locking and Resource Management
Procedural, Object Oriented, Generic, Functional and Template Meta Programming
Sockets and basic Internet Protocols
Tracing and Debugging (without using a debugger)

I have over 15 years of professional C++ programming and have devised a complete web platform that is built in C++ (maybe the only one of its kind). I have done a lot of work in all of the above areas (which are actually all included in the CIYAM platform) and used to be fairly active in comp.lang.c++.moderated years ago (before around 2002).

Rates can be negotiated and if you are willing to do some small tasks for the CIYAM project then the mentoring could even be free of charge.

I know many of the members of this forum think that C++ is a "dinosaur" language but I think when it comes to security there is really nothing better and when it comes to understanding Bitcoin then it could be of real benefit to be able to better understand the source code.

(welcome to PM me for further dicussion)


Cheers,

Ian.
Jump to: