csc240 -lecture_3
TRANSCRIPT
CSC240INTRODUCTION TO PROGRAMMING – I
Mr. Dilawar
Lecturer,Department of Computer Science,
Jahan UniversityKabul, Afghanistan.
Previous Lecture Outline• Understanding the concept of flowcharts.
• Working with sequence, decision, repetition and case control flowchart structures.
• Understanding algorithms.
Lecture Outline• Introduction to C++ and other popular programming languages
• Historical development of C++
• Writing C++ program
• Structure of C++ program
Introduction to C++• C++ is the advanced version of C language that is used for developing
computer programs.
• C++ is a powerful programming language that fully supports• Procedural programming• Object-oriented programming• Data abstraction• Generic programming
Other Popular Programming Languages
Other Popular Programming Languages
Other Popular Programming Languages
Other Popular Programming Languages
Other Popular Programming Languages
Other Popular Programming Languages
Other Popular Programming Languages
Historical Development of C++ Language Programming Language Description
BCPL Language Developed in 1967 by Martin Richards as a language for writing operating systems and compilers for operating system.
B Language Developed by Ken Thompson at Bell Laboratories. The early version of UNIX operating system developed in 1970.
C LanguageDeveloped by Dennish Ritche in 1972 by Bell Laboratories. It is available for most computers and is hardware independent. Portable computer programs can be created.
C++ Language C++ is the extension of C developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories. It provides the capabilities of object-oriented programming.
Current versions of C++ such as Microsoft Visual C++ .NET and Borland C++ Builder.We will follow the syntax and rules of turbo C++ version 3.0 and Borland C++ v5 for MS DOS environment.
Writing C++ Program• C++ systems generally consist of three parts• A program development environment• The language• C++ Standard Library
• C++ programs typically go through six phases• Edit, Preprocess, Compile, Link, Load and Execute.
Writing C++ Program
Writing C++ Program
Writing C++ Program• Phase 1 consists of editing a file with an editor program, normally
known simply as an editor. • Type a C++ program (source code) using the editor.• Make any necessary corrections.• Save the program. • C++ source code filenames often end with the .cpp, .cxx, .cc or .C extensions
(note that C is in uppercase) which indicate that a file contains C++ source code.
Writing C++ Program
Writing C++ Program• Linux editors: vi and emacs.
• C++ software packages for Microsoft Windows such as Microsoft Visual C++ (microsoft.com/express) have editors integrated into the programming environment.
• You can also use a simple text editor, such as Notepad in Windows, to write your C++ code.
• Integrated Development Environments (IDEs)• Provide tools that support the software-development process, including editors for
writing and editing programs and debuggers for locating logic errors—errors that cause programs to execute incorrectly.
Writing C++ Program• Popular IDEs • Microsoft® Visual Studio 2012 Express Edition• Dev C++• NetBeans• Eclipse• Apple’s Xcode• CodeLite
Writing C++ Program• In phase 2, you give the command to compile the program. • A preprocessor program executes automatically before the compiler’s
translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). • The C++ preprocessor submits commands called preprocessing directives,
which indicate that certain manipulations are to be performed on the program before compilation. • These manipulations usually include other text files to be compiled, and
perform various text replacements.
Writing C++ Program
Writing C++ Program• In Phase 3, the compiler translates the C++ program into machine-
language code—also referred to as object code.
Writing C++ Program• Phase 4 is called linking. • The object code produced by the C++ compiler typically contains “holes” due
to some missing parts. • A linker links the object code with the code for the missing functions to
produce an executable program. • If the program compiles and links correctly, an executable image is produced.
Writing C++ Program
Writing C++ Program• Phase 5 is called loading. • Before a program can be executed, it must first be placed in memory. • This is done by the loader, which takes the executable image from disk and
transfers it to memory. • Additional components from shared libraries that support the program are
also loaded.
Writing C++ Program
Writing C++ Program• Phase 6: Execution• Finally, the computer, under the control of its CPU, executes the program
one instruction at a time. • Some modern computer architectures can execute several instructions in
parallel.
Writing C++ Program
Writing C++ Program• Problems That May Occur at Execution Time • Programs might not work on the first try. • Each of the preceding phases can fail because of various errors that we’ll
discuss throughout this course. • If this occurred, you’d have to return to the edit phase, make the necessary
corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). • There is also a standard error stream referred to as cerr. The cerr stream is
used for displaying error messages.
Writing C++ Program
Structure of C++ Program • Preprocessor Directives• Header Files
• main() function
• Body of main() function• Statements
Structure of C++ Program
Structure of C++ Program • The instructions that are given to the compiler before the beginning
of the actual program are called preprocessor directives.• Also known as compiler directives.• The preprocessor directives consist of instructions for the compiler.• The compiler adds special instructions or code from these directives into the
program at the time of compilation.• It starts with a # (HASH) or (SHARP) and the keyword include or define.
• They are used to include header files.
Structure of C++ Program • Header files in C++ are source files that
contains definitions of library functions/objects.• A header is added if function/object defined
in it is to be used in the program.• For example, the header file iostream.h has
the definitions of different built-in input/output functions. To include the header file into the C++ source code we normally write:
#include<iostream>int main(){
……………………return 0;
}
Structure of C++ Program
Structure of C++ Program • The C++ program is typically consist
of at least one function called main() function.
• When C++ program starts execution, main() is the first program that is executed.
• The keyword int to the left of main() indicates that main() “returns” an integer (whole number) value.
#include<iostream>int main(){
……………………return 0;
}
Structure of C++ Program • Each function may or may not
return a value back.
• The function contains body in which duties of the function is placed.
#include<iostream>int main(){
……………………return 0;
}
Structure of C++ Program • A statement instructs the computer to
perform an action.
• Together, the quotation marks and the characters between them are called a string, a character string or a string literal.
• Most C++ statements end with a semicolon (;), also known as the statement terminator.
• C++ is a case-sensitive language.
#include<iostream>int main(){
……………………return 0;
}
Structure of C++ Program • You use blank lines, space characters and tab characters (i.e., “tabs”)
to make programs easier to read.• Together, these characters are known as white space.• White-space characters are normally ignored by the compiler.
Structure of C++ Program
Structure of C++ Program
Structure of C++ Program • Comments can be used for
understanding the source code.
• Single-line (//…………..) comment vs multi-line (/*……..*/) comment.
//sample program#include<iostream>int main(){
……………………return 0;
}
Structure of C++ Program
Example#include<iostream> //Preprocessor Directiveusing namespace std;int main(){ //main() function
cout<<“Hello world”; //statementsreturn 0;
}
Escape Characters• They can be used as a separate characters or embedded in string
constant.
Summery• Introduction to C++ and other popular programming languages
• Historical development of C++
• Writing C++ program
• Structure of C++ program
Thank YouFor your Patience