473,614 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Banfa
9,065 Recognized Expert Moderator Expert
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*...bri ngs 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 4670

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

Similar topics

3
2243
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 question, let it have access to a few private/protected members. this problem is cropping up several times. giving the friend class full access does not seem right n at the same time the function(s) r not reqd by anyone else.
10
5105
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
2162
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 parent class. could this be done by a reference, or just by constructing the class. function Class1() { this.class2 = new Class2();
5
3065
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
1894
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 use to build bricks of more complex condition, conditions are based on fields by using regexp class Condition:
3
1619
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
4739
by: Lyle Avery | last post by:
Hello guys, Look at this in c++ file: class T { public: char c; char ca; };
9
2140
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 way? Regards Darek
7
354
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 in arrays. I have to be able ot check out a video, check in a video, get video info, get customer info. Theres alot of steps but i dont need to do all of them to get the points i need. If anyone has anytime to help me i would really really...
10
3571
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 = "hello world";
0
8621
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8272
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6087
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4049
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4119
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1421
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.