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 Simply a name chosen by the programmer that is used to refer computer storage location.

12 Variables Variables may consist of letters, digits and underscore ( _ ) character with the following condition: They must begin either with a letter or an underscore. Uppercase and lowercase are significant Space is not allowed Keywords are not allowed as variable names

13 List of C++ Keywords

14 Valid/Invalid Variable Names
John int Name const char NAME s_name first_name _name first-name 1man first name @name v.a.l.u.e

15 Data Types Specific category of information that a variable contains.
Type Category Description Keyword Integer Positive or negative number with no decimal int Floating Point Numbers containing decimal places float double long double Character Text char Boolean True(1) or False(0) bool

16 Declaration Statement
Naming a variable and specifying the data type. int float double long double char bool type name; user defined name based on the naming RULES.

17 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;

18 Literals int float char string bool -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” bool true false

19 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

20 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;

21 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.

22 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.

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

24 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=

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

26 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

27 Backup Slides

28 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

29 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;

30 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