C++ help

C++ help

Post by thehackertoyou on Thu May 23, 2013 11:47 pm
([msg=75760]see C++ help[/msg])

So I'm trying to learn C++ and the only tutorial I can find says

"The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program"

It then says I should use
Code: Select all
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}


And then it explains line by line what each thing does. But the problem is I know absolutely no C++ yet and so all the explanations went over my head. I figured if I understood why instead of just how I would become a much better programmer.

So first, what do I need to download to write in c++? The tutorial seems to say I have to have a compiler to write c++ code but I thought a complier is essentially a code translator, so that some high level code can be ran in some lower level language. Is there something equivalent to IDLE for python, but for C++?

Second, what is a C++ console?

Lastly, could you explain what each line in "my first program" does, and more importantly, why it's needed to get the end result? Like, what would happen and why if "#include <iostream>" (for example) was taken out?

Also, if it helps at all I have some python experience and I'm using a mac.

Thanks a lot in advance

--thty
thehackertoyou
New User
New User
 
Posts: 18
Joined: Tue Jan 01, 2013 1:27 pm
Blog: View Blog (0)


Re: C++ help

Post by -Ninjex- on Fri May 24, 2013 1:10 am
([msg=75761]see Re: C++ help[/msg])

thehackertoyou wrote:So I'm trying to learn C++ and the only tutorial I can find says

"The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program"

It then says I should use
Code: Select all
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}


And then it explains line by line what each thing does. But the problem is I know absolutely no C++ yet and so all the explanations went over my head. I figured if I understood why instead of just how I would become a much better programmer.

So first, what do I need to download to write in c++? The tutorial seems to say I have to have a compiler to write c++ code but I thought a complier is essentially a code translator, so that some high level code can be ran in some lower level language. Is there something equivalent to IDLE for python, but for C++?

Second, what is a C++ console?

Lastly, could you explain what each line in "my first program" does, and more importantly, why it's needed to get the end result? Like, what would happen and why if "#include <iostream>" (for example) was taken out?

Also, if it helps at all I have some python experience and I'm using a mac.

Thanks a lot in advance

--thty


I learned what I know from www.learncpp.com

I am not the greatest programmer, but can answer the questions to the best of my ability.

All you need to write C++ code is some kind of text editor. You can do this in notepad, a terminal nano session, etc. Just make sure you save your file with a .cpp extension. Now, your code is pretty much useless, unless you compile it, so that your computer knows how to intercept that code and use it correctly. This is why you need a compiler. The compiler I have always used is GCC/G++. A compiler basically translates your code into machine language, so that the computer knows how to interpret it. So you might wonder why programming in C++ opposed to machine language or something really close such as assembly language, is better. The answer is that in C++, you can be more versatile with OOP, and it is relatively easier to understand, opposed to writting in 1's and 0's.

I will assume that by C++ console, they are meaning some kind of CLI (command line interface) Essentially, your C++ code will be outputed into some kind of console / terminal. This would be neccessary to see the output of your code, unless you build your program to be a GUI application.
Code: Select all
#include <iostream>

#include is the declaration that you are about to add in a header file from outside the program, and what follows (<iostream>) is the header, which will be brought into the program for use.

Code: Select all
using namespace std;

This is basically telling the program that you would like to use a namespace (std) globally, so that you do not need to directly declare it during each use. std has the command cout and cin, which will allow you to write code out to the console (cout), and to get user input to the console (cin) If the using namespac std; was left out of the code, you would have to declare it as: std::cout or std::cin, each time you would like to use it. Adding the namespace for global use, makes it less tedious if you need to call the command several times.

Code: Select all
int main ()

Every program in C++ needs a function called "main" which will be what the program executes during run time, from top to bottom. Now, the "int" basically states that your "main" function will return some kind of integer, which will be "0" as you can see in your code above at "return 0;"

Code: Select all
cout << "Hello World!";

This does as stated above, and prints the desired information to the console / terminal.
If you removed the #include <iostream>, which contains std, the compiler would issue an error that the command would not be found, as it does not know what cout << is, until you bring the header in. If you did not declare "using namespace std;", the program would not know where to look for cout (std), and you would need to declare it as std::cout << "Hello World!";
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
^(-.^)>
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1372
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: C++ help

Post by pretentious on Fri May 24, 2013 5:04 am
([msg=75764]see Re: C++ help[/msg])

If you need help, gimme a yell. I've got assignments coming up and will be living and breathing C for the next week probably haha and GLUT, anyone here good with GLUT?
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
pretentious wrote:Welcome to bat country
User avatar
pretentious
Contributor
Contributor
 
Posts: 676
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: C++ help

Post by thehackertoyou on Fri May 24, 2013 7:14 pm
([msg=75772]see Re: C++ help[/msg])

Okay thanks, that makes a lot of sense. but I have a few more questions now.

You mentioned OOP. What is that?

What are the differences between C, C++ and all the other forms of C that people mention?

I get what "using namespace std" does, but I don't quite get what Iostream does. In my (probably flawed) guess of what it means, is it like specifying what library of commands in C++ to use?

--thty
thehackertoyou
New User
New User
 
Posts: 18
Joined: Tue Jan 01, 2013 1:27 pm
Blog: View Blog (0)


Re: C++ help

Post by Assassian360 on Fri May 24, 2013 8:05 pm
([msg=75773]see Re: C++ help[/msg])

thehackertoyou wrote:You mentioned OOP. What is that?


OOP stands for Object Oriented Programming. It's the idea you can represent related information as a single definition. Then once you have defined what properties something might have you can create objects of that type in your code without having to write masses of duplicate variables. For example you may have a class that represents a student containing information about their name, a course they are studying, etc. By having it defined already as a structure it allows you to have the methods and attributes of that object all together simplifying the creation of a student.

thehackertoyou wrote:What are the differences between C, C++ and all the other forms of C that people mention?


C++ is essentially C with OOP added. The language C doesn't directly support OOP as part of what it includes. C# is a little different again. It supports OOP like C++ does, but it is designed to work with Microsoft's .NET libraries.

thehackertoyou wrote:I get what "using namespace std" does, but I don't quite get what Iostream does. In my (probably flawed) guess of what it means, is it like specifying what library of commands in C++ to use?

When you write programs in C++ and most languages you don't automatically have access to all the possible libraries of code. To "include" all the libraries would bloat your program. So instead you have to specifically indicate what libraries you are wanting to use. iostream contains the basic content for input and output "streaming". (hence the "io") And so the streaming part is referring to being able to queue up output or read just parts of input without having to do it all at once. Typically when starting out that won't be something you have to think about right away and you can just assume that things like cout and cin just print out data or read in data.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: C++ help

Post by centip3de on Fri May 24, 2013 8:31 pm
([msg=75774]see Re: C++ help[/msg])

**EDIT** Dammit, someone beat me to it.

thehackertoyou wrote:Okay thanks, that makes a lot of sense. but I have a few more questions now.

You mentioned OOP. What is that?

What are the differences between C, C++ and all the other forms of C that people mention?

I get what "using namespace std" does, but I don't quite get what Iostream does. In my (probably flawed) guess of what it means, is it like specifying what library of commands in C++ to use?

--thty


OOP - Object oriented programming. This is a type of programming that uses classes, inheritance, polymorphism, and a bunch of other things that make it easier to write large programs (although harder to write small ones, in my opinion).

Difference between the C's - The only difference between the C's is the level at which their written. Or, how close to the metal you want to write. Although C++ is almost completely compatible with C, C is not compatible at all with C++. C++ is much higher level, so you can use OOP, and dealing with memory is a bit easier. Where as with C, you can't use OOP, and dealing with memory is more difficult.

Iostream - It's a file that has a whole bunch of functions for you to use so you can do Input/Output (or IO) tasks, like, getting input, printing output, and a whole slew of other things.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1435
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: C++ help

Post by thehackertoyou on Mon May 27, 2013 3:34 pm
([msg=75803]see Re: C++ help[/msg])

Alright, thanks all, last question, what are some good programs to practice making?

--thty
thehackertoyou
New User
New User
 
Posts: 18
Joined: Tue Jan 01, 2013 1:27 pm
Blog: View Blog (0)


Re: C++ help

Post by centip3de on Mon May 27, 2013 3:40 pm
([msg=75804]see Re: C++ help[/msg])

thehackertoyou wrote:Alright, thanks all, last question, what are some good programs to practice making?

--thty


Start small and then slowly increase in difficulty:

- A hello world program
- A hello program that displays the name a user enters at the end. Ex: User enters "Jim", prints out "Hello, Jim".
- A simple calculator (add, subtract) that uses functions
- A simple text-based adventure game
- A more advanced calculator (add, subtract, multiply, divide) that uses operator precedence (think PEMDAS).
- A more advanced text-based adventure game that uses OOP
- Etc.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1435
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: C++ help

Post by WallShadow on Mon May 27, 2013 4:21 pm
([msg=75809]see Re: C++ help[/msg])

Corrected:

centip3de wrote:Start small and then slowly increase in difficulty:

- A hello world program
- A more complex hello world program
- An even more complex hello world program
- An insanely complex hello world program that seems to have nothing to do with hello world
- A hello world program that requires a whole botnet to compute completely
- Etc.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 602
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests