How to Write a Simple Program in C++

It's common practice that instruction in C++ programming begins with creating a "Hello world" program. This involves printing a "Hello world" message on the text-only terminal interface of Windows systems, also known as the DOS console. This is the simplest possible C++ program that does something meaningful, so that's what this tutorial will focus on.

Things You'll Need

  • C++ integrated development environment (IDE), such as the one from Bloodshed Software.
  • Book on C++, such as "The C++ Programming Language" by Bjarne Stroustrup, Third Edition
Show More

Instructions

    • 1

      Launch the C++ integration development environment (IDE) you have installed in your computer. If you don't have a C++ IDE, download a free one from Bloodshed.net. Create a project, add to it a C++ source file and save your work.

    • 2

      Copy and paste the code below into the source file. Exclude the hexadecimal numbers in the first four columns of each row (0xXX). They are used as reference points in the steps that follow:


      0x00#include < stdlib.h >

      0x01#include < iostream >

      0x02

      0x03using namespace std;

      0x04

      0x05int main() {

      0x06

      0x07   cout << "Hello world." << endl;

      0x08

      0x09   system("PAUSE");

      0x0A   return 0;

      0x0B}

      Click Save.

    • 3

      Understand lines 0x00 and 0x01. They add two library files to the program via the "include" preprocessor directive. Then "stdlib.h" contributes the "system" function that freezes the console for Borland C++ Builder. (For other IDEs, you need neither "stdlib.h" nor "system.") "iostream" contains the "ostream" class and the objects "cout" and "endl" that are explained in Step 6.

    • 4

      Know the meaning of 0x03. This line tells the compiler that the program uses the set of functions from the "std" namespace. Namespaces are used to group functions together to prevent ambiguity and name duplication.

    • 5

      Make sure you understand lines 0x05, 0x0A and 0x0B. They relate to the "main()" function. Every C++ program has a "main()" function. The space that the braces of the "main()" function enclose is the program's execution space. The order of execution is from right to left and from top to bottom. The "main()" function returns an integer. When it returns zero, program execution has been successful.

    • 6

      Learn the meaning of 0x07. This code is the "Hello world" program. "cout" is an object of the "ostream" class from the "iostream" library file. It is used to print data to the console, an output operation. The carets, also called "insertion operators," are always associated with data output. "endl" is another "ostream" object that causes the line to break. Line 0x07 would then read in English as, "Transfer the following data stream (string followed by line break) to the console."

    • 7

      Compile the program and run it. You should get a message on the DOS console that reads, "Hello world." These steps are the basics of what constitutes a C++ program.

Tips & Warnings

  • C++ is a very efficient programming language for writing libraries.

  • Well-known C++ libraries include the Standard C++ Library, the Standard Template Library, the Microsoft Foundation Class Library (proprietary) and the Boost Library.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured