Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[C++] Basics


Recommended Posts

**Note!**

* Before you start, get a C++ program editor ex:Bloodshed Dev-C++. I highly recommend it! it is free. Just google it.
* After you have it follow the following steps. This is for Dev C++(i highly recomend it! Visual Studio is a bit tricky to set up)

1. After installation open it up.

2. Click on file ->> new project.

3. Name it what ever you want & select empty project.

4. Click okay and open it up.

5. Now right click on project and creat new file.

6. Rename the file but make sure it end with cpp. Ex:```
main.cpp
```

**Media:**

>! ![](http://i.imgur.com/8FgEG.png)

* Right now your screen is pretty blank right now. Well lets change that.
* Remember to start the entire code with:

```

#include using namespace std;

int main()

{

```

This code does nothing so far. Our objective is to display a sentence saying hello world.

Look In Spoiler For Demonstration!

**Media:**

>! ![](http://i.imgur.com/vNuyZ.png)

Okay now to display something in c++, we use cout. (C-OUT).

```
cout << "text here";
```

Using```
cout
```&```
<<
```&```
"text here";
```will show a message saying```
text here
```
When you use the```
cout <<
```letters, it means or shows that you want to display something.

For example:

```

int a=3;

cout << a;

will display 3 because you used cout << & the variable a.

Please don't be confused to think that you need a variable to use cout.

you can use cout << with anything.

To display a text or i would say anything when you run your program, you can use either:

cout << & variable OR cout << "text here";

The "text here" is to show you how to make it display a text or word etc.

```

Also know that for some statements or lines of code to work they must always end with the symbol:

```
;
```If not you will get an error.

Now lets try to setup a hello world project with what you just learned.

If you need help you can copy and paste the code provide below.(OPEN SPOILER)

It is not necessary that you use my procedure you can use different alternatives. But for now i recomend you use my way so that you can edit it later to test and try out.

Code Snippet:

>! ```
>! #include using namespace std;
>! int main()
>! {
>! cout<<"your text here";
>! cin.get();
>! return 0;
>! }

```
>! I added```
cin.get();
```which will keep the window open untill you click anykey on your keyboard. This way you can see how the texts look like.

If you got it nice job. Congrats!. If not try again & post problems and comments below

You can now move on to next bit.

**Variables & Variable Identifiers :**

* First lets talk about variables.Lets say you have to take care of your monthly expenses. Now you can have rent expense, grocery, misc expense & others. And you have to keep track of all of them and how much they are every month.
* Example:

```

Rent Expense = 100;

Grocery = 45

Misc Expense = 200

```

* That is what a variable is pretty much, that bunch of numbers you have to keep track of.
* But consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them.
* Each variable needs an identifier that distinguishes it from the others.
* For example, in the previous code the variable identifiers were rent expense, grocery and Misc Expense.
* In real life we can call them that if we wanted but in programming world there are some set backs.
* We can call the variables any names we want to invent, as long as they are valid identifiers.

A variable identifier is used to let me say "brand' a variable. In C++ an identifier can be one or more letters, digits or underscore characters (_). You can't have a space or punctuation marks or symbols.

Ex:

```

Proper Identifiers:

int newName;

int new_name;

int new123;

Bad Identifiers:

int new.name;

int 123wower;

int 123Meow;

[color]int new; ( it is a reserved word.) [/color]

[color]int n ew; no spaces allowed.[/color]

[color]They always have to begin with a letter not numbers!

You can begin with underscores too, but in some cases these may be reserved for compiler specific keywords or external identifiers.

Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are [i]reserved keywords[/i].

Reserved Keywords:

[code]

asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while

[/code]

There are others you can't use certain times.

[code]

and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq

[/code]

Also know that your compiler might also have it's own reserved keywords.

[b]Very important:[/b] The C++ language is a [i]"[/i]case sensitive[i]"[/i] language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters.

Example:

[code]RESULT [/code]
variable is not the same as the

[code]result[/code]
variable or the

[code]Result[/code]
variable.

These are three different variable identifiers.

And that is pretty much most of what you need to know for C++ Variables & Variable Identifiers.[/color]

[color]Thank you for your time. By now you should have a good understanding of variables and how to write them right.[/color]

[color]Post problems & comments below![/color]

[color]Thank you for your Time.[/color]
```
Link to post
Share on other sites
"A computer can make decisions based on data given to it. A calculator is a computer because to makes decisions based on the input you give it."

What you've written there is essentially the same thing. Data doesn't magically appear, it has to be manually inputted at some point.
Link to post
Share on other sites
> "A computer can make decisions based on data given to it. A calculator is a computer because to makes decisions based on the input you give it."
>
> What you've written there is essentially the same thing. Data doesn't magically appear, it has to be manually inputted at some point.

Yeah i wanted to give an example, like something they can relate to.
Link to post
Share on other sites
> This is a terrible post.
>
> I am moving this to Off-Topic until you modify it to not only format it uniformally, but also edit the content to be factual information instead of a garbled mess.
>
> Thanks,
>
> Magical Aeroplane

Re wrote the thread. Is it good now or just garbled mess and un factual information.
Link to post
Share on other sites
You seem really mixed up in your explanations. You are pointing out some basic stuff then confusing them with stuff they really dont need to know.

Also this isnt really a start to programming at all. I suggest you explain some basic stuff like you (sort of) did and then go onto the classic hello world. Also this may just be me but i think people should learn c first. Or at least c style.

Thanks for submitting the tut, though like Aero said it really is kind of a mess. Do you know c++ or are you learning yourself?

Should also work on your formatting, unless code tags are just messing that up.

[Edit]

As you are editing it it is definitely improving.
Link to post
Share on other sites
> You seem really mixed up in your explanations. You are pointing out some basic stuff then confusing them with stuff they really dont need to know.
>
> Also this isnt really a start to programming at all. I suggest you explain some basic stuff like you (sort of) did and then go onto the classic hello world. Also this may just be me but i think people should learn c first. Or at least c style.
>
> Thanks for submitting the tut, though like Magical Aeroplane said it really is kind of a mess. Do you know c++ or are you learning yourself?
>
> [Edit]
>
> As you are editing it it is definitely improving.

So should i make a seprate part just for beginners?

I can make it into spoilers some parts. So beginners can learn the basics, & i guess advanced beginners can skip some parts.
Link to post
Share on other sites
> Magical Magical Aeroplane asked me to rewrite it and fix errors and mistakes.
>
> Give him some time he will move it.
>
> He moved it here in the first place.

He won't move it back until you actually fix it >_>

At least rename this "Introduction to C++ Basics" and not "C++ Hello World". It's a bit of an overload of information for someone looking to just output "hello world".
Link to post
Share on other sites
> He won't move it back until you actually fix it >_>
>
> At least rename this "Introduction to C++ Basics" and not "C++ Hello World". It's a bit of an overload of information for someone looking to just output "hello world".

Yeah lol i was thinking of changing the title but i think some one already beat me to it.
Link to post
Share on other sites
> Dev c++? why would you even suggest that its been droped from its developers a long time ago

Because it works.

And is still used by developers.

There is no C++ compiler not used.

I only recommended it because the setup is easy.

You can try eclipse if you want same thing. A compiler is a compiler.
Link to post
Share on other sites
> Because it works.
>
> And is still used by developers.
>
> There is no C++ compiler not used.
>
> I only recommended it because the setup is easy.
>
> You can try eclipse if you want same thing. A compiler is a compiler.

No thats not what i meant. Dec c++ has stoped being updated since 2005\. most c++ learning sites dont suggest using it simple simple because it hasnt got any support anymore. Also setting up an IDE isnt that hard there are step by step tutorials in every site
Link to post
Share on other sites
> No thats not what i meant. Dec c++ has stoped being updated since 2005\. most c++ learning sites dont suggest using it simple simple because it hasnt got any support anymore. Also setting up an IDE isnt that hard there are step by step tutorials in every site

Well it still works fine.

But it in the end it doesn't matter what compiler you use. As long as you have the right code it will work.

I didn't look that well into Dec ++ , i didn't see it was not supported anymore.

But i have the new beta version… if it is not supported why do they keep upgrading it.
Link to post
Share on other sites
> Code Blocks is free and it is still updated. I first learned C++ using Dev c++ howevr the switch over to codeblocks was one of the best things I could have done.

Yeah i started to use it now.

Pretty nice.

I only stick to Dev C++ because am still in C++ course in highschool and that is the compiler we are currently using.
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...