Sei sulla pagina 1di 3

C++ All-In-One For Dummies

C++ is a popular programming language because its powerful, fast, easy to use, standardized, and more. Whether you are new to C++ programming or an advanced user, check out the following information on C++ mistakes, header files, and syntax. All information pertains to Windows, Mac, and Linux computers.

The Ten Most Common C++ Mistakes


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: 1. 2. 3. 4. 5. 6. 7. 8. 9. You forgot to declare the variable. You used the wrong uppercase and lowercase letters; for example, you typed Main when you meant main. You used one equal sign (=) when you were supposed to use two (==), either in an if statement or in a for loop. You forgot #include <iostream> or using namespace std;. You dropped the laptop in the swimming pool. You forgot to call new and just started using the pointer anyway. You forgot the word public: in your classes so everything turned up private. You let the dog eat the remote. You forgot to type the parentheses when calling a function that takes no parameters.

10. You forgot a semicolon, probably at the end of a class declaration.

The Usual C++ Header Files


In C++, a header file holds forward declarations of identifiers. Here are some of the most common C++ header files that youll be using, along with their correct spellings. These arent by any means all of them, but they are the most common:

Include <string> if youre going to be using the string class. Include <iostream> when you want to use cout and cin. Include <fstream> when you want to read or write files. Include <iomanip> if you want advanced manipulator usage in your streams. Include <stdlib.h> for general operations, including system(PAUSE).

C++ Syntax that You May Have Forgotten


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 switchstatement; 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. Heres a for loop: int i; for (i=0; i<10; i++) { cout << i << endl; } Heres a while loop that counts from 10 down to 1: int i = 10; while (i > 0) { cout << i << endl; i; } And heres a switch statement: switch (x) { case 1: cout << 1 << endl; case 2: cout << 2 << endl; default: cout << Something else << endl; } Heres a class and the code for a member function: class MyClass { private: int x; public: void MyFunction(int y); }; void MyClass::MyFunction(int y) { x = y; }

Heres a base class and a derived class: class MyBase { private: // derived classes can // not access this int a; protected: // derived classes can // access this int b; }; class Derived : public MyBase { public: void test() { b = 10; } }; Heres a function, a function pointer type, and a pointer to the function: int function(char x) { return (int)x; } typedef int (* funcptr)(char); funcptr MyPtr = function; And heres a class template and then a class based on the template: template <typename T> class MyTemplate { public: T a; }; MyTemplate<int> X;

Potrebbero piacerti anche