overview of c++ language

31
Overview of C++ language

Upload: samt7

Post on 30-May-2015

495 views

Category:

Education


0 download

DESCRIPTION

Overview of c++ programming language

TRANSCRIPT

Page 1: Overview of c++ language

Overview of C++ language

Page 2: Overview of c++ language

Lecture’s outline

What is C++?Identifiers.ConstantSemicolons & Blocks in C++Data typeCommentsOperatorDeclaration of variablesGiving value to variableC++ statements

prepared by Taif.A.S.G2

Page 3: Overview of c++ language

What is C++?• Developed by Bjarne Stroustrup (in 1979 at Bell Labs)

• C++ is regarded as a middle-level language, as it comprises a

combination of both high-level and low-level language features.

• C++ is a statically typed, compiled, general purpose, case-

sensitive, free-form programming language that supports

procedural, object-oriented, and generic programming.

• C++ is a superset of C that enhancement to the C language and

originally named C with Classes but later it was renamed C++.

prepared by Taif.A.S.G3

Page 4: Overview of c++ language

identifiersThey are used for naming classes function, module,

variables, object in a program.

They follow the following rules:

1) They can have alphabets , digits, and the underscore(_).

2) They must not begin with a digit.

3) Uppercase and lowercase are distinct.

4) They can be of any length.

5) It should not be a keyword.

prepared by Taif.A.S.G4

6) White space is not allowed.

Page 5: Overview of c++ language

ConstantConstant is one whose value cannot be changed after it has been initialized-any attempt to assign to field will produce a compile-error .So, we can declare the constant by using const field and use all uppercase letter.

Syntax of declare the constant is:

Example:

const int STRENGTH = 100;const float PI = 3.14;

const type name=value

prepared by Taif.A.S.G5

Page 6: Overview of c++ language

Semicolons & Blocks in C++:• In C++, the semicolon is a statement terminator.

That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.

• For example: following are two different statements:

x = y; y = y+1;

prepared by Taif.A.S.G 6

Page 7: Overview of c++ language

Data type

prepared by Taif.A.S.G7

Page 8: Overview of c++ language

Comments

C++ can use both the single line comments and multi-line comments .single line comments begin with // and end at the end of line.For longer comments we can create multi-line comments by starting with /* and ending with */

prepared by Taif.A.S.G8

Page 9: Overview of c++ language

Operator* / % + - are the mathematical operators* / % have a higher precedence than + or – ++ Increment operator –– Decrement operator

== Equal (careful)!= Not equal>= Greater than or equal<= Less than or equal> Greater than< Less than&& logical (sequential) and || logical (sequential) or= assignment

prepared by Taif.A.S.G9

Logical operators

Relational operators

mathematical operators

Page 10: Overview of c++ language

Declaration of variables

The general form of declaration of variables:

Example:

int count;float x,y;double pi;char c1,c2,c3;byte b;

Type variable1, variable2,...., variableN;

prepared by Taif.A.S.G10

Page 11: Overview of c++ language

Giving value to variable

The general form of giving value to variables:

Example:

finalValue=100;x=y=z=0;Yes = ‘x’;

variableName= value;

prepared by Taif.A.S.G11

The general form of giving value to variables:

Example:

finalValue=100;x=y=z=0;Yes = ‘x’;

variableName= value;

Page 12: Overview of c++ language

Simple programs

prepared by Taif.A.S.G12

Write C++ Program to find Sum and Average of two numbers?

Write C++ Program to find area of a circle?

Page 13: Overview of c++ language

prepared by Taif.A.S.G13

C++ statement

Control statement

Selection statement

jump statement

iteration statement

If if-else switch for while do break continue return

Page 14: Overview of c++ language

• C++ supports the following statements Known as control or decision making statements.

1. if statement.2. switch statement.3. Conditional operator ? : statement.

prepared by Taif.A.S.G 14

Decision making

Page 15: Overview of c++ language

prepared by Taif.A.S.G 15

Decision making with if statement

The general form of simple if statement is :

Example:

if (a > b) cout<<"a > b";

if (<conditional expression>)<statement action>

Page 16: Overview of c++ language

prepared by Taif.A.S.G 16

Decision making with if statement cont..

The general form of simple if else statement is :

Example:

if (a > b) cout<<" a > b"; else cout<<"a<b";

if (<conditional expression>)<statement action>else <statement action>

Page 17: Overview of c++ language

prepared by Taif.A.S.G 17

Decision making with if statement cont..

The general form of Multiple if else statement is :

Example:if (a > b) cout<<" a > b"; else if (a< b) cout<<"b > a";else cout<<" a=b";

if (<conditional expression>)<statement action>else if (<conditional expression>)<statement action>else <statement action>

Page 18: Overview of c++ language

• The if statement allows you to select one of two sections of code to execute based on a boolean value (only two possible values). The switch statement allows you to choose from many statements.

prepared by Taif.A.S.G 18

Decision making with switch statement

Page 19: Overview of c++ language

switch (expr) { case c1: statements // do these if expr == c1 break; case c2: statements // do these if expr == c2 break; case c3: case c4: // Cases can simply fall thru. statements // do these if expr == any of c's break; . . . default: // OPTIONALstatements // do these if expr != any above }

prepared by Taif.A.S.G 19

Decision making with switch statement cont..

Page 20: Overview of c++ language

The ? : Operator:

prepared by Taif.A.S.G 20

• can be used to replace if...else statements. It has the following general form:

Exp1 ? Exp2 : Exp3; Where Exp1, Exp2, and Exp3 are expressions. • The value of a ? expression is determined like this:

Exp1 is condition evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.

Page 21: Overview of c++ language

Simple programs

prepared by Taif.A.S.G21

Write C++ Program to find Number is Positive or Negative?

Write C++ Program to find the Grade of student ?

Write C++ Program to check the day of week by using SWITCH-CASE?

Page 22: Overview of c++ language

LoopingThe C++ language provides three constructs for

performing loop operations, there are:1. The for statement.2. The while statement.3. The do statement.

prepared by Taif.A.S.G 22

Page 23: Overview of c++ language

prepared by Taif.A.S.G 23

Looping cont..The general form of for statement:

Example:for( x=0; x<=10; x++) {cout<<x;}

for (initialization; test condition; increment) { Body of loop }

Page 24: Overview of c++ language

prepared by Taif.A.S.G 24

Looping cont..The general form of while statement:

Example:x=0;while(x<=10) {cout<<x;x++; }

initialization; while(test condition) { Body of loop }

Page 25: Overview of c++ language

prepared by Taif.A.S.G 25

Looping cont..The general form of do statement:

Example:x=0;do{cout<<x;x++; }while(x<=10) ;

initialization; do { Body of loop } while(test condition);

Page 26: Overview of c++ language

prepared by Taif.A.S.G 26

Nested Loopingfor(num2 = 0; num2 <= 3; num2++){ for(num1 = 0; num1 <= 2; num1++) { cout<<num2 << " " <<num1; }}

Int num1 Int num2

0 0

1

2

3 end of loop

0 1

1

2

3 end of loop

0 2

1

2

3 end of loop

0 3

1

2

3 end of loop

4 End of loop

Page 27: Overview of c++ language

Functions

• A complex problem is often easier to solve by dividing it into several smaller parts(Modules), each of which can be solved by itself. This is called structured programming.

• In C++ Modules Known as Functions & Classes• main() then uses these functions to solve the

original problem.

prepared by Taif.A.S.G 27

Page 28: Overview of c++ language

Functions (cont..)

• C++ allows the use of both internal (user-defined) and external functions.

• External functions (e.g., abs, ceil, rand, sqrt, etc.) are usually grouped into specialized libraries (e.g., iostream, stdlib, math, etc.)

prepared by Taif.A.S.G 28

Page 29: Overview of c++ language

Function prototype• The function prototype declares the input and

output parameters of the function.

• The function prototype has the following syntax: <type> <function name>(<type list>);

• Example: A function that returns the absolute value of an integer is: int absolute(int);

• If a function definition is placed in front of main(), there is no need to include its function prototype.

prepared by Taif.A.S.G 29

Page 30: Overview of c++ language

Function Definition• A function definition has the following syntax:

<type> <function name>(<parameter list>){<local declarations> <sequence of statements> }

• For example: Definition of a function that computes the absolute value of an integer:

int absolute(int x){ if (x >= 0) return x; else return -x; }

• The function definition can be placed anywhere in the program. prepared by Taif.A.S.G 30

Page 31: Overview of c++ language

Function call

• A function call has the following syntax: <function name>(<argument list>)

Example: int distance = absolute(-5);

prepared by Taif.A.S.G 31