I wonder if there is a computer program to teach C++

Discussion of chess software programming and technical issues.

Moderator: Ras

Uri Blass
Posts: 10890
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

I wonder if there is a computer program to teach C++

Post by Uri Blass »

I think that a computer program to teach C++ can be more efficient than a book to teach the language.

The advantage of a program is that you do not have something static
and the program can ask the user questions to see if the user understand or remember and simply go back to repeat previous explanations if the user does not give a correct reply.

Today my knowledge of the C++ language is not enough to understand code like the stockfish's code.

I have a book but I did not read most of it and I prefer learning from a computer program in case that it is possible.

Uri
MattieShoes
Posts: 718
Joined: Fri Mar 20, 2009 8:59 pm

Re: I wonder if there is a computer program to teach C++

Post by MattieShoes »

I learn a language by looking at source code and writing source code. I don't think there's much of a shortcut to be had...
User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 10:05 pm
Location: Italy
Full name: Stefano Gemma

Re: I wonder if there is a computer program to teach C++

Post by stegemma »

I think that a good C++ forum could be the best way to help you learning C++ language. I would proceed that way:

- inspect the source code you are studying until you find something that it is hard to understand
- post in the forum (or maybe even here, if it is not considered off-topic) a question about what the think that blocked you would means
- discuss about the answers givene and try to find some sample or more information about that

Step by step, you can learn all what you need to understand the C++ program that you're interested to.
Dann Corbit
Posts: 12792
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: I wonder if there is a computer program to teach C++

Post by Dann Corbit »

Uri Blass wrote:I think that a computer program to teach C++ can be more efficient than a book to teach the language.

The advantage of a program is that you do not have something static
and the program can ask the user questions to see if the user understand or remember and simply go back to repeat previous explanations if the user does not give a correct reply.

Today my knowledge of the C++ language is not enough to understand code like the stockfish's code.

I have a book but I did not read most of it and I prefer learning from a computer program in case that it is possible.

Uri
The best way to learn is to take a class from a good teacher who is skilled at the language.

If you read Stroustrup's book, that is a good start. Then try the ideas yourself.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: I wonder if there is a computer program to teach C++

Post by mcostalba »

Uri Blass wrote: Today my knowledge of the C++ language is not enough to understand code like the stockfish's code.
To understand Stockfish's code you just need to know about 10% of the whole C++

Apart from ver very few exceptions I almost use very basic concepts and we try to keep it as simple as possible. It is a choice.

If there is some point or some function that is not clear to you , please drop me an email, I will be glad to help you. If some parts of the code are not clear I will try to rewrite in a better way. It is important to have feedback from peers, because for me it is too easy to overlook some not clear part .

And for us to develop easy to read code is of paramount importance, more then ELO gains.

Marco
Uri Blass
Posts: 10890
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: I wonder if there is a computer program to teach C++

Post by Uri Blass »

mcostalba wrote:
Uri Blass wrote: Today my knowledge of the C++ language is not enough to understand code like the stockfish's code.
To understand Stockfish's code you just need to know about 10% of the whole C++

Apart from ver very few exceptions I almost use very basic concepts and we try to keep it as simple as possible. It is a choice.

If there is some point or some function that is not clear to you , please drop me an email, I will be glad to help you. If some parts of the code are not clear I will try to rewrite in a better way. It is important to have feedback from peers, because for me it is too easy to overlook some not clear part .

And for us to develop easy to read code is of paramount importance, more then ELO gains.

Marco
Thanks but I do not like to ask basic questions that are not because of code that is not clear to programmers but because of the fact that I do not know the language.

There are probably basic things that I do not know about C++
For example I see that stockfish is often using the word operator and I simply do not know how this word is used in the language(I only understand that it is basically something that alllow me to use functions in in a different way and instead of writing something like
c=plus(a,b); that I can write c=a+b;(assuming that a,b are some structures like complex numbers so special function is needed to add them).

Maybe it is going to be good if I read and undersatd chapters 8 and 9 of the following site

http://www.learncpp.com/
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: I wonder if there is a computer program to teach C++

Post by mcostalba »

Uri Blass wrote: There are probably basic things that I do not know about C++
For example I see that stockfish is often using the word operator and I simply do not know how this word is used in the language(I only understand that it is basically something that alllow me to use functions in in a different way and instead of writing something like
c=plus(a,b); that I can write c=a+b;(assuming that a,b are some structures like complex numbers so special function is needed to add them).

Yes, as examle in SF is defined the following comparison '<' operator:

Code: Select all

bool operator<(const MoveStack& f, const MoveStack& s) { return s.score < f.score; }
Where MoveStack is a simple struct that contains a move togheter with its score:

Code: Select all

struct MoveStack {
  Move move;
  int score;
};
After we generate and score the moves we want to sort them. To sort the moves we use std::sort() that is a function of the standard library. This function can be used to sort in ascending order _any_ kind of stuff and inside are compared, moved and swapped the actual objects, not their pointers as in qsort(), that's the main reason it is known faster. But how can a function that operates directly on objects be so general so that can be used with _any_ type ?

The "secret" is stunning simple: std::sort() is written in the most natural form, it has statements of the kind

Code: Select all

if (firstObj < secondObj) { .... do something... }
That is used to check if first object is lesser then second, but the exactly definition of that 'lesser then ' operator is _not_ inside std::sort() but is lying elsewhere. And, as you can imagine, the exact meaning of '<' is given by the programmer that writes the operator<() function and so exactly specifies what std::sort(), or more precisely the C++ compiler should interpret the (a < b) comparison when 'a' and 'b' are objects of type MoveStack.

This is a very powerful and elegant general solution used very often: the standard library contains a collection of algorithms not tied to a specific type, but can be used with any type. The programmer when uses an STL algorithm writes also the specific comparison operators that the C++ compiler silently will substitute inside the STL function during compilation. So we have completely disjointed the algorithm (inside STL) from the actual data it will going to operate (defined by the programmer).


I have said that std::sort() sorts in ascending order, but we need a sorting in descending order so that highest scored moves are the first. Here the trick is very simple as you can see from the comment to the operator<():

Code: Select all

// Note that operator< is set up such that sorting will be in descending order
inline bool operator<(const MoveStack& f, const MoveStack& s) { return s.score < f.score; }
Uri Blass wrote: Maybe it is going to be good if I read and understand chapters 8 and 9 of the following site

http://www.learncpp.com/
A good and easy to read on-line book that can ba also free downloaded as pdf is Thinking in C++

http://www.mindview.net/Books/TICPP/Thi ... CPP2e.html