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

Chapter 5

The document summarizes key concepts about creating and compiling C++ programs. It discusses the 5 phases of editing, preprocessing, compiling, linking, and loading source code. It describes how the preprocessor, compiler, linker, and loader work to translate human-readable source code into executable machine code. The document also covers common error types, integrated development environments for C++, basic program structure, comments, I/O streams, and basic data types and operators.

Uploaded by

Esmael Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views27 pages

Chapter 5

The document summarizes key concepts about creating and compiling C++ programs. It discusses the 5 phases of editing, preprocessing, compiling, linking, and loading source code. It describes how the preprocessor, compiler, linker, and loader work to translate human-readable source code into executable machine code. The document also covers common error types, integrated development environments for C++, basic program structure, comments, I/O streams, and basic data types and operators.

Uploaded by

Esmael Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter Five

Fundamentals of C++ Programming Language

COMPUTER PROGRAMMING- CHAPTER FIVE 1


Mechanics of Creating a Program (C++)
o C++ programs typically go through five-phase edits, preprocess, compile, linking, load:
oEditor(edit): accepts the typing of the source code (or header file).
oSource file: the file that contains the program you prepared in the editor after you save it.
oHeader file: header files such as iostream
oPreprocessor: performs preliminary operations on files before they are passed to the compiler.
oCompiler(compile): translates the source code to machine language.
oThe 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.

COMPUTER PROGRAMMING- CHAPTER FIVE 2


oObject code: the file containing the translated source code. (.obj) The archiver takes a
collection of object files as input and produces a static library
oLinker: links the object file with additional code, such as the library codes.
oExecutable code(load): the loader takes the executable file from disk and transfers it
to memory.

COMPUTER PROGRAMMING- CHAPTER FIVE 3


Compiling Programs
o Any program written in a language other than machine language needs to be translated into machine
language .
oThe set of instructions that do this task are known as translators.
oDifferent kinds of translator software among that compiler and interpreters are of interest for most
programmers
o compiler is a computer program that translates a series of statements written in source code
oA compiler changes or translates the whole source code into executable machine code (also called
object code)
E.g., C++, Pascal, FORTRAN, etc.
oInterpreters: is a computer program that translates a single high-level statement and executes it and then
goes to the next high-level language line etc. E.g.,QBASIC, Lisp, etc.
. COMPUTER PROGRAMMING- CHAPTER FIVE 4
Errors in C++
The most common errors occure during writing C++ programs broadly classified as follows
 Programming (syntax) errors: These are generated when typographical errors are made by programmers.
Compiler errors: These are errors detected by the compiler that makes the program un-compliable.
Linker error: These are errors generated when the executable programs cannot be generated. This may be due to
wrong function prototyping, incorrect header files
Execution error: These errors occur at the time of execution. Looping and arithmetic errors fall under this
category
Logical error: these errors solely depend on the logical thinking of the programmer and are easy to detect if we
follow the line of execution.

COMPUTER PROGRAMMING- CHAPTER FIVE 5


IDE Tools

Dev C++
 Visual C++
VisualAge C++
Code Blocks,

COMPUTER PROGRAMMING- CHAPTER FIVE 6


The Structure of C++ Programs
oProbably the best way to start learning a programming language is with a 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!

COMPUTER PROGRAMMING- CHAPTER FIVE 7


Cont..
// my first program in C++

All the lines beginning with two slash signs (//) are considered comments
and do not have any effect on the behavior of the program.

#include <iostream>

Sentences that begin with a pound sign (#) are directives for the preprocessor.
the sentence #include <iostream> tells the compiler's preprocessor to include the iostream standard header
file. This specific file includes the declarations of the basic standard input-output library in C ++,

COMPUTER PROGRAMMING- CHAPTER FIVE 8


Cont...
Namespace
using namespace std;

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.
int main ()

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.
Also, for that same reason, all C++ programs must have a main function. main’ is
followed by a pair of parentheses () because it is a function.

COMPUTER PROGRAMMING- CHAPTER FIVE 9


Cont..
cout << "Hello World";

This instruction does the most important thing in this program. cout is the standard output stream in C++ (usually
the screen), and the full sentence inserts a sequence of characters (in this case "Hello World") into this output
stream (the screen).

return 0;

The return instruction causes the main () function to finishing and returns the code that the instruction is
followed by, in this case, 0.

COMPUTER PROGRAMMING- CHAPTER FIVE 10


Comments
oComments are pieces of source code discarded from the code by the compiler.
They do nothing.
oTheir purpose is only to allow the programmer to insert notes or descriptions
embedded within the source code.
oC++ supports two ways to insert comments:
// line comment
/* block comment */

COMPUTER PROGRAMMING- CHAPTER FIVE 11


Cont..
We are going to add comments to our second program:
/* my second program in C++
with more comments */
#include <iostream>
using namespace std; int main ()
{
cout << "Hello World! "; // says Hello World!

cout << "I'm a C++ program"; // says I'm a C++


program return 0;
}

Hello World! I'm a C++ program

COMPUTER PROGRAMMING- CHAPTER FIVE 12


Semicolons & Blocks in C++:
oIn C++, the semicolon is a statement terminator. That is, each statement must be ended with
a semicolon.
o For example, the following are three different statements:
x = y;
y = y+1;
y+=1;
A block is a set of logically connected statements that are surrounded by opening and closing
braces. For example:
{
cout << "Hello World"; // prints Hello World return 0;
}
COMPUTER PROGRAMMING- CHAPTER FIVE 13
Cont..
oC++ does not recognize the end of the line as a terminator. For this reason, it
does not matter where on a line you put a statement. For example:
x = y;
y = y+1;
is the same as x = y; y = y+1;
C++ Basic Input/Output
C++ I/O occurs in streams, which are sequences of bytes.

COMPUTER PROGRAMMING- CHAPTER FIVE 14


I/O Library Header Files
I/O Library Header Files
Header File Function and Description

<iostream> This file defines the cin, cout, cerr, and clog objects, which correspond
to the standard input stream, the standard output stream, the un-buffered
standard error stream, and the buffered standard error stream,
respectively.

<iomanip> This file declares services useful for performing formatted I/O with so-
called parameterized stream manipulators, such as setw and set
precision.

<fstream> This file declares services for user-controlled file processing.

COMPUTER PROGRAMMING- CHAPTER FIVE 15


The Standard Output Stream (cout)
The cout is used in conjunction with the stream insertion operator, which is
written as << which are two less than signs as shown in the following example.
#include <iostream>
using namespace std;
int main ()
{
int year=2013;
cout << "Value of year is: " << year << endl;
}
output :Value of year is:2013

COMPUTER PROGRAMMING- CHAPTER FIVE 16


The Standard Input Stream (cin)
The cin is used in conjunction with the stream extraction operator, which is
written as >> which are two greater-than signs as shown in the following
example.
#include <iostream>
using namespace std;
int main () {
int phone;

cout << "Please enter your phone number: ";


cin >> phone;
cout << "Your phone is: " << phone << endl;
}
Please enter your phone: 910203040
Your phone is: 910203040

COMPUTER PROGRAMMING- CHAPTER FIVE 17


Constants, Keywords, Variables, Data Types and
Operators
Keywords (reserved words)
oReserved/Keywords have a unique meaning within a C++ program.
oThe reserved words, must not be used for any other purposes.
oAll reserved words are in lower- case letters.
Variable
odefine a variable as a portion of memory to store a determined value.
oA variable is a symbolic name for a memory location in which data can be stored
and subsequently recalled.
oVariables are used for holding data values so that they can be utilized in various
computations in a program.
COMPUTER PROGRAMMING- CHAPTER FIVE 18
Identifiers
oAn identifier is the name associated with a function or data object
and used to refer to that function or data object.
oAn identifier must:
 Start with a letter or underscore
Consist only of letters, the digits 0-9, or the underscore symbol _
Not be a reserved word

Note:The C++ language is "case sensitive", which means that an


identifier written in capital letters is not equivalent to another one
with the same name but written in small letters.
COMPUTER PROGRAMMING- CHAPTER FIVE 19
Data Type
oOur computer's memory is organized in bytes. A byte is the minimum amount of memory that
we can manage.
Data Types

Integer
bool
Unsigned
Signed

COMPUTER PROGRAMMING- CHAPTER FIVE 20


Variable Declaration
oTo use a variable in C++, we must first declare it specifying which of
the data types above we want it to be.
Syntax rule for declaring a variable is:
Datatype nameIdentifier;
Example
int a;
float myNumber;

COMPUTER PROGRAMMING- CHAPTER FIVE 21


C++ CONSTANTS/LITERALS
oConstants refer to fixed values that the program may not alter and they are called literals.
oConstants can be of any of the basic data types and can be divided into
Integer Literals

Floating-Point Literals

Boolean Literals

Characters Literals

String Literals

COMPUTER PROGRAMMING- CHAPTER FIVE 22


Defining Constants
o There are two simple ways in C++ to define constants:
Using #define preprocessor.
Using the const keyword.
The #define Preprocessor

COMPUTER PROGRAMMING- CHAPTER FIVE 23


Cont..
o
#include <iostream>
#define LENGTH 10
#define WIDTH 5 cout << NEWLINE;
return 0;
#define NEWLINE '\n'
}
using namespace std;
int main()
{ out put
int area; 50
area = LENGTH * WIDTH;
cout << area;

COMPUTER PROGRAMMING- CHAPTER FIVE 24


The const Keyword
syntax
const type variable = value;

#include <iostream>
using namespace std;
int main(){
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';

int area;area = LENGTH * WIDTH;

cout << area;


cout << NEWLINE;
return 0;}

COMPUTER PROGRAMMING- CHAPTER FIVE 25


C++ Operator
oAn operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.

Arithmetic Operators

Relational Operators

Logical Operators

Bitwise Operators

Assignment Operators

Misc Operators

COMPUTER PROGRAMMING- CHAPTER FIVE 26


Thank you !!!

27

You might also like