Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 2 Basic of C++.

Similar presentations


Presentation on theme: "LESSON 2 Basic of C++."— Presentation transcript:

1 LESSON 2 Basic of C++

2 Writing the first program
//my first program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello World!”; getch(); return 0; } Hello World!

3 Structure of C++ Program
hash sign #include <iostream> #include <conio.h> using namespace std; int main() { }  Preprocessor directives  Namespace library  Main function header brace  Open the block  Write declarations and statements  Close the block brace

4 Writing the first program
Source Code //my first program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello World!”; getch(); return 0; }

5 Writing the second program
//my second program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello World!”; cout<< “I’m a C++ program”; getch(); return 0; } Hello World! I’m a C++ program Hello World!

6 Comments Text that are inserted into the source code to explain what the code is doing. Two types of comments: Line comment // Block comment /* */

7 Comments Example –1 #include<iostream> using namespace std;
int main() { cout <<"I play Football"; // Abdullah // ID=3333 return 0; } Example -2 /* Abdullah ID =3333 */

8 Program with Comment //my first program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Multi-line comment”; /*This line is part of the block or multi-line comment. This line is also part of the block comment */ cout<< “Line comment 1”; //This line comment takes up an entire line. cout<< “Line comment 2”; getch(); return 0; } Multi-line comment Line Comment1 Line comment2 Multi-line comment Multi-line comment Line Comment1

9 endl Command #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello”; cout<< “How are you.”; cout<< “What is your name?”; getch(); return 0; } Hello How are you. What is your name? Hello Hello How are you.

10 endl Command #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello” << endl; cout<< “How are you.” << endl; cout<< “What is your name?” << endl; getch(); return 0; } Hello How are you. What is your name? Hello Hello How are you.

11 Variables A named memory location where the value is stored.

12 Format to Declare a Variable
Data Type int height; user defined Variable Name based on the naming RULES.

13 Rules in Naming Variables
Variables may consist of letters, digits and underscore ( _ ) character with the following condition: They must begin either with a letter or an underscore. Special symbols are not allowed Space is not allowed Reserved words (Keywords) are not allowed

14 Reserved Words

15 Sample

16 Valid/Invalid Variable Names
John int letter try char a s_name my_first_name _name First*name 1man first name @name v.a.l.u.e

17 Data Types Specific category of information that a variable contains.
Description int Store numeric integer values float Numeric values with fractional parts char Store one character void Return null or valueless string Store many characters

18 Literals int float char string void -259 -13 200 -6.16 -4.4 2.7541
200 float -6.16 -4.4 2.7541 10.5 char ‘z’ ‘p’ ‘x’ ‘a’ string “Hello World” “How do you do?” “Ali” “Diploma 1” void null

19 Declaration Statement - Example
int sum; char gender; double studentgpa; int a,b,c; int a; int b; int c; int num = 15; float grade1=81.0;

20 Operating with variables
#include <iostream> #include <conio.h> using namespace std; int main() { int a,b; int result; a=5; b=2; a=a+1; result = a–b; cout <<“Result equals to”<<result; getch(); return 0; } a 6 a 5 a 2 b b result 4 result Result equals to 4

21 Constants Literal Symbolic Memory Expression with a fixed value.
Ways to use constants: Literal Symbolic Memory float pi=3.1416; #define pi ; const double pi=3.1416;

22 Operating with const #include <iostream> raduis=20;
#include <conio.h> using namespace std; int main() { const float PI=3.1416; float radius=5; float area; area=radius*radius*PI; cout<<“The area is”<<area <<“with a radius of 5.\n”; raduis=20; area=radius*radius*PI; cout <<“The area is ”<<area <<“with a radius of 20.\n”; getch(); return 0; } The area is with the radius of 5. The area is with the radius of 20. The area is with the radius of 5.

23 Operating with const #include <iostream> raduis=20;
#include <conio.h> #define PI ; using namespace std; int main() { float radius=5; float area; area=radius*radius*PI; cout<<“The area is”<<area <<“with a radius of 5.\n”; raduis=20; area=radius*radius*PI; cout <<“The area is ”<<area <<“with a radius of 20.\n”; getch(); return 0; } The area is with the radius of 5. The area is with the radius of 20. The area is with the radius of 5.

24 cout is output command that sends data given to it to the standard output device.

25 Operating with cout #include <iostream> #include <conio.h>
using namespace std; int main() { int a,b; float x,y; a=10; b=20; x= ; y = ; cout <<“a=”<<a <<“, b=”<<b <<“, x=”<<x <<“, y=”<<y; getch(); return 0; } 10 a a 20 b b x x y y A=10, b=20, x= , y=

26 cin Command reads in information from the keyboard. int age;
cin >> age; float salary; cin >> salary;

27 Operating with cin #include <iostream> #include <conio.h>
using namespace std; int main() { int number; cout<<“Enter number: ” cin>> number; cout>>”You entered ” number; getch(); return 0; } 27 number number Enter number: 27 You have entered 27 Enter number: Enter number: 27

28 Backup Slides

29 Comments Line comment Sample 1 Sample 2
cout << “Hello world!” <<endl; //Everything from here to the right is ignored Sample 2 cout << “Hello world!” <<endl; //cout and endl live in the iostream library cout << “It is nice to meet you” <<endl; //this code is hard to read cout << “Yeah!” <<endl; //specially when lines are different length

30 Comments Sample 3 //cout and endl live in the iostream library
cout << “Hello world!” <<endl; //this code is easir to read cout << “It is nice to meet you” <<endl; //don’t you think so? cout << “Yeah!” <<endl;

31 Comments Multi-line Comment Sample 1 Sample 2
/* This is a multi-line comment This line will be ignored. so will this one. */ Sample 2 /* This is a multi-line comment. * the matching asterisks to the left * can make this easier to read */


Download ppt "LESSON 2 Basic of C++."

Similar presentations


Ads by Google