Hosted By:
Lunarpages.com Web Hosting
Choose Language:
Program Structure
                using namespace std;
                namespace Namespace1
                {
                    class Class1
                    {
                        private:
                            int p;
                        public:
                            Class1(int pp){p=pp;}; 
                            show(int i){cout<<"You entered " + i<<endl;};
                    }
                }    
                int main( string[] args )
                {
                    int i = 0;
                    Class1 c = new Class1(5);
                    if( args[1] == command )
                    {
                        i = args[1];
                        c.show(i);
                    }
                }					                
                
Comments
                //Single line Comment
                
                /*Multi
                  line Comment*/
				
Data Types
                unsigned char,char
                short int,unsigned int,int, Byte,Word,Longword	
                unsigned long,enum,long
                float
                double,long double        
                
Arrays
                int[] a1 = { 1, 2, 3 };
                for(int i=0;i<5;i++)
                    cout<<"Element at " + i + "position is " + a1[i];
                a1[0] = 2;            //New array: 2,2,3
                a1[3] = 5;            //Thows exception
                
                int [][] a2 = {{1,2,3},{4,5,6}};
                
                int *n = new int[dim];
                for(int i=0; i<dim; i++)
                    n[i] = a[i];
				
                int *a3 = new int[2][];
                for(int i=0;i<2;i++)
                    a3[i] = int[3];
                
Operators
                
                //Arithmetic
                * / % + - 
                
                //Assignment
                = 
				
                //Logical
                && || ! 
                
                //Bitwise
                & | ! << <= >> >= ^ ~ &= |= ^=      

                //Increment/Decrement
                ++ --
				
                //Conditional operator
                ?:         

                //Comparison
                < <= > >= == !=      
                
                //Unary 
                new, delete, *, &
                
Decisions
                int x = random();
                char *message ="";
                if( x > 5 )
                    message = "More than five!";
                else
                    message = "Less than five!";
                    
                message = (x > 5) ? "More than five!" : "Less than five!";
                    
                if( x == 5 )
                {
                    cout<<"Yep, we've got 5!"<<endl;
                    cout<<"Need {} for multiple statements in an if"<<endl;
                }
                else if( x == 10 )
                    cout<<"Ten"<<endl;
                else
                    cout<< "Something else"<<endl;
                    
                char c = (char)(Math.random() * 26 + 'a');
                switch(c)
                {
                    case a : cout<<"c is equal to 'a'"<<endl; break;
                    case b : cout<<"c is equal to 'b'"<<endl; break;
                    default: cout<<"Something else"<<endl;
                }                                            
                
Loops
            //Pre-test
            int i = 0;
            while(i < 5)
                i++;
                
            for (int i = 0; i < 5; i++)
            {
                int num = i * 2;
                cout<<"i*2=" + num<<endl;
            }
            
            //Post-test
            do
            {
                i++;
                cout<<"i= " + i<<endl;
            }
            while(i < 5 );
            
            //Collection iteration
            //Collection in C++ are objects like List,Set,Vector,Map etc.
            #include <vector>
            using namespace std;
            vector v;
            for(int i = 0; i < 7; i++)
            {
                v.push_back(i);
            }
            for(int i = 0; i < v.size(); i++)
                cout << i << ": " << v[i] << endl;               
			
            //Loop control
            for( int i = 0; i <= 20; i++ )
            {
                if( (i % 2) == 0 )
                    continue;       //Skip to next iteration
                else if( (i%3) == 0 )
                    break;          //Exit loop
            }
                
Casting & Conversion
                float floatNum = 9.9;
                int intNum = (int) floatNum;    //decimal is truncated, intNum is set to 9
                
                int i = 20;
                cout<<char(i);
                
Enumeration
                enum days { sun, mon, tues, wed, thur, fri, sat } anyday;
                anyday = mon;
                cout<<anyday; //output result will be 1
                enum coins {
                            penny = 1,
                            tuppence,
                            nickel = penny + 4,
                            dime = 10, 
                            quarter = nickel * nickel} 
                }            
                smallchange;
                
Namespaces
                namespace F {
                    float x = 9;
                }

                namespace G {
                    using namespace F;
                    float y = 2.0;
                    namespace INNER_G {
                        float z = 10.01;
                    }
                }
                
Classes & Structs
                //Accessibility
                public
                protected
                private
                
                //Inheritance
                public class Base
                {
                    virtual int f(){//DoSomething};
				}
                
                public class Child : Base
                {
                    f(){//DoSomething};
                }
                
                //Structs are value type, but otherwise work like classes
                struct [<struct type name>] {
                    [<type> <variable-name[variable-name, ...]>] ;
                } [<structure variables>] ;
                
                struct my_struct {
                    char name[80], phone_number[80];
                    int  age, height;
                } my_friend;
                
Functions
                //Arguments are passed by value, reference, or pointers
                public void DoSomething( int x, int& y, char* s)
                {
                    //DoStuff
                }
                public int Func()
                {
                     int retVal;
                     //DoStuff
                     return  retVal;
                }
               
Exception Handling
                int main ()
                {
                    //Throwing an exception
                    throw new EIntegerRange(0, 10, userValue);
					
                    //Catching an exception
                    try
                    {
                        int bad = 5 / 0;
                    }
                    catch (const exception& e)
                    {
                        //Handle the exception here
                        cout << "Got an exception: " << e.what() << endl;
                    }
                    return 0;
                }
                
Constructors/Desctructors
                class Car
                {
                    private:
                        int mSpeed;
                        float mType;
                        char *mString;

                    //Constructors
                    public:
                        Car( int speed ) : mSpeed(speed){}
                        Car(float type, int speed )
                        {
                             //Use this->field to explicity reference members
                            this->mType = type;
    
	                        //implicit reference to member
	                        mSpeed = speed;

	                        //if you declare dynamic memory with new, delete in in the destructor
                            mString = new char[5];
                        }
                    
                        //Destructor   
                        ~Car()
                        {    //delete memory declared with new
                            delete string; 
                        }
                }
               
Pointers
                int *p = new int(5);        //Pointer to an int
                int number = *p;            //Deference p to get value pointed to
                int *q = &number            //Use address-of operator (&) to make q point at number
                
                int *pArray = new int[5];   
                *parray =  0;              //equivalent to pArray[0] = 0
                *(parray + 1) = 1;         /*1 + the starting address is the next index in the 
                                           array equivalent to pArray[1] = 1;*/