Introduction to programming – C++

Once upon a time, there was an Emperor ruling the Earth. The powerful King would ensure safety and welfare of his people in return for wealth or service, and the commoners would grow food in their farms or cast swords and utensils out of iron or paint and sing and dance to entertain. And thus life would move on- in small, isolated pockets for most of the time.
Activities filled their lives; gaining information about various things, creating various things and to count various things.
But, one fine day, the King was consumed by desire for not only to defend and protect but also to conquer and become ever more powerful than before.

(memecenter)

The King’s empire grew to tremendous sizes and reached to many, many kingdoms of the world. The King faced a crisis now. How would he count the gold in his kingdom now, how many minds will be required to keep track of give and take? It would take him months to figure out just a day’s work in his empire. He needed more worthy, fast and moreover accurate people to manage this giant problem of computation, to progress. He alone was just not enough.

And then, the scholars of his kingdom invented a way to do arithmetic. Counting was tedious and tiresome and lacked the required ‘Swag’ that was required to keep track of the gold. It was easier to make a person count a part , then another count another part and add them together. Even easier to make equal parts , count the no. of parts and multiply. This was exceedingly sexy and led to a revolution! The mathematical revolution!

Well , those things also broke down as time passed . Not because of efficiency , mind you , the algorithms i.e steps were extremely efficient. However , we weren’t fast enough to use them. This necessity , gave birth to computers. Devices that would count huge sums in a matter of seconds… well just not at the time of the king , but give or take a few thousand years later

Step inside the time machine and let’s return to our time.. we now have literally a billion times the information the king had to process. And we do it in seconds on our laptops and mobile phones.But , what the hell is really going on down there?

Well , we did invent the computer , but that bloody thing was brainless , so it couldn’t understand what to do when told to “Count all my gold or I’ll have your head!” in English.It did however, understand two simple things… on and off.. pulses high and low…0’s and 1’s.

This is appropriately called the ‘Machine language’. And , it tells the machine what we want and how to do it.

If we wanted to talk to the computer in this way , we could , but it’s a pain in the behind.

If you want to simply say “Hello World”, you will have to type to them

“01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100”

And that’s no fun.

 

 

(deviantart)

But well , things were made easier.. a lot easier when ‘Higher languages’ were developed. which included a library i.e something that already has 0’s and 1’s inside it , and other basic syntax that was included in the library.

So , if you wanted a computer to add two numbers up , in c++ , the code would look somewhat like this

int a, b, sum;
cin >> a;
cin >> b;
sum = a + b;
cout << sum << endl;

And that’s done.Even if you cannot really understand the code above, you should be able to appreciate how much easier it will be to program in the C++ language as opposed to machine level language

Let’s head towards this charming lady named ‘compiler’. She acts as the Agony Aunt for us romantics as we try to flirt with these shrewd , calculating, data-churning, heart-breaking machines! She has two really hot sisters named interpreter and assembler. More on these two later, so roll your drooling tongues in and sit tight.

Imagine you have a date with a person who doesn’t speak your language and you don’t speak theirs… well , you hired her.. the compiler , to translate what each other are saying. She speaks both the languages fluently.

 

 

BUT , WHAT DOES A PROGRAM LOOK LIKE?

That’s easy. It’ll have blank spaces , comments and tokens..tokens are those things that the words the compiler can translate , blank spaces are… well blank spaces, and comments are those things you mumble to yourself so you can remember what you said.

Tokens consist of keywords ,  constants and variable (for you to assign something to ) ,and identifiers..used to identify variables and stuff.

Aand lastly operators viz. +(addition), -(subtraction ), *(multiplication), /(division).

The most basic command you can write is the famous “Hello , world” program.

which goes something like

1. // my first program in C++

2. #include <iostream.h>

3. int main()

4. {

5. cout << “Hello, World!” ;
6. return 0;
7. }

Output: Hello World!

Now , lets scrutinize the mumbo jumbo that we have here

the line 1. is a comment …just something you are saying to yourself

the second line , i.e. ‘2.’ is the pre-processor directive , or the library , or as I like to call it , the binary dictionary. It basically tells which words to use… more precisely , it allows a specific set of words to be translated.

The line 3. initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name “main” to the group of code statements that follow. Functions will be discussed in detail in a later, but essentially, their definition is introduced with a succession of a type ‘int’, a name ‘main’ and a pair of parentheses ‘()’, optionally including parameters(more on these dudes later).

The function named main is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function regardless of where the function is actually located within the code.

The { on line 4. and the } on line 6. are the things that are included in the ‘main’ function.

And , line 5. ‘cout << “Hello , world”;

Is you telling the machine to speak , or give an output (that’s where the ‘cout’ comes in ) of “Hello , world”.

And then you’re done! You just forced your computer into saying “Hello , world” . You finally have the upper hand…. YOU ARE NOW IN COMMAND! YOU CAN FINALLY GET SOMEONE TO SAY “I LOVE YOU”!….provided that someone is a heartless ,cold and calculating machine.*cough cough*

Now , to help us count the annoyingly large stacks of money you have , we need to assign variable to it. Which is pretty simple.. all you have to do , is say “Hey , see this shiz here…It’s ‘a’ .. an integer imma use later…and this shiz is b ,another integer ”

However that is written like this

‘ int a , b; ‘

And if you wanna , say add these two up , you need to introduce a third variable called ‘sum’

so just write down

‘int a,b,sum; ‘

(Oh , bt-dubs .. the ‘;’ and ‘{‘s’ and ‘<<‘s are annoying but important , since without them , the computer won’t understand crap..don’t blame me , blame the people who made c++ a case sensitive language…stupid butts )

And , well , you gotta ask computer to add them up….. NAY , YOU GOTTA COMMAND THE COMPUTER TO ADD THEM UP!

 

which is done by the operator ‘+’

And , to feed value into ‘a’ and ‘b’ , you need to type something called ‘cin’ . Where the ‘in’ stands for ‘input’.

AND NOW , let’s look at what your homemade calculator looks like in code

 

1. #include<iostream.h>
2. #include<conio.h>
3. void main()
4. {
5. int a,b, sum;
6. cout<<”Enter two numbers:\n”;
7. cin>>a>>b;
8. sum=a+b;
9. cout<<”Sum is\n”<<answer;

And you’ve done it!

But YOU WILL DO SO MUCH MORE!

PS: This program won’t work. There’s stuff to cover. There are things called ‘bugs’ that mess your code up or give you not quite what you wanted . Also, much more syntax! So, stay tuned for the next part.

I encourage you to download code::blocks and keep trying new things and see what comes out! Here is  a link

www.codeblocks.org/

– Charmi Jagwani

 

 

 

 

 

 

Advertisements

6 Comments

  1. I have to thank you for the efforts you have put
    in writing this blog. I eally hope to see the same high-grade blpog
    posts from you later on as well. In fact, your creative writing abilities
    has inspired me to get my very own website now 😉

    • Hello
      With all due respect to your computing genius and huge ego, the language used in your comment was inappropriate. We do welcome criticism.
      We encourage you to have a look at other comments, we accept criticism with open arms,but we strongly discourage the ones which have a foul tone or language.
      Thank you for your comments. Do read our other articles and let us know your thoughts on the same,if possible in decent language.

  2. Pingback: Introduction to Programming : C++ #2 | Captain Knowledge

  3. Thanks for any other informative website. The place else may I get that kind of information written in such an ideal approach?
    I have a challenge that I am just now operating on, and I’ve been on the glance
    out for such information.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s