HOME       >>       Programming

Basic C++ Coding


TheCapo

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.

#include <iostream>// this is to include the input, output stream.using namespace std// this is used instead of having to keep writing std::coutmain ()// this is used to start program
okay so if we where going to set up are program it would look some what like this.
/*NameDateWhat 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.

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.
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;
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:
/*NameDateWhat 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.

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.


TheCapo

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.

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

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:

 

/*NameDateWhat the program does*/#include <iostream>using namespace std;int main (){ cout << "Hello this is a test of C++." << endl; cout << endl;return 0;}

yordan

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 Xisto forum feature, but I find it terribly efficient !And of course it does not work with IE6...


Jeigh1405241495

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.


Mordent

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:

 

/*NameDateWhat the program does*/#include <iostream>using namespace std;int main (){ cout << "Hello this is a test of C++." << endl; cout << endl;return 0;}
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:

 

main()
main(void)
int main()
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:

 

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.

yordan

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.


magiccode91405241511

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,

class Test{private: int val;public: Test(int v);};Test::Test(int v){ val = v;}Test test = 127;


Quatrux

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


magiccode91405241511

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.


tek3D

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:

int* m_array = new int[4];m_array = (4,3,5,6}



VIEW ORIGINAL REGISTER GET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.