75% developed

Intro To C++/First Program

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Intro To C++
First Program Variables

Essential Tools[edit | edit source]

Text Editor[edit | edit source]

C++ programs are initially created as a plain text files and saved with the file extension of "cpp". These can be done in any text editor.

Compilers[edit | edit source]

In order to execute C++ programs, a compiler is needed.

As one of the most popular compiled languages, C++ has a number of compilers to use for a number of different systems.

GNU/Linux[edit | edit source]

In GNU/Linux systems, a compiler is often shipped with your distribution by default.

You can check what kind of C++ is installed in the terminal with the following command.

c++ -v

.

If no compiler is installed, GCC is typically available in a package manager.

MacOS[edit | edit source]

In the case of Macbook,for a beginners, you would want to install Xcode from the Apple app store and play around with that, which includes both a compiler and an editor.

Windows[edit | edit source]

For Windows application,the GNU C++ compiler could be easy under the terms of the General Public license (GPL). Part of GNU is "Minimalist GNU for windows"(http://www.mingw.org). You can launch the website, download compilers and install them.

Getting to know an IDE[edit | edit source]

IDE is an abbreviation for Integrated Development Environment and allows for programming in an easier fashion than a standard text-editor. An IDE is usually composed of a source code editor, build automation tools and a debugger. For beginners, they may or may not need all the features of a IDE. It is up to each developer on what they want to use and require. In C++, Code::Blocks (codeblocks.org), NetBeans (netbeans.org), Eclipse CDT (eclipse.org)[note 1] are popular as a free IDE.

Code::Blocks NetBeans Eclipse
Website http://www.codeblocks.org/ http://netbeans.org/ https://eclipse.org/cdt/
IDE screenshot
Code::Blocks IDE screenshot
Code::Blocks IDE screenshot
Netbeans IDE screenshot
Netbeans IDE screenshot
Eclipse IDE screenshot
Eclipse IDE screenshot
Logo
Codeblocks logo
Codeblocks logo
Netbeans logo
Netbeans logo
Eclipse Luna Logotype
Eclipse Luna Logotype

Through the command-line you can use the -o flag to compile to run a program without requiring an IDE. However, using an IDE can help a programmer with automatic formatting, easier debugging, highlighting along with many other features. Some development environments also can allow multiple programmers work together more efficiently and effectively.

Your first program[edit | edit source]

In a plain text editor or an IDE, you can copy the following code.

#include <iostream>
using namespace std;

//single line comment

/*
multi
line
comment
*/

int main() {
    cout << "Hello, enjoy fun!!" << endl;
    return 0;
}

Through an IDE or command line, if you compile and run the program, you should get the following result:

Hello, enjoy fun!

If we analyze the simple program shown above, it consists of preprocessor directive, comment, main function and single line statement. From the above program, each part is explained in detail in the following table:

Explanation of the Hello World program.
Preprocessor directive These are processed by the compiler before the program code must appear. #include instructs the compiler to use the standard C++ library and library name is specified through angled bracket. And "using directive" like using namespace std helps programming easier. Without "using directive", instead of cout and endl, std::cout and std::endl should be written as the iostream library are within std namespace.
Comment Comments are not compiled or executed in the program as code.
Main Function This is the important entry point of every C++ program. A standard C++ program should have one named main function. Otherwise, the compiler will not compile the program. In an IDE, it could be generated through autotyping.
int main() {
}
Statement These are actions that the program execute when it runs. Each statement should be terminated by a semicolon. In the above program, the statements are as follows:
    cout << "Hello, enjoy fun!!" << endl;
    return 0;

In main function, return 0; statement is indispensable as return 0; means the program would return a zero integer value to the operating system. In some IDE's, when you type int main(), it is generated automatically.

So congratulations on making your first program "Hello, enjoy fun!" run as an application!

Footnotes and references[edit | edit source]

  1. The Eclipse CDT Project provides C and C++ Integrated Development Environment based on the Eclipse platform.