Topic 1: Introduction to C++ Program

What is C++?

C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and more. C++ supports different programming paradigms like procedural, object-oriented, and functional programming, making it both powerful and flexible.

Understanding the IDE

An Integrated Development Environment (IDE) is a software application that provides essential tools for software development, including an editor and compiler. Here are some common features of an IDE:

  • Editor: A text editor for writing code.
  • Compiler: A tool that converts source code into executable code.
  • Debugger: A tool for testing and debugging programs.
  • Project Manager: A tool for managing files and projects.

Setting Up Your Development Environment

Before you start writing C++ programs, you need to set up your development environment. Here are the steps:

  • Download and install a C++ IDE. For this tutorial, we will use Dev C++, which you can download from www.bloodshed.net.
  • Install the IDE by following the installation instructions on the website.
  • Once installed, open the IDE and familiarize yourself with the interface.

Writing Your First Program - Hello World Program

  1. Start Dev-C++ IDE: Go to START → Bloodshed Dev-C++ and choose to open Dev-C++.
  2. Create a new source file: Click the toolbar button or go to File → New→ Source File.
  3. Save the file: Go to File → Save As and choose an appropriate location.
  4. Type the following C++ code. Ensure you write the code exactly as shown below:
  5. Hello World Program
    Figure 1.1: Hello World Program
  6. Compile the program: Choose Execute → Compile (or press F9). Fix any errors if they occur.
  7. Run the program: Choose Execute → Run (or press F10).

Note: You can combine steps 5 and 6 by pressing F11.

Output
  • Hello World!

Understanding the Hello World Program

Preprocessor Directives

The #include directive allows you to merge the source code in one file with another file. The #include <iostream> directive is required for using the cin and cout stream objects. It is not a statement, so no semicolon is needed at the end.

The using directive tells the compiler where to find definitions of C++ keywords and classes. The using namespace std; directive indicates that the definitions of standard C++ keywords and classes are located in the std namespace. It is a statement, so a semicolon is required at the end.

...
Figure 1.2

The main() Function

The main function is the driver function that controls all other functions in the program. It consists of the following components:

  • Header: Contains the data type returned by the function (int), the function name (main), and the arguments or parameters (an empty list).
  • Body: Statements inside the function, enclosed in braces. Each statement must end with a semicolon.
  • Return: A keyword causing the appropriate value to be returned from the function. A return 0 in the main function ends the program.

The compiler saves object code in object files with the extension .obj. The linker combines .obj files with other machine code necessary to run the program, producing an executable file with the extension .exe.

...
Figure 1.3

Basic Concepts in C++

Variables and Data Types

In C++, a variable is a storage location, identified by a memory address and a symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The C++ language has several basic built-in data types:

  • int: Represents integers (whole numbers).
  • float: Represents floating-point numbers (numbers with a decimal point).
  • double: Represents double-precision floating-point numbers.
  • char: Represents single characters.
  • bool: Represents boolean values (true or false).

Operators

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Here are some of the most common operators in C++:

  • Arithmetic Operators: + - * / %
  • Relational Operators: == != > <>= <=
  • Logical Operators: && || !
  • Assignment Operators: = += -= *= /= %=

Conditional Statements

Conditional statements allow you to control the flow of your program based on certain conditions. The most common conditional statements in C++ are if, else if, and else.

Loops

Loops allow you to execute a block of code repeatedly. The most common loops in C++ are for, while, and do-while.

Functions

Functions are blocks of code that perform a specific task. They allow you to break your program into smaller, modular pieces. A function in C++ is defined with the following syntax:

return_type function_name(parameters) 
{
    // code to be executed
}
                

Practice Questions

  1. What is C++ and who created it?
  2. Write a simple C++ program that prints "Welcome to C++ Programming!" on the screen.
  3. Explain the use of the #include <iostream> directive in a C++ program.
  4. What is the purpose of the main() function in a C++ program?
  5. Describe the purpose of the return 0; statement in the main() function.
Coming Soon...