Chapter One C++
Chapter One C++
Computer Programming
Programming language
Programming language
The terms computer programs, software programs, or just programs
are the instructions that tells the computer what to do. Computer requires
programs to function, and a computer programs does nothing unless its
instructions are executed by a CPU.
Computer programming (often shortened to programming or coding)
is the process of writing, testing, debugging/troubleshooting, and
maintaining the source code of computer programs.
Computer programs (also known as source code) are often written by
professionals known as Computer Programmers (simply programmers).
Source code is written in one of programming languages.
• A programming language is an artificial language that can be used to
control the behaviour of a machine, particularly a computer.
Programming languages, like natural language (such as Amharic), are
defined by syntactic and semantic rules which describe their structure
and meaning respectively.
• E.g. C++, Java, Fortran, c#, python, etc are also the most modern
programming languages
• Language translators
• Depending on the language, the translator for high level languages is
either a compiler or an interpreter. However, code written using
assembly language is translated to machine language by a program
called assembler.
• The description of the algorithm must be finite. Algorithm cannot be open ended
• Step 1: start
• Step 6: Else
• Step 8: End if
• Step 9: Stop
• Flow chart Diagrams for algorithms
• The standard flowchart symbols and their meaning are given below.
• Example 1: Draw a flowchart to add two numbers (A and B).
Home Work
• Lines beginning with a hash sign (#) are directives for the pre-processor.
This one directs the pre-processor to add some predefined source code
to our existing source code before the compiler begins to process it. In
this case the directive #include <iostream> tells the pre-processor to
include the iostream standard file, which includes the declarations of the
basic standard input-output library in C++. Like printing to the display,
getting user input from the keyboard, and dealing with files.
• using namespace std;
• All the elements of the standard C++ library are declared within what is called a
namespace, the namespace with the name std. The two items our program needs to
display a message on the screen, cout and endl, have longer names: std::cout and
std::endl. This using namespace std directive allows us to omit the std:: prefix
and use their shorter names. This directive is optional, but if we omit it, we must
use the longer names. Listing 2.2 (simple2.cpp) shows how the longer names are
used. The name std stands for “standard,” and the using namespace std line
indicates that some of the names we use in our program are part of the so-called
“standard namespace
• Example2
#include <iostream>
int main() {
}
• int main() {
The return statement causes the main function to finish. return may be
followed by a return code (in our example is followed by the return
code 0). A return code of 0 for the main function is generally interpreted
as the program worked as expected without any errors during its
execution.
Comment
• Comments are parts of the source code disregarded by the compiler. They
simply do nothing. Their purpose is only to allow the programmer to insert
notes or descriptions embedded within the source code.
• The first of them, known as line comment, discards everything from where
the pair of slash signs (//) is found up to the end of that same line.
• The second one, known as block comment, discards everything
between the /* characters and the first appearance of the */ characters,
with the possibility of including more than one line. We are going to
add comments to our second program:
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default,
delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float,
for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private,
protected, public, register, reinterpret_cast, return, short, signed, sizeof, static,
static_cast, struct, switch, template, this, throw, true, try, typedef, typeid,
typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
• Here are some examples of valid and invalid identifiers:
computer has to know what kind of data we want to store in them, since it is not going
to occupy the same amount of memory to store a simple number than to store a single
letter or a large number, and they are not going to be interpreted the same way.
• Data Type: a type which is established when the variable is defined. (e.g. integer, real,
character etc). Data type describes the property of the data and the size of the reserved
memory
C++ Fundamental Data Types
• The table below shows the fundamental data types, their meaning, and
their sizes (in bytes):
Data Type Meaning Size (in Bytes)
int Integer 2 or 4
float Floating-point 4
char Character 1
bool Boolean 1
void Empty 0
Declarations of variables
- subtraction
* multiplication
/ division
% modulo
• Operations of addition, subtraction, multiplication and division
literally correspond with their respective mathematical operators. The
only one that you might not be so used to see is modulo; whose
operator is the percentage sign (%). Modulo is the operation that gives
the remainder of a division of two values. For example, if we write:
!= Not equal to
• Group the binary digits in triplets (three bits) starting at the binary
point to the right and to the left and convert each of these to its octal
equivalent
• you can add 0’s on the right for fractional part and left for the integral
part, if necessary, to form a complete triplet
Example: Convert the following binary numbers to octal
• Octal-to-Hexadecimal This is equivalent to converting octal-to-binary
and then to-hexadecimal
• Example: convert (1076)8 to hexadecimal