Intro101

This fake page will go briefly over the language C++, don't take it to heart, it's coming (mostly) straight from my head. Although it hopefully should still contain some facts!

There exist many different programming languages, typically all of them are built with a main goal in mind. Originally we had binary (101010101's) which obviously seems incredibly tedious to work in, so assembly was made. Assembly was still tedious but introduced mnemonics which made the code far more readable. A struggle seemed to be balancing the limited number of registers and keeping track of them. Example of some assembly:

mov   eax,x ;move x into eax register add   eax,5 ;add 5 to eax register mov   ebx,3 ;move 3 into ebx register imul  ebx   ;multiply eax by ebx

The next major breakthrough (I think) was C, which was created in ~1970. Considered to be a low level language like assembly and machine code, however far easier to work with and looks similar to most languages you've probably ever seen. To me C is probably the most beautiful language created and is still widely used today for micro devices as well as high performance code.

C is a procedural language meaning it runs from the top to the bottom, there are no classes instead use of structs to group data. Although by definition it is actually a high level language, it is considered to be low as it grants access low level areas. Example of a C struct:

struct rectangle {     int width;     int height; }

Now onto our beloved C++, based off C it was extended to include classes, and over the years been extended to include a lot of fancy features. C++ offers a pretty good amount of portability, though no where near the likes of java as it is still a compiled language. Let us now delv into the basics of a great language like C++.

Sir Change-a-lot

Hello again old friend.

The general consensus is that C++ is a strictly typed language roughly meaning that the compiler is quite strict and will throw up errors when types don't match etc. There are several basic type of types in C++, some of these are int(2032), char ('a') and bool (true/false). They each use a set amount of memory that is allocated on the stack when the program starts. Most of programming is assigning variables and doing things to them to create change.

int number = 5; number = number + 5; //number now is equal to 10 *this is a comment*

Underlying everything is binary, so a basic understanding is essential. Typically variables are given a space in memory allocating x amount of bytes. 1-byte is typically 8-bits ie (00001011 == 11)

Integer (int) is a number that is typically 4-bytes long (32-bits)
Boolean (bool) holds a true or false value (1 or 0), where 0 is false and everything else is true.
Character (char) holds a single character ie ('a') and is held in memory as the ascii value (97) which would look like '1100001'.

Hello World

As C++ is a compiled language, it must be compiled into an executable in order to be ran. There are several compilers out there such as intel, microsoft and clang. Each optimises and performs its duties slightly differently however the program will run the same on all of them for most scenarios, the major difference being how fast it may do certain tasks.

Unlike other languages like C#, there is quite a bit of effort and understanding to making a windowed application and therefore until a firm grasp is achieved, console applications are the way to go.

Every C++ application has the same entry point which is 'main()'. Most of the time it is of type int, however void may be used. And so the simplest program one can create is simply having an empty main() method, which does nothing. It would look like:

int main() { }

Like most languages, there already exist a lot of code out there and so taking advantage of true and tested code saves time and heart ache. These bits of code are typically accessed through libraries or header files. Including one that lets use write to console could be helpful. Two of these are and "stdio.h", where the io in each is for input output, the std is for standard and the stream is for stream.

The two above functions define different methods that write to console, the two main ones being printf and cout. There are differences between the two but for now using either doesn't matter.

#include "stdio.h" int main(){     printf("Hello World!"); //this is a comment }

Above we include "stdio.h" to gain access to the printf method, then we call printf to write to console!. However, below we use the functions instead. The scary looking std:: are just identifying the namespace. Different functions can be defined in multiple namespaces to avoid conflict of their names.

Method Maddness

Now that we have used someone elses functions, lets create one of our own. A method must be declared before it is called, otherwise it will not know what you are asking. It is also made up of a signature, where each signature must be unique, it is defined by its name plus its arguements (the parts in the parentheses)

#include iostream using namespace std; //removed the need to use std:: for cout
int calcSum(int, int); //forward declare our method
int main(){     int sum; //declare an int for our sum to go     sum = calcSum(5, 4); //5+4=9     cout << sum; //print our sum } //end of main function
//return-type method-name(int, int) int calcSum(int lhs, int rhs) {     return (lhs+rh); //return an int of lhs+rhs }

Classy Little Things

So far we have been used several key words. Key words are words with C++ that do something, they cannot be used for anything else. Some of these are:

  • bool, int, char
  • struct
  • class
  • template
  • return

Both structs and classes are a way to group data types in useful ways in order to create objects among other things.