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

C++ Lecture 1

C++ notes

Uploaded by

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

C++ Lecture 1

C++ notes

Uploaded by

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

Understanding and

Executing C++ Program

Course Lecturer:
DR ABDUL-WAHAB Nawusu Yakubu

ICT172: Principles to Programming


Lecture Notes
Objectives
After completing this lesson, the student should
be able to:
i. Give a brief history of the C++ Language
ii. Describe the steps in executing a C++ program
iii. Describe the basic structure of a C++ program

ICT172: Principles to Programming


Lecture Notes 2
Very Brief History of C++
• What is C++?
▪C++ is a high level programming language
• Which year was C++ developed?
▪1979 (thereabout)
• Who is the inventor of the C++ language?
▪Bjarne Stroustrup
• Where was C++ developed?
• Bell Laboratory in the USA
• From which programming language does the C++ evolve
from?
▪C++ (Originally called c with classes) is extension of the
C language

ICT172: Principles to Programming


Lecture Notes 3
Very Brief History of C++
•C++ has been used to develop holly or part of many
popular desktop and we applications:

•Adobe Photoshop & Illustrator


•YouTube
•Amazon.com
•Windows OS
•Microsoft Office
•MySQL
•Mozilla Firefox

ICT172: Principles to Programming


Lecture Notes 4
The need for compilation
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
return 0;
}

•Because a computer can understand only machine


language, a program written in C++ must be translated.

•The steps required to execute a program written


in C++ is discussed below.
ICT172: Principles to Programming
Lecture Notes 5
Processing C++ Program
This involves;
1. Editing (Coding)
2. Preprocessing
3. Compiling
4. Linking
5. Loading
6. Executing

ICT172: Principles to Programming


Lecture Notes 6
Step 1: Editing
• A text editor or an IDE is used to create a
C++ program following the rules, or
syntax, of the language.
• This program is called the source code, #include<iostream>
or source program.

• The program must be saved in a text file


that has the extension .cpp

Example:
If you saved a C++ program in the file
named first, then its complete name is
first.cpp.

ICT172: Principles to Programming


Lecture Notes 7
Step 2: Preprocessing
• Preprocessing involves processing statements #include<iostream>

known as preprocessor directives before actual


compilation begins.
• Preprocessor directives are statements that begin
with #. Example: #include<iostream> First.cpp

• Preprocessing thus allows the inclusion of header


files, the definition of symbolic constants,
conditional compilation, conditional execution
and inclusion of some library routines.
• Preprocessor directives are not C++ statements,
so they do not end in a semicolon (;).
• Preprocessing is done by a system program
called preprocessor.

ICT172: Principles to Programming


Lecture Notes 8
Step 3: Compiling
• After processing preprocessor directives, the
next step is to compile the source program
#include<iostream>

into the equivalent machine language


version. This is done by a compiler.
First.cpp
• The compiler first checks the source program
for syntax errors and, if no error is found,
translates the program into the equivalent
machine language.

• The translated version of a source program is


called an object program.

• A compiler is a system program that analyzes


a source program and then translates it into
a form that is suitable for execution on a
target machine.

ICT172: Principles to Programming


Lecture Notes 9
Step 4: Linking
• The C++ IDE contains prewritten codes
(called, library functions) to display the #include<iostream>

results of the program and perform several


mathematical functions to make the
programmer’s job somewhat easier. First.cpp

• This prewritten code (program) resides in a


place called the library.

• After a successful compilation, you must


still bring the code for the resources used
from the IDE into your program to
produce a final program that the
computer can execute.

ICT172: Principles to Programming


Lecture Notes 10
Step 5: Loading
• Programs run from the main memory – RAM.

• You must next load the executable program into main


memory for execution.

• A program called a loader loads an executable program into


main memory.

ICT172: Principles to Programming


Lecture Notes 11
Step 6: Executing
•The final step is to execute the program.

ICT172: Principles to Programming


Lecture Notes 12
The Compile & Run Button in Dev-C++

Compile Run Compile & Run


ICT172: Principles to Programming
Lecture Notes 13
Installing and using the
C++ IDE

ICT172: Principles to Programming


Lecture Notes 14
Practice installing the DEV C++ IDE
•Verifying the installation: Enter and run the
following codes in your installed IDE.

#include<iostream>
int main(){
std::cout<<“Amaraaba, TaTU!”;
return 0;
}

ICT172: Principles to Programming


Lecture Notes 15
Integrated Development Environment (IDE)
•An integrated development environment (IDE) is an
application used to create software.

•An IDE may support different programming languages.

•IDE have a number of different tools and functions that


assist a developer to create a program

ICT172: Principles to Programming


Lecture Notes 16
Code Editor
• The code editor is a text editor area that allows developers to
write, edit and save a source file. It has features that assist with
the writing and editing of codes.
• Auto-completion (or code-completion). This is designed to
save time while writing code. As you start to type the first part
of a function, it makes suggestion to speed up code entry.
• Bracket matching. Brackets are used to mark out blocks of
code. If you forget to close a bracket while writing, bracket
matching will help you to detect missing brackets.
• Syntax checks. This recognizes incorrect use of syntax and
highlights any errors.
• Runtime environment. This allows you can execute the
program one step at a time. This is useful to test that the code
is working line by line before creating the final complete
program.
ICT172: Principles to Programming
Lecture Notes 17
The Editor area for Dev C++

ICT172: Principles to Programming


Lecture Notes 18
Other IDE tools
• Translator. This compiles or interprets the code.

• Auto documentation. This explains the function and purpose of


the code, eg by noting the modules and variables used, and
its expected behaviour, and collates this into a text file that
can be used by other developers to understand how and why
the code was created.

• Libraries. These provide functions that are not included in the


core part of the programming language.
▪These functions can be imported and used at the start of
the program code.

ICT172: Principles to Programming


Lecture Notes 19
Other IDE tools
• Debugger. This is a program within the IDE that is used to
detect errors. If the debugger detects errors, it may suggest
what the type of error is and what line it is on.

• Errors Logs
The error log displays all the error found in a source code.
There are different types of errors, or bugs, which can
prevent computer programs from working in the way they
should.

ICT172: Principles to Programming


Lecture Notes 20
Types of Errors
Runtime errors
• Runtime errors are errors which will cause the program or computer to crash
even if there appears to be nothing wrong with the program code. Running
out of memory will often cause a runtime error. This could be because
instructions have been written in the wrong order.
Syntax errors
• Syntax errors are mistakes in the way that the code is written. Translators can
only execute a program if it is syntactically correct. Common syntax errors
include spelling mistakes, incorrect use of punctuation and the use of capital
letters for keywords.
Semantic errors
• Semantic errors, or logical errors, are those where the program works but
produces different results from what you designed or expected. A program
with semantic errors will execute without any errors being reported.

ICT172: Principles to Programming


Lecture Notes 21
Basic Structure of a C++
program

ICT172: Principles to Programming


Lecture Notes 22
Basic Structure of C++ Program
#include <iostream>
using namespace std;
int main(void) {

//Program statements
return 0;
}

ICT172: Principles to Programming


Lecture Notes 23
C++ Program Structure
#include <iostream>

• This statement is a Preprocessor directive.

• It tells the compiler and the linker that the program will need
to be linked to an external library of routines that handle
input from the keyboard and output to the screen
(specifically the cin and cout statements).

• The <iostream> is a header file that contains definition of


routines that handle inputs from the keyboard and output to
the screen.
ICT172: Principles to Programming
Lecture Notes
C++ Program Structure
using namespace std;
• A namespace permits grouping of various entities like classes,
objects, functions, and various C++ tokens, under a single name.
• The statement above notifies the compiler that the include files
to be used resides in the standard namespace (std).
• Additionally, this statement allows you to use objects such as the
cout and endl without the prefix std::
• It means that if you do not include this statement, then cout
should be used as std::cout and endl should be used as std::endl.

ICT172: Principles to Programming


Lecture Notes 25
C++ Program Structure
•using namespace std;
• A namespace permits grouping of various entities like classes,
objects, functions, and various C++ tokens, etc. under a single
name.
• In the above snippets, namespace std contains declarations for
cout, cin, endl, etc. statements.
• This includes things like cout, cin, string, vectors etc.
• This particular using directive says the program will be using names
that have a meaning defined for them in the std namespace (in this
case the iostream header defines meanings for cout and cin in the std
namespace).

ICT172: Principles to Programming


Lecture Notes 26
The main method
• Every C++ program must contain a main function.
• The main function is the first function called when you run your
program.
• In its simplest form, a C++ main function is given by:
int main(void)
{
……..
}
• The keyword int to the left of main indicates that the main
method “returns” an integer (whole number) value.
• The void in parentheses here means that main does not receive
any information from other functions.
ICT172: Principles to Programming
Lecture Notes 27
The main method
•A left brace, {, begins the body of the main method.

•A corresponding right brace, }, ends the body of the


main function.

•This pair of braces and the portion of the program


between the braces is called a block.

•The main( ) function can be anywhere in the program


but in general practice it is placed in the first position.

ICT172: Principles to Programming


Lecture Notes 28
The return statement
•The return 0 tell the compiler that your C++ program
ends there.

•It is the last executable statement of the main function.

•A value 0 indicates that the program terminated


normally and that no error occurred during program
execution.

ICT172: Principles to Programming


Lecture Notes 29
Comments
//Program statements

• The statement above is a comment.

• Comments are for documentation purposes.

• Comments help explain the program statements such


that programmers not involved in writing a particular
program can read, understand and to make corrections
if needed.

ICT172: Principles to Programming


Lecture Notes 30
When to add a comment
•Part of good programming is the inclusion of
comments in the program for such things as:
▪The programmer’s name
▪The date of the current version of the program
▪A brief description of what the program does.
▪Explain the meaning of key statements in a program.

• Note: Comments are for the reader, not for the compiler.

• So when a compiler compiles a program to check for the


syntax errors, it ignores comments.

ICT172: Principles to Programming


Lecture Notes 31
Types of Comments in C++
•There are two common types of comments in a C++
program:

i. Single-line comment
ii. Multiple-line comment

ICT172: Principles to Programming


Lecture Notes 32
Single-Line Comments
•Single-line comments begin with // and can be placed
anywhere in a line.
▪Everything encountered in that line after // is ignored
by the compiler.
▪For example, consider the following statement:
▪cout << "7 + 8 = " << 7 + 8 << endl;
▪You can put comments at the end of this line as
follows:
▪cout << "7 + 8 = " << 7 + 8 << endl; //prints: 7 + 8 = 15

ICT172: Principles to Programming


Lecture Notes 33
Multi-Line Comments
•Multiple-line comments are enclosed between /* and
*/.
▪The compiler ignores anything that appears
between /* and */.

▪For example, the following is an example of a


multiple-line comment:
/*
You can include comments that can occupy several lines.
*/

ICT172: Principles to Programming


Lecture Notes 34
Multi-Line Comments
The screen (Monitor) is the standard C++ output
stream while the keyboard is the input stream.

•Rewrite the above statement as a comment in


C++.

ICT172: Principles to Programming


Lecture Notes 35
End of Lecture

ICT172: Principles to Programming


Lecture Notes 36

You might also like