|
|
Basic C++ Coding | ||
Discussion by TheCapo with 11 Replies.
Last Update: December 13, 2008, 5:07 am | |||
![]() |
|
|
ok first if you want any comments in you program code there are two ways to do this.
first one i will show you is a block comment.
/* This opens the comment
you can have as many lines as you want in a block comment
this is how you close a block comment */
Okay the second way is a line comment
// A line comment can only be in that one line.
CODE
#include <iostream>// this is to include the input, output stream.
using namespace std
// this is used instead of having to keep writing std::cout
main ()
// this is used to start program
okay so if we where going to set up are program it would look some what like this.
CODE
/*NameDate
What the program does*/
#include <iostream>
using namespace std;
main ()
What i did there is setting up are program now we want it to do something for teaching purposes i am just going to say print "Hello this is a test of C++".
so what we need is a way to do a console output.
CODE
cout <<// This is used to say we are outputting to the computer screen.
<< endl;
// This is used to end that line of text or make a space. Note that making a space on a blank line with no code on you would do cout << endl;
okay now that we have some way to input the code we need to have "" this is where text goes.
CODE
cout << "Hello this is a test of C++." << endl;//That would print out Hello this is a test of C++ to the command prompt.
Now that we printed that we need a way to end the code so we would put a return 0;
CODE
return 0;// This basicly tells the program to end it self after one attempt at the program if it works.
if we put that entire code together we get this:
CODE
/*NameDate
What the program does*/
#include <iostream>
using namespace std;
main ()
{
cout << "Hello this is a test of C++." << endl;
cout << endl;
return 0;
}
There you go the most basic stuff of C++
When we get more advance in C++ you can do all sorts of things, you can do math, and make a calculator. We get if statements which is where if a variable does not met what it is should do ie. if (variable <= 5) if it is not equal to or less then 5 it wont say that part of the code. This is something that is needed in games, and if you want to learn PHP i would say learn this first to get the use of if statements.
However, I hate the way you write down several time the same line, it does not make the thing more clear to understand, seems that you simply want to add useless lines in your post.
QUOTE (yordan)
Rather basic tuto, but, why not, some of us are really starting from nowhere.However, I hate the way you write down several time the same line, it does not make the thing more clear to understand, seems that you simply want to add useless lines in your post.
Link: view Post: 130443
yea for some people it is harder to understand but when i was learning it that is the way i learned and i like it that way give what each thing is. It really depends on the person reading it you can really like it or really hate it. But i was not adding useless lines
CODE
/*NameDate
What the program does*/
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello this is a test of C++." << endl;
cout << endl;
return 0;
}
I had a stupid question about iostream (I am more familiar with stdio.h) and when I clicked on iostream, Answers.com automatically opened the Wikipedia page explaining the difference between iostream and stdio.h !
I don't know if this is a FileZilla thing or if it's a astahost forum feature, but I find it terribly efficient !
And of course it does not work with IE6...
QUOTE (pyost)
While this code does compile (maybe not in some compilers), it is not valid. main(), being a function like any other, must have a type! Since it returns an integer, the correct code is this:CODE
/*NameDate
What the program does*/
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello this is a test of C++." << endl;
cout << endl;
return 0;
}
Link: view Post: 130446
Is not valid what? Correct me if I'm wrong, but is C++ not just a layer on top of C? If written correctly, C++ compilers should have no problem compiling C programs, and a lot of C compilers are quite happy with a lot of permutations on main(), so much so that GCC (the compiler that I'm most familiar with) while conforming to ANSI standard C accepts:
CODE
main()CODE
main(void)CODE
int main()CODE
int main(void)And a whole host of others...why? As you said, main() is a function, but it's so damn commonly used (and admit it: programmers are lazy) that generally a lot of different variations are accepted (note that this isn't true for every function). One aspect you have to remember is that all of the above aren't quite what the compiler really "sees". All of the above variations are shorthand for:
CODE
int main( int argc, const char* argv[] )Do you really want to type that out every time? Nope, which is why a lot of compilers accept nigh-on anything that resembles it.
However, C and C++ syntaxes are not exactly the same, some lines are exact in C++ and not in C, and some other ones have the exactly opposite behavior.
And, of course, declarative statements allow you to know exactly what you are doing, sometimes trusting the default values could lead to unwanted results.
C++ have many things to learning to.
Such as copy constructor, type casting etc.
Here is an example,
CODE
class Test{
private:
int val;
public:
Test(int v);
};
Test::Test(int v)
{
val = v;
}
Test test = 127;
after I knew that the dummy hidden pointer object `this`.
It is a method(function) parameter that get passed to it and point to the object that invoked it.
Personally, I saw that most of time still are typical function calls.
With c, you are responsible to manage all of that.
With c++, compiler does this for you with type checking.
There are still many projects uses c as native API such as subversion.
I want to initialize an dynamic array and then constuct its values with 1 instruction. How could I do that?
Something like this:
CODE
int* m_array = new int[4];m_array = (4,3,5,6}
Similar Topics:
Basic Tips and Tricks in HTML
Html 39 ing amp Basic Codes ...
Visual Basic Projects: Scoreboard
C/c++ -gdb Linux Debug Tool Simple Gdb tutorial (1)
|
(1) Basic C++ Event Handler Class If you're familiar with C# event handlers, this is a C++ mimic of
|
HOME 





HTML and CSS basic coding and writing code for beginners
Game Maker: Basic Coding tutorial (Part 1/3 )
Learning BASIC Programming: Lesson 1
Blitz Basic Games Programming for Beginners - Part 1

