Advertisement
here2share

The 10 Most Common C++ Mistakes

Nov 16th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. The 10 Most Common C++ Mistakes
  2.  
  3. Although many C++ programmers take measures to prevent bugs, mistakes still slip through. This list of the ten most common mistakes while writing C++ code can help both new and veteran programmers:
  4.  
  5.     You forgot to declare the variable.
  6.  
  7.     You used the wrong uppercase and lowercase letters; for example, you typed Main when you meant main.
  8.  
  9.     You used one equal sign (=) when you were supposed to use two (==), either in an if statement or in a for loop.
  10.  
  11.     You forgot #include <iostream> or using namespace std;.
  12.  
  13.     You dropped the laptop in the swimming pool.
  14.  
  15.     You forgot to call new and just started using the pointer anyway.
  16.  
  17.     You forgot the word public: in your classes so everything turned up private.
  18.  
  19.     You let the dog eat the remote.
  20.  
  21.     You forgot to type the parentheses when calling a function that takes no parameters.
  22.  
  23.     You forgot a semicolon, probably at the end of a class declaration.
  24.  
  25. The Usual C++ Header Files
  26.  
  27. In C++, a header file holds forward declarations of identifiers. Here are some of the most common C++ header files that you’ll be using, along with their correct spellings. These aren’t by any means all of them, but they are the most common:
  28.  
  29.     Include <string> if you’re going to be using the string class.
  30.  
  31.     Include <iostream> when you want to use cout and cin.
  32.  
  33.     Include <fstream> when you want to read or write files.
  34.  
  35.     Include <iomanip> if you want advanced manipulator usage in your streams.
  36.  
  37.     Include <stdlib.h> for general operations, including system(“PAUSE”).
  38.  
  39. C++ Syntax that You May Have Forgotten
  40.  
  41. Remembering a bunch of C++ syntax can make you “loopy.” The following samples show the syntax of some of the more easily forgotten C++ situations: a for loop, a while loop, and a switch statement; a class and the code for a member function; a base class and a derived class; a function, function pointer type, and pointer to the function; and a class template and then a class based on the template.
  42.  
  43.  
  44. // Here’s a for loop:
  45.  
  46. int i;
  47. for (i=0; i<10; i++) {
  48.     cout << i << endl;
  49. }
  50.  
  51. // Here’s a while loop that counts from 10 down to 1:
  52.  
  53. int i = 10;
  54. while (i > 0) {
  55.     cout << i << endl;
  56.     i—;
  57. }
  58.  
  59. // And here’s a switch statement:
  60.  
  61. switch (x) {
  62. case 1:
  63.     cout <<1< < endl;
  64. case 2:
  65.     cout <<2<< endl;
  66. default:
  67.     cout << “Something else<< endl;
  68. }
  69.  
  70. // Here’s a class and the code for a member function:
  71.  
  72. class MyClass {
  73. private:
  74.     int x;
  75. public:
  76.     void MyFunction(int y);
  77. };
  78. void MyClass::MyFunction(int y) {
  79.     x = y;
  80. }
  81.  
  82. // Here’s a base class and a derived class:
  83.  
  84. class MyBase {
  85. private:
  86.    // derived classes can
  87.    // not access this
  88.    int a;  
  89. protected:
  90.    // derived classes can
  91.    // access this
  92.    int b;  
  93. };
  94. class Derived : public MyBase {
  95. public:
  96.     void test() {
  97.         b = 10;
  98.     }
  99. };
  100.  
  101. // Here’s a function, a function pointer type, and a pointer to the function:
  102.  
  103. int function(char x) {
  104.     return (int)x;
  105. }
  106. typedef int (* funcptr)(char);
  107. funcptr MyPtr = function;
  108.  
  109. // And here’s a class template and then a class based on the template:
  110.  
  111. template <typename T>
  112. class MyTemplate {
  113. public:
  114.     T a;
  115. };
  116. MyTemplate<int> X;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement