Online Test Banks
Score higher
See Online Test Banks
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

C++ Syntax that You May Have Forgotten

Part of the C++ All-In-One For Dummies Cheat Sheet

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.

Here’s a for loop:

int i;
for (i=0; i<10; i++) {
    cout << i << endl;

Here’s a while loop that counts from 10 down to 1:

int i = 10;
while (i > 0) {
    cout << i << endl;

And here’s a switch statement:

switch (x) {
case 1:
    cout << “1” << endl;
case 2:
    cout << “2” << endl;
    cout << “Something else” << endl;

Here’s a class and the code for a member function:

class MyClass {
    int x;
    void MyFunction(int y);
void MyClass::MyFunction(int y) {
    x = y;

Here’s a base class and a derived class:

class MyBase {
   // derived classes can
   // not access this
   int a;   
   // derived classes can 
   // access this
   int b;   
class Derived : public MyBase {
    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 <typename T>
class MyTemplate {
    T a;
MyTemplate<int> X;
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

Inside Sweepstakes

Win an iPad Mini. Enter to win now!