Learn C++ in 24 Hours
User Rating: / 2
PoorBest 
Written by Bryce Ringwood   

Part 1

For many people reading this, there can be only one reason for learning C++. In a word "Wndows". It is feasible to program in C++  after as little as 24 hours of study. 
 
In 1992, when Microsoft introduced version 7.0 of their compiler, they also introduced C++ and "Microsoft Foundation Classes". Although it wasn't a great experience (You had to compile your Windows programs in DOS), they were obviously very keen that you should understand their C++ ideas and become familiar with the product. To this end, they provided a slim volume entitled "Getting Started". There is no doubt that it covered many of the salient points and could be assimilated in 24 (Non consecutive!) hours or less. I confess that I still use this little manual for reference. The compiler was timed to be available for use with the then new 3.1 version of Windows. 
 
The booklet was divided into three sections:
 
 
1. 'C++' as an "improved" 'C'
2. Classes 
3. Object Oriented design.
 
 
The first section introduced language enhancements, such as inline functions, const operator, references, and so on. 
 
The section on classes described how classes can be thought of as definitions of user-defined types. For example, C++ does not (natively) have a matrix type. But if you are feeling heroic, you can write a matrix class and then use it to perform matrix operations, such as addition, multiplication etc. using the normal arithmetic operators.
 
The final section described all the things that my colleagues at work had difficulty with. It described how classes could inherit characteristics from their parents, the use of pure virtual functions, operator overloading, and so on. More importantly, perhaps, it described how the concepts could be applied to modelling an application.
 
The book did not mention templates, nor did it mention the "Standard Template Library". It was Microsoft-specific - so that many of the language features were  missing. This is not surprising, since it was a reference on Microsoft's first attempt at a C++ compiler.
 
I am told that the Arduino 'Wiring' language is written in C++. At any rate, it seems to be a very minimal version of C++. I believe there is a proper C++ compiler for the Arduino that you can use via the Eclipse IDE. PIC microcontrollers have a minimal 'C' compiler. This leaves us with C++ on Windows, and on the Raspberry Pi (and other Linux machines). 
 
Most of the time, I plan on using 'C' - but the best laid plans gang all aglay. The most recent mishap being a program that needed to carry out arithmetic on 22 digit integers. Since this is bigger than a "long long" data type - I had to re-use my own "bigint" class. (Don't get excited, just about all you can do is add and multiply arbitrarily large integers using it.) The result: a short, messy, impromptu program in C++. All it really does is read something, convert from hexadecimal and write the results to a database. One of the results was a large hex number that needed to be converted to a bigint, and bigint has the conversion already written.
 
So - What's the quickest way to learn ? Well - I think just go ahead with your programming project and learn as you go, which is exactly what I'm doing with my current PHP5 project. Nowadays there are tons of user-groups and forums where just about any problem can be sorted out. Probably because of my age, I find that I learn more easily from books than by other means. 
 
Although Bjarne Stroustrup's "The C++ Programming Language" is available on the web, It is better to purchase the latest edition from a book store, even though the price is almost R2000-00. The free edition is very out of date now. 
 
The trouble is, all these books fail to address some people's biggest fears.

Part 2 Getting Started Chapter 0

Statements
 
Statements are lines in your program that tell the computer to do something. For example :
result = a + b;
Note that there is a semicolon at the end of every statement - you are allowed to have more than one statement to a line:
result = a + b; c = result / 5.0;
Some programming languages - BASIC and FORTRAN, for example do not have a line statement ending character.
The other thing that looks daunting are the braces {} everywhere. Braces are used to group statements together. For example:
if (something is true)
     { 
          a = b + c;
          d = f + g;
     }
etc:
 
At first, you will get lots of compiler errors because you forgot to end a statement with a ";". Nobody ever died from a compiler error. This particular syntax consisting of related statements inside braces allowed 'C' to be a "structured programming language". Structured programming was ushered in by Pascal and was said to be far superior to the tangled logic of FORTRAN and BASIC. FORTRAN fought back with Fortran 77. BASIC took over the world with Microsoft's "Visual Basic", which almost anyone could use to write Windows programs. Not everyone agrees with the tenets of structured programming, but it must be admitted that it provides much better program flow - you can now read a program by starting at the top of the page and reading down until you get to the end. If you are used to Pascal - think of the opening brace of C/C++ '{' as BEGIN and the closing one '}' as END.
 
Data Types
 
In real life, nobody worries too much about distinguishing between rational, irrational and integer numbers. Computer languages, such as BASIC don't care very much either. First of all - what is the difference anyway? Simply put, Integers are all the whole numbers, including the negative ones. Examples being -5, 100, 17, 3 etc. Rational numbers are numbers that can be expressed as fractions e.g. 22/7 , 113/355, 4/3 and so on. Irrational numbers can't be expressed as fractions, examples being "pi" (3.141592....), "e" (2.71828...). Although the C++ compiler doesn't distinguish between rational and irrational numbers, it does distinguish between integer and floating point numbers and between character and other data types. Alas, all the built-in data types must be learned. The basic types are 'char','int','bool','long','float','double' and 'void'. 
 
Why does it make the distinction? The idea being to keep programmers out of trouble. For example:
 
int main {
     char animal[]="horse";
     float mane = 4.5;
     int result;
     result = mane / animal;
     ... ... ...     
}
 
Obviously that's silly - nobody would ever do that. Less obviously:
 
int main {
     int i=3;
     float mane = 4.5;
     int result ;
     result = mane / i;
     ... ... ...
     return 0;     
}
This will cause the compiler to issue a warning message . "result" will be the nearest integer - i.e. "1". Maybe not what was intended.
 
What about this nonsense:
 
int main()
{
    char a = '0';
    char b = '3';
    int c = b - a;
    cout << c;
    return 0; 
}
 
It compiles without error and runs giving the answer 3.  The 'char data type is 1 byte wide for a single character. The '0' and '3' get stored as their equivalent pattern of bits. I think this is confusing (but very useful). Perhaps it would have been less confusing if the type had been called "byte"  rather than "char". C++ will always attempt a "reasonable" type conversion if you mix types in an expression. Data typing helps with the organisation of a computer program and as your programs grow in size, you will begin to see the advantages. In a large program it is quite easy to lose track of a variable or mis-spell it and get an unwanted result. Also, think of the people who will have to maintain your program. Note that some modern languages are typeless. Javascript lets you declare everything as a "var". The Javascript interpreter contained inside the browser automatically figures out what the variable type happens to be. There seems to be some debate about whether data typing is a good idea. I leave it to you to decide. 
 
Parting shot - C++ 11 allows this:
 
int main()
{
       
       auto a = '0';
       auto b = '3';
       auto c = b - a;
       cout << c;
       return 0;
}
 
Yes - the data type is inferred from its initial value just like Javascript.   It doesn't work directly with the gcc compiler at present, and you still have to declare your variables before using them.
                                                  
This covers the initial "stumbling blocks" for some people. 23 Hours to go ...
 
 
Joomla template by a4joomla