Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CPP-Lecture-02 e

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Programming Fundamentals

(Using C++)
Huma Israr
Department Of Information Engineering Technology
National Skills University, Islamabad.
huma.israr@nsu.edu.pk
Fall 2023, BS-CS
2

Week 02

Lecture – 01
Lecture – 02

Semester Calendar
3

Class Schedule

Lab Hours (Practice)

Monday (11:00 to 01:00)


Basic Program Construction
 Compilers take source code and transform it into executable
files, which your computer can run as it does other programs.
 Source files are text files (extension .CPP)
 Executable files have the .EXE extension
IDE:

IDE Advantages:

 Handles build process for you


 Syntax highlighting and live error detection
 Code completion (fills in as you type)
 Creation of files via templates
 Built-in debugging
 Code refactoring (ex. Change a variable name everywhere in
your code)
 Higher productivity than plain text editors!
Edit
 The first phase consist of editing a file.
 The programmer types a C++ program with editor.
 The program source file is then stored on
secondary storage device such as disk.
 C++ program file names often end with .cpp
Compile
 Next, the programmer gives the command to compile the
program.
 Use the compiler to:
 Check that the program obeys the language rules
 Translate into machine language (object program)

 In C++ system
 A preprocessor program executes automatically before the
compiler’s translation phase begins.
 Preprocessor obeys commands called preprocessor
directives.
 Indicates that certain manipulations are to be performed on the program
before compilation.
Linking:
 The preprocessor is invoked by the compiler before the
program is converted to machine language.
 The next phase is called linking.
 A linker links the object code with the code for the missing functions to
produce an executable image.

 If the program compiles and links correctly, an executable


image is produced.
 Linker:
 Combines object program with other programs provided by the SDK to create
executable code
 Library: contains prewritten code you can use
Loader:

 The next phase is called loading.


 Before a program can be executed, the program
must first be placed in memory. This is done by the
loader, which takes the executable image from disk
and transfers it to memory
Execute:
 Finally, the computer executes the program one instruction
at a time.
 Some IDEs do all this with a Build or Rebuild command
A Very Brief History of C++
History of C and C++
 History of C
 Evolved from two other programming languages
 BCPL and B
 “Typeless” languages
 Dennis Ritchie (Bell Laboratories)
 Added data typing, other features
 Development language of UNIX
 Hardware independent
 Portable programs
 1989: ANSI standard
 1990: ANSI and ISO standard published
 ANSI/ISO 9899: 1990
History of Computer Languages:
 History of C++
 Extension of C
 Early 1980s: Bjarne Stroustrup (Bell Laboratories)
 Provides capabilities for object-oriented programming
 Objects: reusable software components
 Model items in real world
 Object-oriented programs
 Easy to understand, correct and modify
 Hybrid language
 C-like style
 Object-oriented style
 Both
15

Getting Started

Building Program
&
Compiling
Structure of C++ Program
Source Code Output

// My First C++ Program


#include<iostream>
using namespace std;
int main() ** Hello World **
{
cout<<“ ** Hello World **”;

return 0;
}
Components of C++ Program:
 Line 1: // My First C++ Program
 Lines Begin with two slash sign (//) are called comments by
the programmer and have no effect on the behavior of the
program .
 Programmer use them to include short observation or
explanation concerning the code or program.
 In this case, It is a brief introductory description of the
program.
 Note: One line Comment
Components of C++ Program (Conti……)
 Line 2: #include<iostream.h>
 Lines Begin with hash sign (#) are called pre-processor
directives or pre-compiler directives, include some standard
library header files
 In this case, the directive #include<iostream>, instruct the
pre-processor to include a section of standard c++ code ,
Known as header iostream , that allow standard input output
operations.
 iostream makes input and output available to us
 iostream standard header file that defines various types and
variables related to I/O
Components of C++ Program (Conti….)
 Line 3: using namespace std;
 legalizes" header contents” .
 Use the standard namespace
 Loads a namespace called std. Namespaces are used to
separate sections of code for programmer convenience. To
save typing we’ll always use this line in this course.
Components of C++ Program (conti….)
 Line 4: int main()
 int main() , begin function main()
 The line initiate the declaration of the main function.
 Essentially, a function is a group of instruction/statements
which are given name.
 In this case, gives the name “ main “ to the group of
statements/instructions that follows.
 The execution of all C++ programs begin with the main
function regardless of where the function is actually located
within the code.
Note: open and close braces { } mark the beginning and end of main function
Components of C++ Program (Conti…)
 Line 5: cout<<“ ** Hello World ** ”;
 Prints the message ** Hello World ** .
 cout is an instance of iostream
 cout is the object that writes to the stdout device, i.e. the
console window.
 It is part of the C++ standard library.
 Use with “using namespace std;”
 Operator << is used for output
 << is the C++ insertion operator. It is used to pass characters
from the right to the object on the left.
Components of C++ Program:
 Line 6: return 0;
 The return statement returns an integer value to the
operating system after completion.
 0 means “no error”. C++ programs must return an integer
value.

You might also like