| 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 |
| 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;
a1[3] = 5;
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 |
* / % + -
=
&& || !
& | ! << <= >> >= ^ ~ &= |= ^=
++ --
?:
< <= > >= == !=
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 |
int i = 0;
while(i < 5)
i++;
for (int i = 0; i < 5; i++)
{
int num = i * 2;
cout<<"i*2=" + num<<endl;
}
do
{
i++;
cout<<"i= " + i<<endl;
}
while(i < 5 );
#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;
for( int i = 0; i <= 20; i++ )
{
if( (i % 2) == 0 )
continue;
else if( (i%3) == 0 )
break;
}
|
| Casting & Conversion |
float floatNum = 9.9;
int intNum = (int) floatNum;
int i = 20;
cout<<char(i);
|
| Enumeration |
enum days { sun, mon, tues, wed, thur, fri, sat } anyday;
anyday = mon;
cout<<anyday;
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 |
public
protected
private
public class Base
{
virtual int f(){};
}
public class Child : Base
{
f(){};
}
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 |
public void DoSomething( int x, int& y, char* s)
{
}
public int Func()
{
int retVal;
return retVal;
}
|
| Exception Handling |
int main ()
{
throw new EIntegerRange(0, 10, userValue);
try
{
int bad = 5 / 0;
}
catch (const exception& e)
{
cout << "Got an exception: " << e.what() << endl;
}
return 0;
}
|
| Constructors/Desctructors |
class Car
{
private:
int mSpeed;
float mType;
char *mString;
public:
Car( int speed ) : mSpeed(speed){}
Car(float type, int speed )
{
this->mType = type;
mSpeed = speed;
mString = new char[5];
}
~Car()
{
delete string;
}
}
|
| Pointers |
int *p = new int(5);
int number = *p;
int *q = &number
int *pArray = new int[5];
*parray = 0;
*(parray + 1) = 1;
|