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

CSC201 - Computer Programming 1 Lesson 2_120139

Uploaded by

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

CSC201 - Computer Programming 1 Lesson 2_120139

Uploaded by

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

UNIVERSITY OF DELTA, AGBOR.

FACULTY OF COMPUTING

COMPUTER SCIENCE DEPARTMENT


CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 1
COURSE CODE: CSC201

COURSE TITLE: COMPUTER PROGRAMMING 1

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 2


Lesson 2

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 3


C++ PROGRAMMING LANGUAGE
❖ C++ is a cross platform programming language which is used to
create high performance application and software like OS, games,
embedded system, Graphical User Interface (GUI) etc.
❖ C++ language was developed by Bjarne Stroustrup in 1979 at Bell
Laboratory in Murray Hill, New Jersey, as an enhancement to the C
language and originally named C with Classes but later it was
renamed C++ in 1983 .
❖ C++ is a statically typed (type checking is performed during
compile time as opposed to run time), compiled, general-purpose,
case-sensitive, free-form programming language that supports
procedural, object-oriented, and generic programming.
CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 4
❖ C++ is regarded as a middle-level programming language, since it
can accommodate both low-level and high-level language features.
❖ C++ is a superset of C, and that virtually any legal C program is a
legal C++ program.
❖ C++ fully supports object-oriented programming, including the four
pillars of object-oriented program development:
➢ Encapsulation
➢ Data hiding
➢ Inheritance
➢ Polymorphism

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 5


FEATURES OF C LANGUAGE
C++ is a general purpose programming language that was developed as an
enhancement of the C language to include an object-oriented paradigm. It is
an imperative and compiled language. C++ has a number of feature,
including:
❖ Object-Oriented programming
❖ Machine Independent
❖ Simple
❖ High-level language
❖ Case-sensitive
❖ Compiler based
❖ Dynamic Memory Allocation/Memory management
❖ Multi-threading
CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 6
GETTING STARTED WITH C++
C and C++ compilers are widely available. Download any one you
like. We're going to employ codeblocks here. It is compatible with C
and C++.
You require two things to begin using C++:
❖ A text editor to create C++ code, such as Notepad.
❖ A compiler like GCC to translate the C++ code into a language
that the computer can understand.
NOTE: For the purpose of this course, an IDE (Integrated
Development Environment) will be used.

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 7


DOWNLOAD AND INSTALL AN IDE
❖ An IDE (Integrated Development Environment) is used to edit and
compile the C++ code.
❖ Popular IDE's include Code::Blocks, Eclipse, and Visual Studio.
These are all free, and they can be used to both edit and
build/compile C++ code.
❖ We will use Code::Blocks in this course, which is a good place to
start.
❖ The most recent version of Code::blocks is available at
http://www.codeblocks.org/
❖ Install the text editor and compiler for Windows OS by
downloading the mingw-setup.exe file.
CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 8
CODE::BLOCKS
For individuals wishing to learn how to code or for those looking to
hone their skills, Code Blocks is a fantastic resource. It includes a
plethora of features and an intuitive user interface that make learning
to code simple.
Developers may code, debug, create, run, and deploy projects using
the free, open source and cross-platform C/C++ IDE (Integrated
Development Environment) called Code::Blocks. It offers strong
possibilities to personalize your development environments, including
source control integration and graphical representations of memory
and CPU consumption.

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 9


CODE::BLOCKS
When code::blocks is installed and run, the following screen appears

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 10


CODE::BLOCKS
To start a new project, click on “create a new project”

Click on “create a
new project”

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 11


❖ Select a wizard type on
the left.
❖ From the category list
that appears, select
console application.
❖ click on the “Go
button”

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 12


❖ click on the “Next button” ❖ Select C++, and click on the “Next
button”

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 13


❖ First, give the project a title
❖ Create a folder where you want
all C/C++ program file to be
stored.
❖ The project filename and
resulting filename are
automatically created with the
associated extensions.

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 14


❖ Select C/C++ compiler from the
compiler drop down menu. We shall
be using the GNU GCC Compiler by
default.
❖ Click Finish

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 15


A default “Hello World” program is created

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 16


Structure of a C program
In this lecture, we are going to learn about the basic structure of a C++
program. A C++ program is divided into six different sections, namely:
Documentation
Link section
Definition
Global declaration section
Function definition section
Main function
Basic structure of C++ program

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 17


Documentation Section
❖ This section comes first and is used to document the logic of the
program that the programmer going to code.
❖ It can be also used to write the purpose of the program.
❖ Whatever written in the documentation section is the comment and is
not compiled by the compiler.
❖ Documentation Section is optional since the program can execute
without them. Below is the snippet of the same:

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 18


Documentation Section
/* This is a C++ program to find the
factorial of a number

The basic requirement for writing this


program is to have knowledge of loops

To find the factorial of number


iterate over range from number to one
*/

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 19


Linking Section: The linking section contains two parts:
Header Files:
Standard headers are specified in a program through the preprocessor
directive #include – the iostream header is used. When the compiler
processes the instruction #include<iostream>, it includes the contents of
the stream in the program. This enables the programmer to use standard
input, output, and error facilities that are provided only through the
standard streams defined in <iostream>. These standard streams process
data as a stream of characters, that is, data is read and displayed in a
continuous flow.

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 20


Linking Section
Namespaces:
A namespace permits grouping of various entities like classes, objects,
functions and various C++ tokens etc under a single name.

Namespaces can be accessed in multiple ways:


❖ using namespace std;
❖ using std :: cout;

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 21


Definition Section
❖ It is used to declare some constants and assign them some value.
❖ In this section, you can define your own data type using primitive
data type.
❖ #define is a compiler directive which is used to define variable and
constants.
Global Declaration Section
❖ the variables and the class definitions which are going to be used in
the program are declared to make them global.
❖ The scope of the variable declared in this section lasts until the entire
program terminates.
These variables are accessible within the user-defined function also.
CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 22
Function Declaration Section
❖ It contains all the functions which our main functions need.
❖ Usually, this section contains the User-defined functions.
Main function section
❖ The main function tells the compiler where to start the execution of the
program.
❖ All the statements that are to be executed are written in the main
function.
❖ The compiler executes all the instructions which are written in the curly
braces { } which encloses the body of the main function.
❖ Once all instructions from the main function are executed, control comes
out of the main function and the program terminates and no further
execution occur.
CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 23
First C Program
Before starting with C++ language, you need to learn how to write,
compile and run the first C++ program.
To write the first C++ program, open the code::blocks C++ empty file
environment and type the following code:

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 24


❖ Save the file (name the file)
❖ Then, build (compile) and run the C++ program

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 25


Click this symbol to Build Click this symbol to Click this joint symbols to
or compile the program Run the program Build/Run the program

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 26


COMPILATION PROCESS IN C++ LANGUAGE
WHAT IS COMPILATION
The process of transforming source code into object code is known as
compilation. The compiler is used to assist in the process. If the source
code is free of syntactical or structural errors, the compiler generates
the object code.
An example may look like this
#include <iostream> 0100100000000000
using namespace std; 0111000000010000
int main() 0101010101011110
{ 0000000011111111
cout << "Welcome to CSC201 Class" << endl; 0000011111111111
cout << "Computer Programming 1" << endl; 0101010101011110
return 0;
} Object code
Source code

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 27


The input source code is transformed into object code or machine code
during the compilation process in C++. The four steps of the
compilation process are pre-processing, compilation, assembly, and
linking. The preprocessor removes all the comments from the source
code after receiving it as input. The preprocessor interprets the
preprocessor directive. For instance, if the program contains the
directive “iostream", the preprocessor will interpret it and replace it
with the contents of the “iostream" file.
Before being converted into an executable form, our program goes
through the following phases:

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 28


❖ Pre-processor

❖ Compiler

❖ Assembler

❖ Linker

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 29


Preprocessor
The source code is written with a text editor, and the file extension for
source code files is ".cpp“. The preprocessor receives this source code
first, and extends it after that. The code is enlarged after which it is
given to the compiler.

Compiler
The preprocessor passes the pre-expanded code to the compiler. This
code is changed into assembly language by the compiler. Alternately,
we may say that the preprocessed code is changed into assembly code
by the C++ compiler.

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 30


Assembler
A program called an assembler is used to translate assembly code into
object code. The source file's name is the same as the name of the
object file that the assembler creates.

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 31


Linker
The linker's primary task is to bring together the object code of library
files and the object code of our program. The circumstance can
occasionally happen when our application references to functions
specified in other files; in this case, the linker is crucial. It connects our
program's object code to that of these files. Consequently, we draw the
conclusion that the linker's responsibility is to join the object code of
our program with the object code of the library files and other files.
The executable file is what the linker produces as output. The only
difference between the executable and source files' names is their
extensions. The executable file's DOS extension is ".exe."

CSC201 - COMPUTER PROGRAMMING 1 WITH C++ 32

You might also like