Wednesday, March 25, 2015

C++ Referral sheet


C++ is very popular with programmers and developers it is very powerful, is very reliant and easy to develop in. We are going to provide you with a C++ referral sheet that you will come in very useful if you are a beginner or an advanced user of C++.

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 you’ll be using, along with their correct spellings. These aren’t by any means all of them, but they are the most common:
  • Include <string> if you’re 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, includingsystem(“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 aswitch 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.
Here’s a for loop:
int i;
for (i=0; i&lt;10; i++) {
    cout &lt;&lt; i &lt;&lt; endl;
}
Here’s a while loop that counts from 10 down to 1:
int i = 10;
while (i &gt; 0) {
    cout &lt;&lt; i &lt;&lt; endl;
    i—;
}
And here’s a switch statement:
switch (x) {
case 1:
    cout &lt;&lt; 1 &lt;&lt; endl;
case 2:
    cout &lt;&lt; 2 &lt;&lt; endl;
default:
    cout &lt;&lt; Something else &lt;&lt; endl;
}
Here’s 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;
}
Here’s 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;
    }
};
Here’s 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 here’s a class template and then a class based on the template:
template &lt;typename T&gt;
class MyTemplate {
public:
    T a;
};
MyTemplate&lt;int&gt; X;

Filed Under :

0 comments for "C++ Referral sheet"

Post a Comment

background