Jump to content

Archived

This topic is now archived and is closed to further replies.

Kapersky

"Hello World" in C++.

Recommended Posts

[align=center]c_0.png[/align]

 

"Hello World" in C++!

Hey guys. So most of us think that C++ is a difficult-to-learn language and that it's very advanced and stuff.

 

It is advanced, no doubt about that (and very powerful).

 

But it's really not a difficult language to learn.

 

Let's start by learning a Hello World program.

 

The function of this program is to print a simple "Hello World" line and then exit.

 

Let's begin!

 

 

#include 


int  main()
{
      std::cout   <<    "Hello World! ";
      std::cout   <<    "I'm from multimart";

      return 0;
}

 

Output: Hello World! I'm from multimart

 

 

Let's explain line-by-line!

 

 

 

Line 1: #include

 

=> This is a preprocessor directive. This means it is an order/directive to the preprocessor. The preprocessor is a program that does some specific things before the actual compilation of your program begins. We give an input to the preprocessor (in this case to include standard C++ library called "iostream"). It produces an output ( includes the iostream library). A library is a file containing special functions and elements which are used later in our program. In the "Hello World" program, we need the "iostream" library because we need to access the "cout" element. The "cout" element is a part of iostream library.

 

 

iostream => I/O Stream => Input/Output Stream

 

Our PCs have two basic streams:-

 

 

Input Stream: When we type something using keyboard, it is put in the input stream of PC.

 

==> The PC then returns the output by performing some operation on the input. Sometimes, the PC doesn't perform any operation and the input is returned in its original form.

==> The output is put in the output stream of PC.

 

Output Stream: Whatever we see on our Monitor, it is the output stream of PC.

 

 

NOTE: All lines that begin with the hash sign "#" are PD's (Preprocessor Directives).

 

 

Line 2: int main()

 

=> This line is really simple. It is a function declaration ( This lines declares a function ! )

 

=> A function is a group of programming lines/statements that produces some effect.

 

=> Once a function is declared, it has to be called somewhere in your script.

=> "main" is a special function. You don't have to call it. It is called automatically when your program begins.

 

=> Calling a function means to execute the function.

 

 

How to Declare a Function?

 

function_type function_name (function arguments in brackets)

 

So in this case, "int" is the type of function. main is the name of function. And at last, brackets are there to put function arguments in them. In this case there are no arguments.

 

The braces: { and }

 

Opening and closing braces enclose the function body. Everything related to the function has to be inside the braces.

 

 

 

Line 3 and 4: std::cout << "Hello World! ";

"<<" means to insert something.

 

In this case, we are inserting some text "Hello World" into the std::cout

 

Don't get confused by std::cout (It's really simple)

 

1) "std" stands for "standard".

2) "cout" stands for "character output".

 

Standard Character Output ==> std::cout

 

=> We use "::" because cout is a part of the "std" (standard C++ library)

 

 

 

So do you get it now? The function of the program was to output some text in the output stream of the PC. In C++, the element that determines the output stream is called "cout". Cout is a part of "iostream". So first we included iostream so that we could access "cout" later in our script. Then we used "cout" and inserted some text into it.

 

 

 

Line 5 : return 0;

 

=> This line stops the program. Whenever you have to stop a program, just return some value to stop it. For example: return 0;

 

Google helped me too regarding this.

 

Hope this will help ya guys! :)

 

Yours sincerely,

Kapersky.

Share this post


Link to post
Share on other sites

Jesus, code should be wrapped in code tags.......... This hurts my feelings.

 

Sorry, :(. I won't let you cry, my babes. I've enclosed the code, in code tags.

Share this post


Link to post
Share on other sites

Jesus, code should be wrapped in code tags.......... This hurts my feelings.

 

Sorry, :(. I won't let you cry, my babes. I've enclosed the code, in code tags.

 

TheOnlyDroid starts to feel much better.

Share this post


Link to post
Share on other sites

If I would keep reading such kind of tutorials, I'll be learning C++  within a month.

 

Go ahead; it's not that hard to learn. :) Once, you start learning.. then it'll not take much to master it.

Share this post


Link to post
Share on other sites

Thanks, learned a little few tricks I didn't know from this.

Share this post


Link to post
Share on other sites

Thanks, learned a little few tricks I didn't know from this.

 

Thank you for your input. Actually, I saw many "Hello world" in C++ tutorials on internet, but all was not so comprehensive. So I created this.

Share this post


Link to post
Share on other sites

I always use to make 'Hello Weed' programs lmfao. Well good one and simple.

 

Thanks :3 And I love to smoke weed :3

Share this post


Link to post
Share on other sites

I always use to make 'Hello Weed' programs lmfao. Well good one and simple.

 

Thanks :3 And I love to smoke weed :3

 

Haha likewise mate. How are you doing? Catch me on PMs sometimes.

Share this post


Link to post
Share on other sites

I always use to make 'Hello Weed' programs lmfao. Well good one and simple.

 

Thanks :3 And I love to smoke weed :3

 

Haha likewise mate. How are you doing? Catch me on PMs sometimes.

 

yooo ballluu wassup man hows that drug rehab goin

Share this post


Link to post
Share on other sites

You don't have to put that much information just for "Hello World" you can simply put less letters like this.

__________________________________________________________________________________________

#include

 

using namespace std;

 

int main()

{

 cout << "Hello World" << endl;

 return 0;

}

 

This is just more organized and more simpler to me, maybe others to this is what is mainly used.

Share this post


Link to post
Share on other sites

I started learning the basics of C++ then started going into more advanced one. I worked on a pokemon console that would actually simulate pokemon game, selling pokemons, buying pokeballs, catching pokeballs, money etc.

 

I stopped working on it since it limited me. I started working more on the "dynamic" system of the game, means, making battle simulations, catching pokemons simulation etc, but it pretty much didn't work out.

 

I was trying to go from one code to another, but it wouldn't allow me as there was error:

 

int part1()
{
    main();
}

int main()
{
    part1();
}

 

The error is that you can't go from part1 back to main, causing error and stopping my development. If you got any better idea of how to resolve such errors, don't hesitate replying me :)

 

PS: I've already tried "void" but it didn't work as well. (would run an infinite loop)

Share this post


Link to post
Share on other sites

I started learning the basics of C++ then started going into more advanced one. I worked on a pokemon console that would actually simulate pokemon game, selling pokemons, buying pokeballs, catching pokeballs, money etc.

 

I stopped working on it since it limited me. I started working more on the "dynamic" system of the game, means, making battle simulations, catching pokemons simulation etc, but it pretty much didn't work out.

 

I was trying to go from one code to another, but it wouldn't allow me as there was error:

 

int part1()
{
    main();
}

int main()
{
    part1();
}

 

The error is that you can't go from part1 back to main, causing error and stopping my development. If you got any better idea of how to resolve such errors, don't hesitate replying me :)

 

PS: I've already tried "void" but it didn't work as well. (would run an infinite loop)

 

Sure, I'll respond to you and will try to help you, but I can't at the moment, because I'm away from my home. I'll reply you with solution/fix as soon as I come back to home.

Share this post


Link to post
Share on other sites

Still waiting for the solution ;)

 

Sorry man! I went inactive due to my studies, and am still inactive.. I'm in fact using my phone, so can't write/create a code or solution now sorry! I'll surely hit you when I'd be able to solve your problem.. apologies!

Share this post


Link to post
Share on other sites


×
×
  • Create New...

Important Information

Please visit Terms of Use for our Terms of Use and Privacy Policy for our privacy policy