csc240 -lecture_3

47
CSC240 INTRODUCTION TO PROGRAMMING – I Mr. Dilawar Lecturer, Department of Computer Science, Jahan University Kabul, Afghanistan.

Upload: ainuddin-yousufzai

Post on 21-Mar-2017

63 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Csc240  -lecture_3

CSC240INTRODUCTION TO PROGRAMMING – I

Mr. Dilawar

Lecturer,Department of Computer Science,

Jahan UniversityKabul, Afghanistan.

Page 2: Csc240  -lecture_3

Previous Lecture Outline• Understanding the concept of flowcharts.

• Working with sequence, decision, repetition and case control flowchart structures.

• Understanding algorithms.

Page 3: Csc240  -lecture_3

Lecture Outline• Introduction to C++ and other popular programming languages

• Historical development of C++

• Writing C++ program

• Structure of C++ program

Page 4: Csc240  -lecture_3

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

Page 5: Csc240  -lecture_3

Other Popular Programming Languages

Page 6: Csc240  -lecture_3

Other Popular Programming Languages

Page 7: Csc240  -lecture_3

Other Popular Programming Languages

Page 8: Csc240  -lecture_3

Other Popular Programming Languages

Page 9: Csc240  -lecture_3

Other Popular Programming Languages

Page 10: Csc240  -lecture_3

Other Popular Programming Languages

Page 11: Csc240  -lecture_3

Other Popular Programming Languages

Page 12: Csc240  -lecture_3

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.

Page 13: Csc240  -lecture_3

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.

Page 14: Csc240  -lecture_3

Writing C++ Program

Page 15: Csc240  -lecture_3

Writing C++ Program

Page 16: Csc240  -lecture_3

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.

Page 17: Csc240  -lecture_3

Writing C++ Program

Page 18: Csc240  -lecture_3

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.

Page 19: Csc240  -lecture_3

Writing C++ Program• Popular IDEs • Microsoft® Visual Studio 2012 Express Edition• Dev C++• NetBeans• Eclipse• Apple’s Xcode• CodeLite

Page 20: Csc240  -lecture_3

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.

Page 21: Csc240  -lecture_3

Writing C++ Program

Page 22: Csc240  -lecture_3

Writing C++ Program• In Phase 3, the compiler translates the C++ program into machine-

language code—also referred to as object code.

Page 23: Csc240  -lecture_3

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.

Page 24: Csc240  -lecture_3

Writing C++ Program

Page 25: Csc240  -lecture_3

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.

Page 26: Csc240  -lecture_3

Writing C++ Program

Page 27: Csc240  -lecture_3

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.

Page 28: Csc240  -lecture_3

Writing C++ Program

Page 29: Csc240  -lecture_3

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.

Page 30: Csc240  -lecture_3

Writing C++ Program

Page 31: Csc240  -lecture_3

Structure of C++ Program • Preprocessor Directives• Header Files

• main() function

• Body of main() function• Statements

Page 32: Csc240  -lecture_3

Structure of C++ Program

Page 33: Csc240  -lecture_3

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.

Page 34: Csc240  -lecture_3

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;

}

Page 35: Csc240  -lecture_3

Structure of C++ Program

Page 36: Csc240  -lecture_3

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;

}

Page 37: Csc240  -lecture_3

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;

}

Page 38: Csc240  -lecture_3

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;

}

Page 39: Csc240  -lecture_3

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.

Page 40: Csc240  -lecture_3

Structure of C++ Program

Page 41: Csc240  -lecture_3

Structure of C++ Program

Page 42: Csc240  -lecture_3

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;

}

Page 43: Csc240  -lecture_3

Structure of C++ Program

Page 44: Csc240  -lecture_3

Example#include<iostream> //Preprocessor Directiveusing namespace std;int main(){ //main() function

cout<<“Hello world”; //statementsreturn 0;

}

Page 45: Csc240  -lecture_3

Escape Characters• They can be used as a separate characters or embedded in string

constant.

Page 46: Csc240  -lecture_3

Summery• Introduction to C++ and other popular programming languages

• Historical development of C++

• Writing C++ program

• Structure of C++ program

Page 47: Csc240  -lecture_3

Thank YouFor your Patience