Advertisement
wingman007

2018_C++

May 2nd, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <limits>
  4. #include <complex>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. enum { ASM , AUTO , BREAK };
  10.  
  11. int main ()
  12. {
  13.     std::string name = "Stoyan";
  14.     std::cout << "Hello, new world!\n";
  15.     printf("name: %s \ n ",name.c_str());
  16.    
  17.     string str;
  18.     cout << "P l e a s e e n t e r y o u r n a m e \ n ";
  19.     cin >> str ;
  20.     cout << "H e l l o , " << str << "!\ n ";
  21.    
  22.     cout << "largestfloat == " << numeric_limits<float>::max() << ", char is signed == " << numeric_limits<char>::is_signed << '\n';
  23.    
  24.    
  25.     // void x; // error: there are no void objects
  26.     // void f(); // function f does not return a value (§7.3)
  27.     void* pv; // pointer to object of unknown type (§5.6)
  28.    
  29.     typedef char* Pchar; // A declaration prefixed by the keyword t y p e d e f declares a new name for the type rather than a new variable of the given type.
  30.     Pchar p1, p2; // p1 and p2 are char*s
  31.     char* p3 = p1;
  32.     cout << "beep at end of message \a\n";
  33. }
  34.  
  35. char ch;
  36. string s;
  37. int count = 1;
  38. const double pi = 3.1415926535897932385;
  39. extern int error_number;
  40.  
  41. char* name = "Njal";
  42. char* season[] = { "s p r i n g ", "s u m m e r ", "f a l l ", "w i n t e r " };
  43.  
  44. struct Date { int d, m, y; };
  45. int day(Date* p) { return p ->d; }
  46. double sqrt(double);
  47. template<class T> T abs(T a) { return a<0 ? -a : a; }
  48. typedef std::complex<short> Point; // t y p e d e f declares a new name for the type rather than a new variable of the given type
  49. struct User;
  50. enum Beer { Carlsberg , Tuborg , Thor };
  51. namespace NS { int a; }
  52.  
  53. // typedef A name defined like this, usually called a ‘‘ t y p e d e f ,’’ can be a convenient shorthand for a type
  54. // with an unwieldy name. For example, u n s i g n e d c h a r is too long for really frequent use, so we
  55. // could define a synonym, u c h a r :
  56. typedef unsigned char uchar;
  57.  
  58. // Another use of a t y p e d e f is to limit the direct reference to a type to one place. For example:
  59. typedef int int32;
  60. typedef short int16;
  61.  
  62. char c = 'a';
  63. char* p = &c; // p holds the address of c
  64.  
  65. // Unfortunately, pointers to arrays and pointers to functions need a more complicated notation:
  66. int* pi1; // pointer to int
  67. char** ppc; // pointer to pointer to char
  68. int* ap[15]; // array of 15 pointers to ints
  69. int (*fp)(char*); // pointer to function taking a char* argument; returns an int
  70. int* f(char*); // function taking a char* argument; returns a pointer to int
  71.  
  72. // The fundamental operation on a pointer is dereferencing, that is, referring to the object pointed
  73. // to by the pointer. This operation is also called indirection. The dereferencing operator is (prefix)
  74. // unary *. For example:
  75. char c1 = 'a';
  76. char* p1 = &c1 ; // p holds the address of c
  77. char c2 = *p1 ; // c2 == ’a’
  78.  
  79.  
  80. float v[3]; // an array of three floats: v[0], v[1], v[2]
  81. char* a[32]; // an array of 32 pointers to char: a[0] .. a[31]
  82.  
  83. void f (int i)
  84. {
  85. int v1[i]; // error: array size not a constant expression
  86. vector<int> v2(i); // ok
  87. }
  88.  
  89. // An array can be initialized by a list of values. For example:
  90. int v1[] = { 1 , 2 , 3 , 4 };
  91. char v2[] = { 'a', 'b', 'c', 0 };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement