Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)

Similar presentations


Presentation on theme: "Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)"— Presentation transcript:

1 Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1

2 Chapter NoChapter Name 1. Introduction to C++ 2. Variables,constants,expressions 3. Conditional statements 4. Loops 5. Arrays 6. Functions 7.Classes 8.Inheritance 2 Course Contents BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER) Recommended book:oop in c++ by Robert lafore 2

3 Introduction to C++ : Differences between C and C++: C++ is derived from the C language. C is a structured language, while C++ is an object-oriented language. Dennis Ritchie developed C in 1970, while Bjarne stroustrup developed C++ in the early 1980 at Bell Laboratories. 3

4 Conti….. C++ is an object-oriented version of C. Object-oriented programming involves concepts that are new to programmers of traditional languages such as BASIC, Pascal and C. It goals clear, more reliable and more easily maintained programs. 4

5 Conti… As compare to C, C++ has many features like improved approach to input/output and a new way to write comments. C++ inherits much of the efficiency of C language, as well as the readability and ease of writing programs. C++ supports the concept of encapsulation and data hiding through the creation of user- define types, called Classes. 5

6 Conti…. C++ supports the polymorphism concept. Polymorphism means to create multiple definitions for operators and functions. 6

7 Writing c ++ program The source code of c ++ program is stored on the disk with extension cpp (cpp stand c plus plus). The program is stored in a text file on the disk. any text editor can be used to write and edit c++ source code. The c++ compiler translates a c ++ program into the machine code. the machine code is called object code. it is stored in a new file with extension obj. 7

8 Conti….. The object code is then linked to the libraries. After linking the object code to the libraries, an executable file with extension.exe is created. The executable program is then run from operating system command line. For example a source code of program in c++ is written and stored in file first.cpp. After compilation the object code is saved in file first.obj and executable code is stored in file first.exe. 8

9 Structure of c++ program A c++ program consists of three main parts. These are: Preprocessor directives The main () function C++ statements 9

10 Preprocessor directives The instruction 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 directives normally start with a number sign # and keyword “include” or “define “.for example preprocessor directives are used to include header files in the program. 10

11 Header file Header file is a ++ source file that contains definition of library functions. Header files are added into program at the time of compilation of the program. A header file is added if the function /object defined in it is to be used in the program. The preprocessor directive “include” is used to add a header file into the program. The name of file is written in angle bracket <> after “# include” directive. it can also be written in double quotes. 11

12 Conti…….. C++ has a large number of header files in which library functions are defined. The header file must be included in the program before calling its functions in the program. A single header file may contain a large number of built in library functions. For example,the header file iostream.h has definition of different built in input output objects and functions. 12

13 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 statements within this function are the main body of the C++ program. 13

14 Conti…. If main() function is not included, the program is not compiled and an error message is generated. The syntax of main() function is : main () { program statement…. } 14

15 Basic input/output The statements that are used to get data and then assign it to variables are known as input statements The statements that are used to receive data from the computer memory and then to send it to the output devices are called output statements 15

16 The “cout” object-output stream The cout is pronounced as ‘see out’. The ’cout’ stand for console output. The console represents the computer display screen. The ‘cout’ is a c++ predefined object. It is used as an output statement to display output on the computer screen. It is a part of iostream header file. Flow of data from one location to another is called stream. The ‘cout’ is the standard output stream in c++. This stream sends the output to the computer screen. 16

17 Syntax of cout Cout<<constant/variable; where ‘cout ‘ name of output stream object. ‘<<‘ insertion operator it separate variable,constants. The string constant are given in a double quotation marks. numeric constants, variable and expression are without quotation marks. 17

18 A program to print a message on screen using cout object. # include main() { clrscr(); cout<<“ c++ is a powerful programming language”; } Note: clrscr() function is part of conio.h header file. 18

19 The “cin “ object-input stream The “cin” stands for console input. It is pronounced as “see in”. It is an input stream. It is used as input statement to get input from the keyboard during execution of the program. When an input statement is executed, the computer waits to receive an input from keyboard. When a value is typed and enter key is pressed,the value is assigned to the variable and control shifts to the next statement. The cin object is also part of iostream.h header file. 19

20 Syntax of cin Cin >> var1>>var2…; where cin represent the object name used as input stream. “>>” extraction operator or get from operator. It gets an input from the input device and assign it to the variable. each variable is separated by extraction.”>>”. for example. cin>>a>>b>>c; 20

21 Calculate sum and product get value from keyboard during program execution. #include main () { int n1,n2,s,p; clrscr(); cout<<“enter first number “; cin>>n1; cout<<“enter second value”; cin>>n2; s=n1+n2; p=n1*n2; cout<<“sum of two number”<<s<<endl; cout<<“product of two number “<<p<<endl; getch(); } 21

22 The endl manipulator The “endl” stands for end of line. It has same effect as the escape sequence “\n”. This is a predefined iostream manipulator. For example cout<<“c++\ n”<<“programming \n“; is equivalent to: cout<<“c++”<<endl<<“programming” <<endl; 22

23 Program to print a message on screen by using endl manipulator #include main() { cout <<“I am a”<<endl<<“student”; } 23

24 The “setw” manipulator The “setw” stand for set width. This manipulator is used to set width of the output on the output device. syntax is : setw(n) where n represent the width of the output field. It is an integer value. The setw manipulator is a part of iomanip.h header file. If the manipulator is to be used in the program,this header file must be included at the beginning of the program using include statement. 24

25 P rogram that uses “setw” manipulator #include main() { clrscr(); cout<<setw(5)<<63<<setw(5)<<8<<endl; cout<<setw(5)<<100<<setw(5)<<77<<endl; getch(); } 25

26 The escape sequence Special non-printing characters are used to control printing on the output device. These are called escape sequence. These characters are not printed. These are used inside string constants or independently. These are written in single or double quotes. It is combination of backslash ‘\’ and a code character. 26

27 Escape sequence and explanation \a“a” stands for alert a alarm. It cause a beep sound. \b“b” stands for backspace. After printing a string the cursor will move one space back. \t“t” stands for tab. It cause one tab forward. \n“n” stands for new line. It move control to the beginning of next line. \r“r” stands for carriage return. It move the cursor to the beginning of the current line. 27

28 Conti….. \\These are two backslash characters. One is used as control character and second is used to print a backslash ‘\’ character. For eg: cout<<“A:\\xyz”; out put will be A:\xyz \””This escape sequence is similar to the “\\”. It is used to print double quotation marks (“). For eg :cout<<“\”MIHE”; Output will be “MIHE 28

29 29


Download ppt "Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)"

Similar presentations


Ads by Google