Advertisment


Author Topic: C++ First Program  (Read 3266 times)

0 Members and 1 Guest are viewing this topic.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3951
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
C++ First Program
« on: January 25, 2013, 04:05:09 pm »
Code: cpp [Select]

/*
   Welcome to the first chapter of this book.

   I will teach you how to program from the ground up (excluding setting up projects) in
      the way I learn best. In my opinion, hands on examples you can modify are the
      best methods to teach a new concept. Because of this, the tutorials will follow
      a very simple set of rules.

   1. Only new code will be commented.
      This keeps the code clean and less confusing.

   2. All tutorials will be short and to the point.
      Tutorial one will have the most amount of comments since it is also introducing
      syntax which is harder to show more often then explain.

   3. There are NO errors in this code.
      If your code is not compiling, then you most likely made a typo. All tutorials
      and text in this book is precompiled before being entered into PDF format to ensure
      code accuracy.

   4. Tutorials are designed for a Windows machine.
      This does not mean the tutorials do not transfer over to other platforms. It
      means that these tutorials have been tested specifically on a Windows machine.
      Programming concepts and the majority of these tutorials can be carried over
      to a Linux distro or a Mac machine.

   5. All tutorials have a help page.
      All tutorials presented in this book have a website equivilent. If you have questions
      pose them to the site included in each lesson.

   6. All tutorials have a project to go with them.
      If you have downloaded this book from online source aside from our site (http://Www.CelestialCoding.com)
      then there is a chance the code is not included. This books newest version can be downloaded from our
      site as well as the source code to go along with it.

   7. All tutorials are geared toward games.
      This keeps the interest alive as well as shows real world examples.

   With that, welcome to programming, lets start on lesson 1...
*/



Code: cpp [Select]

/*
      In programming, there are different ways to teach. One way is to have you read a book
         and test you at the end. Another is by hands on examples. I prefer the latter of the
         two teaching philosophies. This sentence you have been reading is called a
         'Block Comment.' Notice how it starts with a \* and ends with a  * /. This
         type of comment allows for multiple lines of comments to occur.
*/

//
//   This is a single line comment. Anything typed after the double slash is considered to be
//      a comment. This will allow us to do line by line commenting which will be the bulk
//      of the comments you read in the following tutorials.
//


#include <iostream> // Include the contents of iostream
/*
   #include <>
      The include keyword is what we call a preprocessor command. The IDE will
      replace this command with the contents of the file we asked for. It
      essentially copies and pastes the file above all the code below.
     
      There are two ways to use the #include keyword. One way, which we used above,
      is to use the greater than and less than symbols. This means the file we
      are requesting is located in the default folder set by your IDE. When installing
      a IDE, all the default files are copied automagically into this location. The other
      way to use the keyword is quotation marks. For example
         #include "myCustomHeader.h"
      The quotations tell the IDE that the file is located in the project folder. This is
      what you will use most often for when you create custom header files for your projects.
*/


int main()      // Main program entry with no parameters. Returns a type of int. Functions will be covered later.
{            // Opening bracket for function main()
   
   std::cout << "Hello world!" << std::endl;
   /*
      std::cout << "..." << std::endl;
         This is what is called a stream. For now, think of this as a way to
         print output to the console window. The command is made up of
         multiple parts explained below.
         
         std::
            This is what we call a namespace. The namespace is essentially a container that
            holds variable types and commands that should be divided from other code chunks.
            To use code from inside these namespaces, you have two options. The first is to
            use the way we did above. This keeps your code nice and clean if you only have
            a few commands from that namespace to use. The otherway is to declare that you
            will be using that namespace and you can then leave out the std:: and simply
            type 'cout << ...'.

         cout << "..." << std::endl;
            cout is a command we use to print data to the screen buffer. This can also print
            to a stream variable, but that is beyond the scope of this initial lesson. The
            '<<' symbol shows the flow of data. You can think of it as this anyhow since you
            will not by modifying this anytime soon in these tutorials or even in real life.
            Following the '<<' is the string "Hellow world!". This should be self explanitory.
            We then have another '<<' which is just stating that more data is following the previous.
            'std::endl' quickly follows. We cna remove the std:: since that is the namespace that
            contains the endl command. 'endl' simply means "endline." It allows us to have
            the equivilent of an enter press and move down to the next line in the console window.

         ;
            ALL STATEMENTS END WITH A COLON.
            Conditional loops, defines, macros, and functions are the only exception to this rule.
            That is all.
   */

   return 0;   // return 0 meaning 0 errors
}            // Closing bracket for function main()


/*
   *********************** NOTE *************************
   If your program flickers very fast, it is because it
   is running and exiting very quickly. Many IDEs will
   prevent a automatic close while in debugging mode. If
   you intend to use release mode, the code for pausing
   a program is located in the next lesson.
*/
« Last Edit: January 25, 2013, 04:25:25 pm by Nathan »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 130
  • Reputation 34
  • Gender: Male
    • View Profile
Re: C++ First Program
« Reply #1 on: January 29, 2013, 05:00:29 am »
Very nice.
However, I find that reading a book is the superior way. But that just might be me.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3951
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: C++ First Program
« Reply #2 on: January 30, 2013, 07:35:22 pm »
Very nice.
However, I find that reading a book is the superior way. But that just might be me.

Understandable. We all learn in different ways.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Shishka

  • - Secret Permissions Level 1 -
  • Full Member
  • ***
  • Posts: 171
  • Reputation 218
  • Gender: Male
    • View Profile
Re: C++ First Program
« Reply #3 on: February 05, 2013, 04:58:37 am »
I'm hands on and visual, I can't really do books well.
"Everything will be gone long before me. When the first living thing was born, I was here, waiting. When the last living thing dies, my job is finished. I'll put the chairs on the tables, turn out the lights and lock the universe behind me when I leave."

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 130
  • Reputation 34
  • Gender: Male
    • View Profile
Re: C++ First Program
« Reply #4 on: February 05, 2013, 06:46:25 pm »
Well. If the book is correctly written, then you can't really go wrong with it. Hands on is necessary in the end, but if you don't even know what you're doing, then it's rather pointless. You can waste loads of time trying to do something that's already been perfected many times over. You'd be better off picking up a book and reading how it's supposed to be done. The only downfalls are outdated knowledge, authors who don't explain things fully enough, and text that jumps from code to code and doesn't put up a bridge between the two. I.e. Windows programming. Of course, it's probably that way for a reason. Money money money.

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 130
  • Reputation 34
  • Gender: Male
    • View Profile
Re: C++ First Program
« Reply #5 on: February 05, 2013, 06:52:09 pm »
Speaking of which.. I've been trying to find a book that isn't 20 years old, that can help me out with windows programming. MSDN is great, but it pretty much has all three of those downfalls. And on top of that, they keep trying to push everyone to use some form of directx or mfc or other library. I would really just like a reference to the underlying code. No library that was created to make everything abstract and easy.


edit: This look like a good book? "Windows System Programming (4th Edition)"
« Last Edit: February 05, 2013, 06:56:09 pm by Vlykarye »

Offline Shishka

  • - Secret Permissions Level 1 -
  • Full Member
  • ***
  • Posts: 171
  • Reputation 218
  • Gender: Male
    • View Profile
Re: C++ First Program
« Reply #6 on: February 06, 2013, 03:14:47 am »
You can waste loads of time trying to do something that's already been perfected many times over.

And thats exactly why I self teach and can't stand reading book's on the subject. I don't want to do it someone else's way even if it is 'perfect' I want to figure out my own way, possibly better, most always worse.
"Everything will be gone long before me. When the first living thing was born, I was here, waiting. When the last living thing dies, my job is finished. I'll put the chairs on the tables, turn out the lights and lock the universe behind me when I leave."

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1421
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: C++ First Program
« Reply #7 on: February 06, 2013, 07:50:49 pm »
Speaking of which.. I've been trying to find a book that isn't 20 years old, that can help me out with windows programming. MSDN is great, but it pretty much has all three of those downfalls. And on top of that, they keep trying to push everyone to use some form of directx or mfc or other library. I would really just like a reference to the underlying code. No library that was created to make everything abstract and easy.


edit: This look like a good book? "Windows System Programming (4th Edition)"

Did you want native Windows API or something like C#?
Projects:
[ Axios Engine ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline Vlykarye

  • Full Member
  • ***
  • Posts: 130
  • Reputation 34
  • Gender: Male
    • View Profile
Re: C++ First Program
« Reply #8 on: February 06, 2013, 11:59:37 pm »
I want to learn more about the native Windows API. I found http://en.wikibooks.org/wiki/Windows_Programming during my endeavors. I think I will be able to learn a great deal from reading that book.