473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Class 2: Hello World. All Right, Let's Do This!

Banfa
9,065 Expert Mod 8TB
Posted by Ganon11

We have the foundation.

We know what it means to input or output.

We won't get confused when we're told to compile something.

It's time to do some programming!

In every language, there is one program which defines all of the syntax basics while displaying a friendly, warm message to encourage beginning programmers to keep going. This program is called Hello World, and here it is!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>                              // Line 1
  2. using namespace std;                             // Line 2
  3.  
  4. int main() {                                     // Line 3
  5.     cout << "Hello, World!" << endl;             // Line 4
  6.  
  7.     return 0;                                    // Line 5
  8. }                                                // Line 6
  9.  
  10. /* Output
  11.  
  12. Hello, World!
  13.  
  14. */
  15.  
*Sniffle*...brings a tear to my eye every time...All right, enough crying. What does all this mean? Let's take it one line at a time.

Line 1: This line tells the compiler that you want to include the files under iostream.h - a header file. iostream is Computer-Talk for Input/Output Streaming. This means that your program now recognizes all the functions and commands regarding input and output. As input/output are necessary for all programs, this line (also known as a statement) is vital.

Line 2: This will be explained along with line 4. For now, just be content with the knowledge that without this line, programming can become quite frustrating.

Line 3: This is called a function header. It tells the compiler that everything between this curly bracket '{' and the next curly bracket '}' are all part of the function main. Additionally, this function 'returns' an integer (int). The parentheses '()' are more useful when you make your own functions, but that's a ways off.

Line 4: Ahh...the first statement that does something! This is what tells the computer to display the words "Hello, World!". This is done with cout. cout stands for Common OUTput - by default, the monitor is your default output. cout is followed by two braces '<<' which tell the compiler that whatever follows is your output. "Hello, World!" is some text that must be enclosed in quotation marks. Be sure to close any quotation marks you start! Otherwise, the computer doesn't know when to stop reading text for output. Finally, we have endl, which tells the computer to put any further output on the next line.

Here's where Line 2 comes in handy. cout and endl are devices that have already been defined - we didn't define them. But we did have to tell the computer to include them. cout and endl, along with some other basic things, are all included in a group of objects (called a namespace). The namespace these happen to be located in is called std, or standard.

Without telling the computer that we are using std, we would have to type a lot more. Line 4 would become
Expand|Select|Wrap|Line Numbers
  1. std::cout << "Hello, World!" << std::endl;
  2.  
which is just UGLY. The last thing in this line is the semicolon. Earlier, I called a line of code a statement. Each statement is like an English command. In English, I can say, "Run a lap. Do 50 pushups. Say "I love you." Run another lap." You know how to seperate my commands based on where the period is. Similarly, the computer knows how to seperate my statements based on where the semicolon is. Without the semicolon, the computer might think that Line 4 and Line 5 were all part of the same statement, which would mess things up.

Line 5: This is the final statement of the function main. Here is where we return a value. Most functions we will write will have our own special personalized return values, but main() has only 2 return values. If we return 0, the computer knows that everything has run perfectly well. All is fine with the world... But if we return 1, the computer knows something has gone wrong. For example, suppose you ask the user for a number between 1 and 10. Cool. But what if the user types 56? Well, that's not between 1 and 10, so the program won't run like we thought! It would be proper to return 1 in this case to let the computer know that we're finished, but things didn't go as planned.

Line 6: The end bracket, corresponding to the bracket opened in line 3. This lets the computer know that any code after this is NOT involved with main().

A final note before we end: Compilers don't care if my code is easy to read or not. Technically, I could rewrite this program as
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main() { cout << "Hello World! << endl; return 0; }
  4.  
Or
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "Hello World! << endl;
  6. return 0;
  7. }
  8.  
But which is easiest to read? My style is called indenting - I use spaces to let you know what statements are part of what block. This may not be too difficult to understand now, but consider this:
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2. block1 {
  3. anotherBlock {
  4. block3 {
  5. statements;
  6. }
  7. moreStatements;
  8. }
  9. finalStatement;
  10. }
  11. return 0;
  12. }
  13.  
It's hard to tell what block moreStatements belongs to, right? That's hard to read! But if I wrote this as
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.     block1 {
  3.         anotherBlock {
  4.             block3 {
  5.                 statements;
  6.             }
  7.             moreStatements;
  8.         }
  9.         finalStatement;
  10.     }
  11.     return 0;
  12. }
  13.  
This is the EXACT SAME THING as before, but it's a lot easier to read, right? Your particular indenting style may vary - for exaple, putting the '{' symbol on the line after int main() instead of on the same line. But this won't change how the program is run, only how easy your code is for someone else to read. Develop your own style, borrow mine, it doesn't matter. Just please use something to make it legible so I can read it!

Until next time, programmers, when we'll learn a little more about outputting, and get some practice doing it for ourselves!
Dec 5 '06 #1
0 4655

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: MoCha | last post by:
hello everyone, i want to know of any trick used to restrict the access scope of a friend class (or function). ie instead of the friend class having access to the entire internals of the class in...
10
by: Bezalel Bareli | last post by:
I know I have seen some threads on the subject long time ago and it was using a virtual base class ... in short, what is the nicest way to implement the Java final class in c++ Thanks.
2
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the...
5
by: Joe Van Dyk | last post by:
Say I have the following class: using std::string; class Player { public: Player() : name(""), age(""), other_stuff("") {} private: string name; string age;
4
by: joh12005 | last post by:
Hello, i posted for suggestions a little idea even if it still needs further thoughts but as i'm sure you could help :) if would like to implement some kind of Condition class which i coud...
3
by: shapper | last post by:
Hello, I created a simple class as follows: Public Class HelloWorld Public Function SayMessage() As String Return "Hello World!" End Function
5
by: Lyle Avery | last post by:
Hello guys, Look at this in c++ file: class T { public: char c; char ca; };
9
by: d.adamkiewicz | last post by:
Hello Folks Anybody can show me real world singleton class example? Something that works (is implemented) as part of working solution. Does it make sense to create database handler class that...
7
by: triplejump24 | last post by:
Hello. Im working on what seems to me a very complicated programming assignment. I basically have to design a video library using class. The program needs to read data files i have and put the into...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.