1 / 18

PROGRAMMING LANGUAGE

PROGRAMMING LANGUAGE. Lecture #5 Introduction to C++. By Shahid Naseem (Lecturer). LECTURE OUTLINES. LECTURE OUTLINES. Introduction to C++ Structure of C++ Program Preprocessor Directives Header files Keywords Tokens Data Types in c++ Constants. INTRODUCTION OF C++.

jaron
Télécharger la présentation

PROGRAMMING LANGUAGE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PROGRAMMING LANGUAGE Lecture #5 Introduction to C++ By ShahidNaseem (Lecturer)

  2. LECTURE OUTLINES

  3. LECTURE OUTLINES • Introduction to C++ • Structure of C++ Program • Preprocessor Directives • Header files • Keywords • Tokens • Data Types in c++ • Constants Introduction of Visual Basic (Civil Engineering Department)

  4. INTRODUCTION OF C++ • C language was developed in 1970 by Dennis Ritche. In C language, new features like data typing and many other functions were introduced. C language was developed as a computer independent language. A program written in C language can be run on any computer. • In 1980, C++ was developed by BjameStroustrup. It was an extension of C. C++ enable programmers to improve the quality of code. It included object oriented programming. • The source code of a C++ program is stored on the disk with file extension CPP. (CPP stands for c plus plus). • The Borland C++ and Turbo C++ have their own editor for writing the source code. The C++ compiler translates a C++ source program into the machine code. • The C++ compiler translates a C++ source program into the machine code. The machine code is called object code. Introduction of Visual Basic (Civil Engineering Department)

  5. STRUCTURE OF C++ PROGRAMS • A C++ program consists of three main parts. These are: • Preprocessor Directives • The main () function • C++ Statements. Introduction of Visual Basic (Civil Engineering Department)

  6. PREPROCESSOR DIRECTIVES • The instructions that are given to the compiler before the beginning of the actual program are called “Preprocessor Directives”. These are also known as “Compiler Directives”. These pre-processor directives consist of instructions for the compiler. The compiler adds special instructions or code from these directives into the program at the time of compiler. • These pre-processor directives normally start with a number sign (#) and the keyword “include” or “define”. Preprocessor directives are used to include header files in the program. #include <iostream.h> Main () { Cout<<” This is my first program”; } Introduction of Visual Basic (Civil Engineering Department)

  7. HEADER FILE • Header file is a C++ source file that contains definitions of library functions/objects. Header files are added into the program at the compilation of the program. A header file is added if the function/object defined in it is to be used in the program. The main () function • The “main ()” function indicates the beginning of a C++ program. The main () must be included in every C++ program. When a C++ program is executed, the control goes directly to the main () function. • The statement within this function are the main body of the C++ program. If main () function is not included, the program is not compiled and an error message is generated. Introduction of Visual Basic (Civil Engineering Department)

  8. KEYWORDS • The words that are used by the language for special purposes are called keywords. These are called reserved words. • In a C++ program, the word main is used to indicate the starting of program, include is used to add header files, INT to declare an integer type variable. All these words are keywords of C++. Introduction of Visual Basic (Civil Engineering Department)

  9. TOKENS Tokens • A program statement consists of variable names, keywords, constants, punctuation marks, operators etc. In C++, these elements of a statement are called tokens. e.g. main () { Inta,b; } Main , {, }, a,b and punctuation marks (,) and (;) are tokens of the programs. Introduction of Visual Basic (Civil Engineering Department)

  10. DATA TYPES IN C++ • The variable types specifies the type of data that can be stored in it. Each variable is declared by its types. • C++ has five basic data types. • Int integer • Float Floating Point • Double Double Precision • Char Characters • Bool Boolean Introduction of Visual Basic (Civil Engineering Department)

  11. THE “INT” DATA TYPE • The INT represents the integer data. It is used to declare integer type variables. • An integer is a whole number i.e a number without a fraction or a decimal point. E.g. 601, 250,-6,501 are integers. • The range of values that can be stored in INT data types variable depends upon the computer system being used i.e -32768 to 32767 and it takes two bytes. Introduction of Visual Basic (Civil Engineering Department)

  12. THE FLOAT DATA TYPE • The float represents real or floating type data. • The real type data is represented in decimal or exponential notation. Float type data may be signed or unsigned. E.g. 23.26, 16.21,0.6, -9.8 . The float data type takes four bytes and it can store real values from 3.4 x 10-38 to 3.4 x 10+38. The Character Data Type • The char stands for character. It is used to declare character types variables. • In character type variables, alphabetic characters, numeric digits and special characters can be stored. • The storage capacity for a single character is 8-bit or one byte. A char type variable can hold from 1 byte to 65535 bytes. Introduction of Visual Basic (Civil Engineering Department)

  13. THE BOOL DATA TYPE • The word bool stands for Boolean. It is used to declare logical types variables. In a logical type variable, only two values true or false can be stored. The true is equivalent to “1” and false to “0”. Introduction of Visual Basic (Civil Engineering Department)

  14. CONSTANTS • A quantity that cannot change its value during execution of the program is called constant. • There are four types of constants in C++. • Integer constants. • Floating point constants • Character constants • String constants Introduction of Visual Basic (Civil Engineering Department)

  15. INTEGER CONSTANTS Integer constants • A numerical value without a decimal part is called constant. The plus (+) and minus (-) signs can also be used with an integer constants. It is used in expressions for calculations. Floating Point Constants • Numeric values that have an integer as well as a decimal part are called floating-point values. These values can also be written in exponential notation. • In exponential notation, a floating point constant is written as 123.5E2. The symbol E represents the exponent. A floating point constant may be a positive or a negative value. The exponent may also be a +ve or a –ve value. Introduction of Visual Basic (Civil Engineering Department)

  16. CHARACTER/STRING CONSTANTS Character Constants • A single character enclosed in single quotation marks is called character constant. E.g. ‘a’, ‘/’ represent character constants. String Constants • A sequence of characters consisting of alphabets, digits and /or special characters enclosed in double quotations marks is called string constants. e.g. “Pakistan”, “Lahore-54500” Introduction of Visual Basic (Civil Engineering Department)

  17. PROGRAM#1 #include<iostream.h> Main () { Int r; Const float p=3.14; Float peri; r=2; peri=2*p*r; cout<<”Result is=”<<peri; } Introduction of Visual Basic (Civil Engineering Department)

  18. THE “DEFINE” DIRECTIVE • It is a pre-processor directive. It is used to define a constant quantity. It is used at the beginning of the program. # define identifier constant #include<iostream.h> #define p=3.14 Main () { Int r; Float peri; r=2; Peri=2*p*r; Cout<<”Result is=”<<peri; } Introduction of Visual Basic (Civil Engineering Department)

More Related