Advertisement
XRahat2011

C++ ClassRoom -- Lecture No. 1

Nov 21st, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. OK!
  2.  
  3. In C++
  4. We use
  5. //
  6. for one line comments
  7. examples:
  8. //this is a comment
  9. or
  10. int a=5; //a one line comment can come here too without any problems ;)
  11. and /* */
  12. for more lines
  13. example:
  14. /* this is a big comment */
  15. or /* this
  16. is
  17. a
  18. big
  19. comment */
  20. or int a /* OK! The int variable name here is a ;) */ =5; //I know this example is looking ugly but still it's just an example ;)
  21.  
  22. cout outputs a message to the screen
  23. cin takes in user input
  24.  
  25. int=integer; //Example: int x = 999;
  26. float=float; //Example: float y = 5.7;
  27. bool=true /* or */ false; //That is exactly like this: bool a=true; bool b=false;
  28. double=double;  //Example: double z = 998894.54982;
  29.  
  30. Double means "Double precision Floating-point number" while Float means "Floating-point number"
  31.  
  32. float vs double : double wins because double can take more numbers. For simple numbers like 599.75 always use float but for complicated numbers like 998894.54982 always use double. Why? Because when doing maths in C/C++ using double is like a must because double is more precise than float! Float can round numbers too when the answer is longer than it's storing capacity but usually numbers are not too bigger than double's storing capacity hence always using double when doing maths in C/C++ is better ;)
  33.  
  34. char is "character" & char is for 1 letter
  35. char a="h";
  36. varchar is "variable characters" & varchar is for up to 4000 letters at once ;)
  37. varchar vc = "Hello World or Hello world or hello World or hello world"
  38. bool is for true or false
  39. bool a=true; //never use bool a="true"; always use bool a=true; because C/C++ does know what is true with bool data type
  40. bool b=false; //never use bool b="false"; always use bool b=false; because C/C++ does know what is false with bool data type
  41.  
  42. using of const
  43. we can not change value of a constant (that is const)
  44. for example:
  45. const int a=4;
  46. a=3;
  47. this is wrong! Because we can not change the value of a constant after it's declared :(
  48.  
  49. another example:
  50. int a=4;
  51. a=3;
  52. this is correct! Because we can always change the value of a variable anytime we want ;)
  53.  
  54. with sizeof()
  55. we take the memory size of a variable
  56. for example:
  57.  
  58. int a=5;
  59. cout<<sizeof(int);
  60.  
  61. this will print
  62. 4
  63. because int=4 in your computer's memory (or might be different :P)
  64. it is about your computer!
  65. cout<<sizeof(a);
  66. is the same as
  67. cout<<sizeof(int);
  68. because a is declared as an int!
  69. So, the result will be
  70. 4
  71. again! Because /*remember*/ a is decalred as an int ;)
  72.  
  73. #include <iostream>
  74. using namespace std;
  75.  
  76. int main(int argc, char ** argv) {
  77.  int a=5;
  78.  cout<<sizeof(5);
  79. }
  80.  
  81. this will print
  82. 4
  83. not 5
  84. because 5 is an int
  85. and int=4 in your computer (or might be different :P)
  86. -------
  87. lets  go in "if else" statements now
  88. we declare it as
  89.  
  90. if(expression) {
  91.     statement1
  92. } else {
  93.     statement2
  94. }
  95.  
  96. example:
  97.  
  98. int a=5;
  99. if(a==5){
  100.  cout<<"a is 5";
  101. } else {
  102.  cout<<"a is not 5"
  103. }
  104.  
  105. also, if we want to check more things
  106. we use "else if"
  107. example:
  108.  
  109. #include <iostream>
  110. using namespace std;
  111.  
  112. int main(int argc, char ** argv) {
  113. int a;
  114. cin>>a;
  115. if(a>5) {
  116. cout<<"a is greater than 5";
  117. } else if(a<5) {
  118.  cout<<"a is smaller than 5";
  119. } else {
  120.  cout<<"5";
  121.  }
  122. }
  123.  
  124. Lecture Ends Here ;)
  125. -------
  126. Challenges Start Here ;)
  127.  
  128. So,
  129. Challenge is:
  130. Make a program
  131. that declares all variables int, float & most other types
  132. and after putting them
  133.  
  134. example:
  135. int a;
  136. cin>>a;
  137. cout<<a;
  138. float b;
  139. cin>>b;
  140. cout<<b;
  141.  
  142. etc....
  143. continue the program (in whatever way you want) & then use everything in the program (take the example above for an example)!
  144. int, bool, char, varchar etc...
  145.  
  146. --"Owl" & "XRahat2011" Participated In Challenge & Both Succeeded Without Any Mistakes ;)
  147.  
  148. Challenges Finish Here ;)
  149. -------
  150. HomeWork Starts Here ;)
  151.  
  152. Make a very simple calculator that can add, subtract, divide & multiply numbers ;)
  153.  
  154. HomeWork Ends Here ;)
  155. -------
  156. ** The End ;)
  157. ** Class Moderated By "XRahat2011" ;)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement