Overview of c++ language

1,145 views
864 views

Published on

Overview of c++ programming language

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,145
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
86
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Overview of c++ language

  1. 1. Overview of C++ language
  2. 2. Lecture’s outline What is C++? Identifiers. Constant Semicolons & Blocks in C++ Data type Comments Operator Declaration of variables Giving value to variable C++ statements prepared by Taif.A.S.G
  3. 3. 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.G
  4. 4. identifiersThey are used for naming classesfunction, 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.6) White space is not allowed. prepared by Taif.A.S.G
  5. 5. ConstantConstant is one whose value cannot be changed after it hasbeen initialized-any attempt to assign to field will produce acompile-error .So, we can declare the constant by usingconst field and use all uppercase letter.Syntax of declare the constant is: const type name=valueExample:const int STRENGTH = 100;const float PI = 3.14; prepared by Taif.A.S.G
  6. 6. 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
  7. 7. Data typeprepared by Taif.A.S.G
  8. 8. CommentsC++ can use both the single line comments and multi-linecomments .single line comments begin with // and end at the end of line.For longer comments we can create multi-line comments bystarting with /* and ending with */ prepared by Taif.A.S.G
  9. 9. Operator* / % + - are the mathematical operators* / % have a higher precedence than + or – mathematical operators ++ Increment operator –– Decrement operator == Equal (careful) != Not equal >= Greater than or equal Relational <= Less than or equal operators > Greater than < Less than && logical (sequential) and Logical || logical (sequential) or operators = assignment prepared by Taif.A.S.G
  10. 10. Declaration of variablesThe general form of declaration of variables: Type variable1, variable2,...., variableN;Example:int count;float x,y;double pi;char c1,c2,c3;byte b; prepared by Taif.A.S.G
  11. 11. Giving value to variableThe general form of giving value to variables: variableName= value;Example:finalValue=100;x=y=z=0;Yes = ‘x’; prepared by Taif.A.S.G
  12. 12. Simple programs Write C++ Program to find Sum and Average of two numbers?Write C++ Program to find area of a circle? prepared by Taif.A.S.G
  13. 13. C++ statement Control statement Selection iteration jump statement statement statementIf if-else switch for while do break continue return prepared by Taif.A.S.G
  14. 14. Decision making• 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
  15. 15. Decision making with if statementThe general form of simple if statement is : if (<conditional expression>) <statement action>Example: if (a > b) cout<<"a > b"; prepared by Taif.A.S.G 15
  16. 16. Decision making with if statement cont..The general form of simple if else statement is : if (<conditional expression>) <statement action> elseExample: <statement action> if (a > b) cout<<" a > b"; else cout<<"a<b"; prepared by Taif.A.S.G 16
  17. 17. Decision making with if statement cont..The general form of Multiple if else statement is : if (<conditional expression>) <statement action> else if (<conditional expression>) <statement action> elseExample: <statement action>if (a > b)cout<<" a > b"; else if (a< b) cout<<"b > a";else cout<<" a=b"; prepared by Taif.A.S.G 17
  18. 18. Decision making with switch statement• 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
  19. 19. Decision making with switch statement cont..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 cs break; . . .default: // OPTIONALstatements // do these if expr != any above } prepared by Taif.A.S.G 19
  20. 20. The ? : Operator:• 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. prepared by Taif.A.S.G 20
  21. 21. Simple programs 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? prepared by Taif.A.S.G
  22. 22. 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
  23. 23. Looping cont..The general form of for statement:for (initialization; test condition; increment) { Body of loop }Example:for( x=0; x<=10; x++){cout<<x;} prepared by Taif.A.S.G 23
  24. 24. Looping cont..The general form of while statement: initialization; while(test condition) { Body of loop }Example:x=0;while(x<=10){cout<<x;x++; } prepared by Taif.A.S.G 24
  25. 25. Looping cont..The general form of do statement: initialization; do { Body of loop } while(test condition);Example:x=0;do{cout<<x;x++; }while(x<=10) ; prepared by Taif.A.S.G 25
  26. 26. Int num2 Int num1 0 0 Nested Looping 1for(num2 = 0; num2 <= 3; num2++) 2{ 3 end of loop for(num1 = 0; num1 <= 2; num1++) 1 0 { 1 cout<<num2 << " " <<num1; 2 }} 3 end of loop 2 0 1 2 3 end of loop 3 0 1 2 3 end of loop prepared by Taif.A.S.G 26 4 End of loop
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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

×