Cpp beginnings

Edozié Izegbu
Learning to code
Published in
7 min readNov 10, 2015

Square Zero

An image from Ghost in the Shell the movie (1995) which contains REAL C code , its not C++ but its pretty close!

I think I should start talking about with some Statically typed languages which I believe are much more valuable to learn first than dynamically typed languages like Ruby, Python and JS for a number of reasons that I shall enumerate here.

  1. Statically Typed languages force you and your compiler to know what you’re going to be putting into your named variable. This reduces the chance of making mistakes , as it will immediately tell you “hey thats a string when thats supposed to be an integer”
  2. The transition from learning a Statically typed language to learning a Dynamically typed language is statistacally easier than vice versa. No brainer here, the more verbose a language is, the more you truly understand what is going on, Class Inheritance and, importing Dependencies all are necessary for a simple “Hello World” program.
  3. Statically Typed languages check your type before run time during compile time , where as Dynamically typed languages, check during run time
  4. Program Execution can sped up in statically typed languages by omitting run time checks
  5. And many more

To start off we are going to create a simple Hello World program, which I know is infancy stages for programming but this is really aimed at breaking down the languages features and how they are applied.

Don’t worry its not this verbose.

Types

I think the next thing to learn are Types these are the things that you are defining your variables to be in a Statically typed language. Im going to cover a few types here.

To define a type they define a data structure and various operations on that data structure , there are some which are built into the core language that I will explain right now and others that are defined outside one of which I will highlight at the end.

Please remember that these Types are all case sensitive so don’t go putting String or Int like you are typing a sentence out in English.

string : “I am a bunch of words surrounded by quotation marks”

int : 2 , 4 , 3 , 200 . [Numbers from x to y]

float : 1.23 , 44.434 , 9934.2942354 [Numbers which basically have decimal points or the total number takes 32 bits/4 bytes space]

double: 1.23243, 435.242342, 95432.3242 [Numbers which use average 32–64 bits /4–8 bytes space]

struct: Vector, Person , Car [Objects which have variables constituents of the preceding types .. don’t get what I mean well you will soon]

Floating points encompass a float and a double where the variable number can support a variable amount of numbers after the decimal point. Ie the decimal point can ‘float’ around. Floating points follow the Institute of Electrical and Electronic Engineers (IEEE) 754 Binary Format. That is a float is 4 bytes a double is 8 bytes and a long double is 8, 12 or 16 bytes. If you don’t know what a byte or a bit is I will be writing an article fairly soon on how these operate its important you know.

Firstly we have to understand that C++ is unlike any dynamically typed language as you cannot simply write print(“string”) you have to explicitly inherit the most basic libraries that you take for granted in Ruby or Python. So to start off we are going to include the library <iostream> and <string> which is written in C++ as

#include <library_name>

so at the top I am going to put #include <iostream> ; on top of a cpp file.

<iostrean> Is a library that allows for manipulation of input and output streams for your program.

If you are following along and want to write the code too read here , Make a file now by entering into the terminal : mkdir cprac ; cd cprac ; touch hello_world.cpp Now I am using an Atom editor with a handy package called “script”. This lets me compile and run my scripts of most modern languages within the Editor. If you don’t have this set up go and you are on a Mac use Xcode (I am assuming you are using version 6+) and set up a new project , that is a Command Line tool, and you’ll find that there is a Hello World program there ready for you to make it , if you are on Windows go onto Visual Studio or Notepad ++ and do it the windows way, I haven’t used VS in such a long time so you can figure out how to start a basic command line project on VS yourself by googling “Starting making C++ program in VS” — replace VS with your chosen IDE (Internal Development Environment) to find out how to do it there.

Now you may notice that I put #include <iostream> ; ←Semicolon

The semi colon there is hugely important in C++ and is not so much in many modern dynamic languages like ruby or python. Make sure you note that semicolons are after every expression, an expression are a combined set of procedures / data that returns a value . So here #include <iostream> is returning a value (The library iostream itself is a collection of values, so that the compiler knows to use methods and names defined within iostream )

So dont forget to put your semicolons after your expressions , if you are puzzled as to what an expression is, ask yourself , “Is this returning a value?” if your answer is Maybe or Yes, put a semi-colon afterwards. For a more detailed explanation as to where to place semi-colons in cpp look here.

Now that we have written our first damm line of code we are about to write our second and this is very important because we are going to write our main function. Now functions in statically typed languages also are made to return particular types of values, we will make our main return an integer because main functions can only integers.

Functions

typename functionname() {

Function content

}

The main function is a non-member function in the global namespace that runs everything in the program.

Non member basically means that it is not a member of any class , if it were you can call things like “this” which refers to the superior class.

So our function will therefore be called

int main() {

//some stuff

}

Notice how I have put //some stuff , double slash is how comments are done in c++ so get used to them. Now we have to fill that in, but how do we print stuff out on to the screen? Yes , you use the iostream library, which inherits from std. Firstly you put

std::cout //this is a qualified name , which uses :: a Scope operator. To the left a stream out object that you will need to insert your objects into

<< // insertion operator which is a left-associative operator basically the tool for placing things into your stream. Meaning

“Hello World” // This is the string object that we are going to place into the cout stream

<< // You can chain multiple insertion operators to print out things of different types.

std::endl; // This is a manipulator, the command that terminates your output stream and places a new line afterwards

the whole body of main looks like this.

std::cout << “Hello World” << std::endl;

To clarify exactly what is going on, the first insertion operator is writing the string literal object “Hello World” onto the stream and the result is the left operand of the first insertion operator [std::cout]. std::cout has a special type called std::ostream , the second insertion operator then takes this std::ostream and has the manipulator std::endl do something to it. The key property of manipulators is that it does something other than just writing on the stream. In std::endl ‘s case it is to end the current line of output and start a new one.

Another keyword you should know is a variable’s scope. This is the part of a program where a name is available, the first scope that we use is a namespace (a collection of related names) named std this is so that it can avoid conflicts with names that we might define ourselves. When we use a name from the standard library we must specify that the name we want from that namespace is indeed from that library with the scope operator. Eg “std::cout” means cout defined in a namespace called std.

return 0; //we return 0 because as main is supposed to return an integer , and by convention 0 indicates success and any non-zero number indicates failure.

Here it is. In all its glory. Well if you enjoyed this or learned something new or both please recommend this and don’t hesitate to follow as Im always posting something on my blog on programming.

--

--