0% found this document useful (0 votes)
18 views

Lesson 1 Introduction To C

Uploaded by

edwardpoley5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lesson 1 Introduction To C

Uploaded by

edwardpoley5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Introduction to

C++
GEADLITE – LIVING IN THE INFORMATION
TECHNOLOGY ERA
Understanding the
Programming a Computer
Programming can be referred to “a way of developing and executing
numerous sets of commands to let a computer implement different
tasks.”
Programming is often referred to as computer programming and involves
different activities or tasks like examination, accepting, discerning, and
disentangling problems.
Programming also refers to confirmation of requests of the set of rules
together with its exactness and its resource consumption, execution of
the algorithm in an objective programming language.
The algorithm is also known as a source code that is written by a
programmer and is fed into the machine for its execution and to see
possible results.
Characteristic of C++
Characteristics of C++ C++ is not a purely object-oriented language but a
hybrid that contains the functionality of the C programming language.

This means that you have all the features that are available in C:
• universally usable modular programs
• efficient, close to the machine programming
• portable programs for various platforms.
C++ Development
Environment
For Android Device
DEV C++
Dev-C++ Compiler Setup on
Windows OS
1. Download Dev C++ from: https://bloodshed-dev-c.en.softonic.com

2. Double click/ Open the executable file


Dev-C++ Compiler Setup on
Windows OS
3. wait for unpacking the data

4. Select language then click ok button


Dev-C++ Compiler Setup on
Windows OS
5. If you accept the terms of the agreement click I Agree button to
continue.
Dev-C++ Compiler Setup on
Windows OS
6. Select the type of install (FULL Version) then click Next Button
Dev-C++ Compiler Setup on
Windows OS
7. Select Destination Folder then Click Install Button
Dev-C++ Compiler Setup on
Windows OS
8. Wait while Dev C++ is being installed
Dev-C++ Compiler Setup on
Windows OS
9. Checked the Run Dev-C++ Checkbox to run the program then Click
Finish Button
Translating a C++ program
The following three steps are
required to create and translate a
C++ program:
The following three steps are required to create and translate a C++
program:
1. First, a text editor is used to save the C++ program in a text file. In
other words, the source code is saved to a source file. In larger
projects the programmer will normally use modular programming. This
means that the source code will be stored in several source files that
are edited and translated separately.
2. The source file is put through a compiler for translation. If everything
works as planned, an object file made up of machine code is created.
The object file is also referred to as a module.
3. Finally, the linker combines the object file with other modules to form
an executable file. These further modules contain functions from
standard libraries or parts of the program that have been compiled
previously.

It is important to use the correct file extension for the source file’s
name. Although the file extension depends on the compiler you use,
the most commonly found file extensions are .cpp and .cc.
Note
It is important to use the correct file extension for the source file’s
name. Although the file extension depends on the compiler you use,
the most commonly found file extensions are .cpp and .cc.
Getting Started - Write our First
Hello-world C++ Program
Step 1: Open Dev-C++ Application
• Double click the Dev-C++ icon or right click the
Dev-C++ icon then select open
Step 2: Create source File
• On menu bar click the File -> New -> Source File or used keyboard
technique Ctrl+ N
Getting Started - Write our First
Hello-world C++ Program
Step 3: Write the Source Code:
• Enter the following source codes using Interactive Development
Environment (IDE) (such as CodeBlocks, Eclipse, NetBeans or
Dev-C++).
• Save the source file as "hello.cpp". A C++ source file should be
saved with a file extension of ".cpp". You should choose a
filename which reflects the purpose of the program.
Getting Started - Write our First
Hello-world C++ Program
Example using DOSBOX
Getting Started - Write our First
Hello-world C++ Program
Step 4: Build the Executable Code:
• On IDE (such as Dev-C++), select Execute -> Compile or
simply press F9 button to compile
• On menu bar Click
Getting Started - Write our First
Hello-world C++ Program
Step 5: Run the Executable Code:
• Execute (Run) the program.:
• On IDE (such as Dev-C++), select Execute -> Run or press F10
Button.
Brief Explanation of the
Program
/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are
ignored by the compiler; but they provide useful explanation and
documentation to your readers (and to yourself three days later). There
are two kinds of comments:
◦ Multi-line Comment: begins with /* and ends with */. It may span more
than one lines (as in Lines 1-3).
◦ End-of-line Comment: begins with // and lasts until the end of the current
line (as in Lines 4, 7, 8, 9 and 10).
Brief Explanation of the
Program
#include <iostream>
using namespace std;
◦ The "#include" is called a preprocessor directive.
◦ Preprocessor directives begin with a # sign.
◦ They are processed before compilation.
◦ The directive "#include <iostream>" tells the preprocessor to include the
"iostream" header file to support input/output operations.
◦ The "using namespace std;" statement declares std as the default
namespace used in this program.
◦ The names cout and endl, which is used in this program, belong to the std
namespace. These two lines shall be present in all our programs. I will
explain their meaning later.
Brief Explanation of the
Program
int main() { ... body ... }
◦ defines the so-called main() function.
◦ The main() function is the entry point of program execution.
◦ main() is required to return an int (integer).
cout << "hello, world" << endl;
◦ "cout" refers to the standard output (or Console OUTput).
◦ The symbol << is called the stream insertion operator (or put-to operator),
which is used to put the string "hello, world" to the console.
◦ "endl" denotes the END-of-Line or newline, which is put to the console to
bring the cursor to the beginning of the next line.
Brief Explanation of the
Program
return 0;
◦ terminates the main() function and returns a value of 0 to the operating
system.
◦ Typically, return value of 0 signals normal termination; whereas value of non-
zero (usually 1) signals abnormal termination.
◦ This line is optional. C++ compiler will implicitly insert a "return 0;" to the
end of the main() function.
C++ Terminology and Syntax
Statement: A programming statement performs a piece of programming
action. It must be terminated by a semicolon (;) (just like an English
sentence is ended with a period), as in Lines 5, 8 and 9.

Preprocessor Directive: The #include (Line 4) is a preprocessor directive


and NOT a programming statement. A preprocessor directive begins with
hash sign (#). It is processed before compiling the program. A preprocessor
directive is NOT terminated by a semicolon - take note of this unusual rule.

Block: A block is a group of programming statements enclosed by braces { }.


This group of statements is treated as one single unit. There is one block in
this program, which contains the body of the main() function. There is no
need to put a semicolon after the closing brace.
C++ Terminology and Syntax
Comments: A multi-line comment begins with /* and ends with */, which
may span more than one line. An end-of-line comment begins with // and
lasts till the end of the line. Comments are NOT executable statements and
are ignored by the compiler; but they provide useful explanation and
documentation. Use comments liberally.

Whitespaces: Blank, tab, and newline are collectively called whitespaces.


Extra whitespaces are ignored, i.e., only one whitespace is needed to
separate the tokens. Nevertheless, extra white spaces and newlines could
help you and your readers better understand your program. Use extra
whitespaces and newlines liberally.

Case Sensitivity: C++ is case sensitive - a ROSE is NOT a Rose, and is NOT a
rose.
Example 2

In this case, we performed two insertions into cout in two different


statements. Once again, the separation in different lines of code has been
done just to give greater readability to the program, since main could have
been perfectly valid defined this way:
Example 2
We were also free to divide the code into more lines if we considered it
more
Example 3
Escape Sequences
Sample Program

You might also like