0% found this document useful (0 votes)
80 views27 pages

University of Gondar Institute of Technology Department of Electrical and Computer Engineering

The document discusses the five phases of creating a C++ program: editing, preprocessing, compiling, linking, and loading. It describes each phase and covers related topics like header files, IDE tools, program structure, comments, data types, variables, operators, and examples of simple C++ programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views27 pages

University of Gondar Institute of Technology Department of Electrical and Computer Engineering

The document discusses the five phases of creating a C++ program: editing, preprocessing, compiling, linking, and loading. It describes each phase and covers related topics like header files, IDE tools, program structure, comments, data types, variables, operators, and examples of simple C++ programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

University of Gondar

Institute of Technology
Department of Electrical and Computer Engineering

By: Endalkachew D.
Chapter five
Fundamentals of C++ Programming Language
Mechanics of Creating a Program (C++)
C++ programs typically go through five-phase edits, preprocess, compile, linking,
load:
Editor(edit): accepts the typing of the source code (or header file).
Source file: the file that contains the program you prepared in the editor after you
save it.(.cpp)
Header file: header files such as iostream
The preprocessor: is a program that scans the source code for preprocessor
directives such as include directives.
Compiler(compile): translates the source code to machine language.
The compiler is another program that translates the preprocessed source code into
corresponding machine language instructions, which are stored in a separate file,
called an object file, having a .obj extension.
Phase of C++ program
IDE Tools
You can use any plain-text editor such as Notepad to write the source code. You also
can download a free compiler, which usually includes a preprocessor and linker.
There are several IDE tools to write a C++ program such as Code Blocks, Code
Warrior, Dev C++, Quency … etc.
The Structure of C++ Programs
Hello world program:
// my first program in C++
#include <iostream>
using namespace std;
int main ()// main () is where program execution begins.
{
cout << "Hello World!";
return 0;
}

Output:

Hello World!
Cont.…

This is a comment line.


All the lines beginning with two slash signs (//) are considered comments and do
not have any effect on the behavior of the program.
They can be used by the programmer to include short explanations or
observations within the source itself.
Cont.…

Sentences that begin with a pound sign (#) are directives for the preprocessor.
They are not executable code lines but indications for the compiler.
#include<iostream> tells the compiler's preprocessor to include the iostream
standard header file. Such as input and output standard files.
Cont.…

C++ uses namespaces to organize different names used in programs.


Every name used in the iostream standard library file is part of a namespace called
std.
Consequently, the cout object is called std::cout. The using namespace std;
statement avoids the need for putting std:: before every reference to cout, so we can
just use cout in our code.
Cont.…

This line corresponds to the beginning of the main function declaration.


The main function is the point by where all C++ programs begin their execution.
It is independent of whether it is at the beginning, at the end, or in the middle of the
code - its content is always the first to be executed when a program starts.
Cont.…

This instruction does the most important thing in this program.


cout is the standard output stream in C++
Notice that the sentence ends with a semicolon character (;).
This character signifies the end of the instruction and must be included after every
instruction in any C++ program
Cont.…

The return instruction causes the main () function to finishing


Comments
Comments are pieces of source code discarded from the code by the compiler. They
do nothing.
Their purpose is only to allow the programmer to insert notes or descriptions
embedded within the source code.
C++ supports two ways to insert comments:
Semicolons & Blocks in C++
In C++, the semicolon is a statement terminator.
That is, each statement must be ended with a semicolon.
It indicates the end of one logical entity.

A block is a set of logically connected statements that are surrounded by opening


and closing braces.
The Standard Output Stream (cout)
The predefined object cout is an instance of an ostream class.
The cout object is said to be "connected to" the standard output device, which
usually is the display screen.
The cout is used in conjunction with the stream insertion operator, which is written
as << which are two less than signs.

endl is used to add a new-line at the end of the line.


When the above code is executed the result will be
The Standard Input Stream (cin)
The predefined object cin is an instance of istream class.
The cin object is said to be attached to the standard input device, which usually is
the keyboard.
The cin is used in conjunction with the stream extraction operator, which is written
as >> which are two greater-than signs.
Cont.…
When the above code is compiled and executed, it will prompt you to enter a name.

To request more than one datum you can use the following:

This will be equivalent to the following two statements:


Constants, Keywords, Variables, Data Types and Operators
Keywords (reserved words)
Reserved/Keywords have a unique meaning within a C++ program.
These symbols, the reserved words, must not be used for any other purposes.
Data Type
When programming, we store the variables in our computer's memory, but the
computer must know what we want to store in them since storing a simple number,
a letter or a large number is not going to occupy the same space in memory.
Variable Declaration
Syntax rule for declaring a variable is:

Note: Variables must be declared before used!


You can create more than one variable of the same type in one statement by writing
the type and then the variable names, separated by commas. For example:
C++ Operator
C++ Operator
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. C++ is rich in built-in operators and provides the following
types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Examples
1. Write a C++ program which displays your name and your section in different lines
2. Define variables to represent the following entities:
Age of a person
The income of an employee
Several words in a dictionary
Letter of the alphabet
A greeting message
3. Write a C++ program number which obtains a natural number from the user and
display the number.
4. Write a program that takes two numbers from the user and determines that the first
number is a factor of the second number.
5. Write a C++ program that receives two integer values from the user. The program then
should print the sum (addition), difference (subtraction), product (multiplication),
quotient (division), and remainder after division (modulus).
2/8/2021 27

You might also like