Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Starting with C++; For all of us beginners in C++
Topic Started: Jul 12 2008, 01:22 AM (1,556 Views)
Gandalf20000
Member Avatar
Geek
[ *  *  *  *  *  * ]
Since almost all of us here are beginners with C++, I thought I'd give a tutorial for getting started with C++. I don't know much, but I know some.

C++ Basics:

  • You use comments just like you would in Game Maker. // for a line, /* and */ for a block.
  • You initialize variables almost the same way. You must define what kind they are. Here are a few kinds of variables, mainly the most used:

    • int: used to create a normal integer variable. Has a range of a little more than four billion.
    • float: used to create a 32 bit floating point value variable.
    • double: used to create a 64 bit floating point value variable (e.g., a variable that has a decimal). You could use float, but it's less accurate and uses about the same amount of memory. Has a range of about 1032.
    • bool: used to create a variable that either has a value of true (usually 1) or false (usually 0).
    • unsigned int: An integer that is only positive.
    • long: An integer that has a larger range than int.
    • char: A variable that holds one character. Example: char a='a'
    • string: A variable that holds a string of characters. Example: string helloWorld="Hello World."

  • You must include any headers you're going to use. This is done by putting #include <blahblahblah>
  • You usually have to say what library you're going to use by putting in "using namespace std;"
  • You then have to define any global variables, etc.


Here's a basic program, with plenty of comments to help you out. When you're finished, if you've done it right, it should say, "This is my first C++ program!"
Code:
 

//This is a basic C++ program.

//Include your header files.
#include <cstdio>
#include <cstdlib>
#include <iostream>

//Say what namespace to use.
using namespace std;


//Start the main function
int main(int nNumberofArgs, char* pszArgs[])
{
//Start programming here! For this example, we'll make the program print text on the screen that says, "This is my first C++ program!"
cout << "This is my first C++ program!\n";

//Make sure you can read the output so you know you did it right!
system("PAUSE");
//The program isn't returning any particular value, so just put in zero.
return 0;
}


This concludes my tutorial (for now). If you need more details, I'd be happy to go more in depth.
Edited by Gandalf20000, Aug 21 2008, 04:38 AM.
Offline Profile Quote Post Goto Top
 
Mauro
DirectX - WinAPI Programmer
[ *  *  *  * ]
mmh

that says, "This is my first C++ program!"

you forgot the "//" ^^ (since some peple only copy-and-paste the code...

btw good ;)


Programming c++ and directx
Offline Profile Quote Post Goto Top
 
Rixeno
Member Avatar
Teo-Carliss
[ *  *  *  *  * ]
Yeah, it's almost like the "Hello World" (Actually, it is the same thing, just different "quote")... At least here it explains some stuff, other sites only say to COPY-PASTE-COMPILE-RUN-DONE...
Report spam the nanosecond you see it.
Ninjas are everywhere, for all you know there are eleven behind you right now, including me...
Offline Profile Quote Post Goto Top
 
Gandalf20000
Member Avatar
Geek
[ *  *  *  *  *  * ]
It is a lot like the "Hello World!" program. However, I wrote this from scratch, no copying, simply what I've learned. I can give you guys another program in the tutorial if you want.
Offline Profile Quote Post Goto Top
 
harkathmaker
Member Avatar
Persistent Enquirer
[ *  *  *  *  * ]
Is it required to place a return statement? Or is it "good coding"?
Offline Profile Quote Post Goto Top
 
Rixeno
Member Avatar
Teo-Carliss
[ *  *  *  *  * ]
No, I'm not saying it is, it just reminds me of the "Hello World" example because of the simplicity :P. Although the "Hello World" example used different functions from only one header, unlike three. But yeah, it just made me think of that, sorry if it was offensive :huh:
Report spam the nanosecond you see it.
Ninjas are everywhere, for all you know there are eleven behind you right now, including me...
Offline Profile Quote Post Goto Top
 
Gandalf20000
Member Avatar
Geek
[ *  *  *  *  *  * ]
Yeah, I used 3 headers because those are the headers I learned C++ with. I don't know what they do exactly (And I really don't care. :rolleyes: ), so I included all 3 of 'em. I wasn't offended you thought it was like the "Hello World" program. I think it is as well.
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
harkathmaker
Jul 13 2008, 01:59 AM
Is it required to place a return statement? Or is it "good coding"?
If the function does not have void as return type (which implies that there is no return value), it always has to contain a return statement. It should also have return statements placed so that some return statement is reached in every case. Otherwise you will get a warning. A little example on this. The following piece of code would result in a compiler warning, because it may be that no return statement is reached:
Code:
 
int GiveMeANumber(bool TheNumberHasToBeFive){
if(TheNumberHasToBeFive){
return 5;
}
}

The following code would not result in a warning:
Code:
 
int GiveMeANumber(bool TheNumberHasToBeFive){
if(TheNumberHasToBeFive){
return 5;
}
return 7;
}


P.S.: Nice tutorial. The example application really is very similar to a Hello World application, but this is always a good start. Anyway here are two small suggestions how it may be improved:
- Your description of the using namespace keyword is a bit misleading. Namespaces are a technique to avoid naming conflicts. Many libraries place all of their content in a namespace. If the library includes a function called foo() and useses the namespace bar the client would have to write bar::foo() to access this function. The :: resolves the namespace. The advantage of having foo() in a namespace is that the client may specify another function called foo() then. It would not result in a conflict, since the compiler can distinguish between the foo() from the library and the foo() specified by the user through the different namespaces. But if you are using very many functions from a particular library you may find it buggy to write something like bar:: all the time. For this reason you may tell the compiler that you want everything from a particular namespace to be in your namespace. To do so you use using namespace.
- You should add float to your list of data types. double is a 64-bit floating point value, while float is a 32-bit floating point value. Usually float is used more often, since 32-bit are usually precise enough. All 3D APIs you will find out there will work almost exceptionally with float.
Offline Profile Quote Post Goto Top
 
Bazza
Member Avatar
Forum God
[ *  *  *  *  *  * ]
yay ive made a program that can type words. now if only i can find some one that actualy wants to read......
My instinct is to hide in this barrel, like the wily fish.
Offline Profile Quote Post Goto Top
 
Nextmastermind
Member Avatar
Forum Leader
[ *  *  *  *  * ]
Nice...Now I'm off to find out how to get C++...
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
nextmastermind
Feb 24 2009, 04:30 AM
Nice...Now I'm off to find out how to get C++...
Try Miscrosoft Visual Studio 2005 or 2008.
Blog|EHS
Offline Profile Quote Post Goto Top
 
Nextmastermind
Member Avatar
Forum Leader
[ *  *  *  *  * ]
skarik
Feb 24 2009, 04:59 AM
nextmastermind
Feb 24 2009, 04:30 AM
Nice...Now I'm off to find out how to get C++...
Try Miscrosoft Visual Studio 2005 or 2008.
Free? If not, how much?
Offline Profile Quote Post Goto Top
 
sharlowidalot
Member
[ *  * ]
nextmastermind
Feb 24 2009, 11:36 PM
skarik
Feb 24 2009, 04:59 AM
nextmastermind
Feb 24 2009, 04:30 AM
Nice...Now I'm off to find out how to get C++...
Try Miscrosoft Visual Studio 2005 or 2008.
Free? If not, how much?
Free, download Here i dunno where 2005 is :dunno:
Offline Profile Quote Post Goto Top
 
Nextmastermind
Member Avatar
Forum Leader
[ *  *  *  *  * ]
I'm guessing I download the game developers kit? (Dark GDK)
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
NO.

Download the Visual Studio.

Here's 2005 if 2008 crashes often for you: http://www.microsoft.com/express/2005/

Don't use GDK, use some other engine. Hell, mod Half Life. That's fun to do, and it helps lots with understanding of arrays and pointers.
Edited by skarik, Feb 25 2009, 02:10 AM.
Blog|EHS
Offline Profile Quote Post Goto Top
 
Go to Next Page
« Previous Topic · Tutorials · Next Topic »
Add Reply