Loading...


bookmark - Basic C++ Coding

Basic C++ Coding

 
 Discussion by TheCapo with 11 Replies.
 Last Update: December 13, 2008, 5:07 am
 
bookmark - Basic C++ Coding  
Quickly Post to Basic C++ Coding  w/o signup Share Info about Basic C++ Coding  using Facebook, Twitter etc. email your friend about Basic C++ Coding Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Hi everyone today i am going to teach you a very simple program and how it is made. I wont get into anything fancy right now but will teach you how to run a program in the command prompt.

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

/*Name
Date
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

/*Name
Date
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.

   Wed Nov 5, 2008    Reply         

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. :rolleyes:

   Thu Nov 6, 2008    Reply         

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. :rolleyes:
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






   Thu Nov 6, 2008    Reply         


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

/*Name
Date
What the program does*/

#include <iostream>
using namespace std;

int main ()
{

cout << "Hello this is a test of C++." << endl;
cout << endl;

return 0;
}

   Thu Nov 6, 2008    Reply         

Wow !
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...

   Thu Nov 6, 2008    Reply         

Interesting tutorial. As mentioned its almost painfully brief, but that could be good enough to at least show people that they can make something appear on screen that they themselves created, and get them interested in looking deeper into coding with c++ or another language. Luckily most of the basic coding techniques are transferable between languages and just the syntax changes a little but after figuring out the differences the overall style of many individual little things doesn't change alot.

   Thu Nov 6, 2008    Reply         


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

/*Name
Date
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. :rolleyes:

   Mon Nov 10, 2008    Reply         

It is exact that C++ compilers should compile standard C programs without problem.
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.

   Mon Nov 10, 2008    Reply         

Basically, this is relatively simple tutorial and it builds the foundation every needed, Thx !!

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;

   Wed Nov 12, 2008    Reply         

Yes, in fact the difference between C and C++ that C++ has a real OOP/Object Oriented Programming for this reason the syntax is a bit different and in fact more comfortable, but I believe that people who works with simple C can do the same result as people who work with C++, I think what matters is the result and of course speed, performance, bugs :rolleyes:

   Wed Nov 12, 2008    Reply         

Yes, I do trust that.
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.

   Wed Dec 10, 2008    Reply         

Could I ask a question?
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}

   Sat Dec 13, 2008    Reply         

Quickly Post to Basic C++ Coding  w/o signup Share Info about Basic C++ Coding  using Facebook, Twitter etc. email your friend about Basic C++ Coding Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Basic Tips and Tricks in HTML

Here is some quick links for basic html coding... A quick and easy way to create your first web page! -> The easiest HTML guide for beginners You'll learn how to create tables from real examples. -> [url="http: ...more

   03-Sep-2004    Reply         

Html 39 ing amp Basic Codes ...

** I was looking through the tutorials page and I saw one tutorial on beginning HTML, I thought I would expand a little on it and show ya some basic codes you can use for your website if your beginning HTML! ** ...more

   01-Feb-2006    Reply         

Visual Basic Projects: Scoreboard

Hi again, I've done another visual basic tutorial in this forum on how to make a simple, customized web browser. Now I am going to say how to make a scoreboard. To do so, open a Standard EXE project in Visual Basic 6.0 and insert 6 labels and 2 command buttons. Change the captions on ...more

   21-Aug-2007    Reply         

C/c++ -gdb Linux Debug Tool Simple Gdb tutorial   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   Basic C++ Event Handler Class If you're familiar with C# event handlers, this is a C++ mimic of