#OOP_D_ITS - 3rd - Migration From C To C++

769 views
683 views

Published on

Published in: Health & Medicine, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
769
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

#OOP_D_ITS - 3rd - Migration From C To C++

  1. 1. Migration: C to C++<br />09/09/2009<br />1<br />Hadziq Fabroyir - Informatics ITS<br />
  2. 2. C/C++ Program Structure<br />Operating<br />System<br />void function1()<br />{<br /> //...<br /> return;<br />}<br />int main()<br />{<br /> function1();<br /> function2();<br /> function3();<br /> return 0;<br />}<br />void function2()<br />{<br /> //...<br /> return;<br />}<br />void function3()<br />{<br /> //...<br /> return;<br />}<br />Operating<br />System<br />
  3. 3. Naming Variable<br />MUST<br />Identifier / variable name can include letters(A-z), digits(0-9), and underscore(_)<br />Identifier starts with letteror underscore<br />Do NOT use keywordsas identifier<br />Identifier in C++ is case-sensitive<br />CONSIDER<br />Usemeaningfullname<br />Limit identifier length up to 31 characters, although it can have length up to 2048<br />Avoid using identifiers that start with an underscore<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />3<br />
  4. 4. Keywords<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />4<br />
  5. 5. Declaring Variable<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />5<br />int value;<br />char[] firstName;<br />Char[] address; <br />int 9ball;<br />long bigInt;<br />System::String full_name;<br />int count!;<br />long class;<br />float a234_djJ_685_abc___;<br />
  6. 6. Initializing Variable<br />int value = 0; <br />char[] firstName = “Budi”; <br />long bigInt(100L);<br />System::String^ full_name = “Budi Lagi”;<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />6<br />
  7. 7. Fundamental Data Types<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />7<br />
  8. 8. Literals<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />8<br />
  9. 9. Example of Data Types<br />int main()<br />{<br /> char c = &apos;A&apos;;<br /> wchar_t wideChar = L&apos;9&apos;;<br /> int i = 123;<br /> long l = 10240L;<br /> float f = 3.14f;<br /> double d = 3.14;<br /> bool b = true;<br /> return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />9<br />
  10. 10. Enumerations<br />Variable with specific sets of values<br />Enum Day {Mon, Tues, Wed, Thurs, Fri, Sat, Sun};<br />Day today = Mon;<br />Enum Day {Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};<br />Day nextDay = Tues;<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />10<br />
  11. 11. Basic Input/Output Operations<br />int main()<br />{<br /> //declare and initialize variables<br /> int num1 = 0;<br /> int num2 = 0;<br /> //getting input from keyboard<br /> cin &gt;&gt; num1 &gt;&gt; num2;<br /> //output the variables value to command line<br /> cout &lt;&lt; endl;<br /> cout &lt;&lt; &quot;Num1 : &quot; &lt;&lt; num1 &lt;&lt; endl;<br /> cout &lt;&lt; &quot;Num2 : &quot; &lt;&lt; num2;<br /> return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />11<br />
  12. 12. Escape Sequence<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />12<br />
  13. 13. Basic Operators<br />int main()<br />{<br /> int a = 0;<br /> int b = 0;<br /> int c = 0;<br /> c = a + b;<br /> c = a - b;<br /> c = a * b;<br /> c = a / b;<br /> c = a % b;<br /> a = -b; <br /> return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />13<br />
  14. 14. Bitwise Operators<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />14<br />& bitwise AND<br />~ bitwise NOT<br />| bitwise OR<br />^ bitwise XOR<br />&gt;&gt;shift right<br />&lt;&lt;shift left<br />
  15. 15. Increment and Decrement Operators<br />int main()<br />{<br /> int a = 0;<br /> int b = 0;<br /> a++;<br /> b--;<br />++a;<br /> ++b; <br /> return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />15<br />
  16. 16. Shorthand Operators<br />int main()<br />{<br /> int a = 0;<br /> int b = 0;<br /> a += 3;<br /> b -= a;<br /> a *= 2;<br /> b /= 32;<br /> a %= b; <br /> return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />16<br />
  17. 17. Explicit Casting<br />static_cast&lt;the_type_to_convert_to&gt;(expression)<br />(the_type_to_convert_to)expression<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />17<br />
  18. 18. Constant Declaration<br />int main()<br />{<br />const double rollwidth = 21.0;<br /> const double rolllength = 12.0*33.0;<br /> const double rollarea = rollwidth*rolllength;<br /> return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />18<br />
  19. 19. Declaring Namespace<br />namespace MyNamespace<br />{<br /> // code belongs to myNamespace<br />}<br />namespace OtherNamespace<br />{<br /> // code belongs to otherNamespace<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />19<br />
  20. 20. Using Namespace<br />#include &lt;iostream&gt;<br />namespace myStuff<br />{<br />int value = 0;<br />}<br />int main()<br />{<br />std::cout &lt;&lt; “enter an integer: “;<br />std::cin &gt;&gt; myStuff::value;<br />std::cout &lt;&lt; “ You entered “ &lt;&lt; myStuff::value &lt;&lt; std:: endl;<br />return 0;<br />}<br />#include &lt;iostream&gt;<br />namespace myStuff<br />{<br /> int value = 0;<br />}<br />using namespace myStuff;<br />int main()<br />{<br />std::cout &lt;&lt; “enter an integer: “;<br />std::cin &gt;&gt; value;<br />std::cout &lt;&lt; “ You entered “ &lt;&lt; value&lt;&lt; std:: endl;<br />return 0;<br />}<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />20<br />
  21. 21. Visual C++ Programming Environment<br />Hadziq Fabroyir - Informatics ITS<br />ISO/ANSI C++ (unmanaged)<br />C++/CLI<br />.NET Framework<br />Managed C++<br />Native C++<br />Framework Classes<br />Native C++<br />MFC<br />Common Language Runtime (CLR)<br />Operating System<br />HHardware<br />09/09/2009<br />21<br />
  22. 22. C++/CLI Data Types<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />22<br />
  23. 23. ITC1398 Introduction to Programming<br />Chapter 3<br />23<br />Control Structures<br />Three control structures <br />Sequence structure<br />Programs executed sequentially by default<br />Selection structures<br />if, if…else, switch<br />Repetition structures<br />while, do…while, for<br />
  24. 24. ITC1398 Introduction to Programming<br />Chapter 3<br />24<br />if Selection Statement<br />Choose among alternative courses of action<br />Pseudocode example<br />If student’s grade is greater than or equal to 60<br /> print “Passed”<br />If the condition is true<br />Print statement executes, program continues to next statement<br />If the condition is false<br />Print statement ignored, program continues<br />
  25. 25. Activity Diagram<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />25<br />
  26. 26. ITC1398 Introduction to Programming<br />Chapter 3<br />26<br />if Selection Statement<br />Translation into C++<br />if ( grade &gt;= 60 ) cout &lt;&lt; &quot;Passed&quot;;<br />Any expression can be used as the condition<br />If it evaluates to zero, it is treated as false<br />If it evaluates to non-zero, it is treated as true<br />
  27. 27. ITC1398 Introduction to Programming<br />Chapter 3<br />27<br />if…else Double-Selection Statement<br />if<br />Performs action if condition true<br />if…else<br />Performs one action if condition is true, a different action if it is false<br />Pseudocode<br />If student’s grade is greater than or equal to 60 print “Passed”Else print “Failed” <br />C++ code<br />if ( grade &gt;= 60 ) cout &lt;&lt; &quot;Passed&quot;;else cout &lt;&lt; &quot;Failed&quot;;<br />
  28. 28. Activity Diagram<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />28<br />
  29. 29. ITC1398 Introduction to Programming<br />Chapter 3<br />29<br />if…else Double-Selection Statement<br />Ternary conditional operator (?:)<br />Three arguments (condition, value if true, value if false)<br />Code could be written:<br />cout &lt;&lt; ( grade &gt;= 60 ? “Passed” : “Failed” );<br />Condition<br />Value if true<br />Value if false<br />
  30. 30. ITC1398 Introduction to Programming<br />Chapter 3<br />30<br />if…else Double-Selection Statement<br />Nested if…else statements<br />One inside another, test for multiple cases <br />Once a condition met, other statements are skipped<br />Example<br /> If student’s grade is greater than or equal to 90<br /> Print “A”<br /> Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else<br /> Print “F”<br />
  31. 31. ITC1398 Introduction to Programming<br />Chapter 3<br />31<br />if…else Double-Selection Statement<br />Nested if…else statements (Cont.)<br />Written In C++<br />if ( studentGrade &gt;= 90 ) cout &lt;&lt; &quot;A&quot;;elseif (studentGrade &gt;= 80 ) cout &lt;&lt; &quot;B&quot;;elseif (studentGrade &gt;= 70 ) cout &lt;&lt; &quot;C&quot;; elseif ( studentGrade &gt;= 60 ) cout &lt;&lt; &quot;D&quot;;else cout &lt;&lt; &quot;F&quot;;<br />
  32. 32. while Repetition Statement<br />A repetition statement (also called a looping statement or a loop) allows the programmer to specify that a program should repeat an action while some condition remains true. The pseudocode statement<br />While there are more items on my shopping list Purchase next item and cross it off my list <br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />32<br />
  33. 33. for Repetition Statement<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />33<br />
  34. 34. do …while Repetition Statement<br />do { <br />statement <br />} while ( condition );<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />34<br />
  35. 35. switch Multiple-Selection Statement<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />35<br />
  36. 36. For your practice …<br />Lab Session I (Ahad, 19.00-21.00)<br />4.14 <br />5.20 <br />6.27<br />Lab Session II (Senin, 19.00-21.00)<br />4.35 <br />5.12 <br />6.30<br />09/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />36<br />
  37. 37. ☺~ Next: OOP using C++ ~☺<br />[ 37 ]<br />Hadziq Fabroyir - Informatics ITS<br />09/09/2009<br />

×