Type |
Chapter |
Description |
C++ |
Input |
1 |
input, getting information from outside
world (user) |
cin >> age; //extraction
cin >> x >> y; |
Output |
1 |
output, getting information/results to
outside world (screen) |
cout << age; //insertion
cout << x << 'a'
<< 123 << endl;
cout << x <<
"a" << 123 << endl; |
|
1 |
declare variables |
int age;
int height, weight;
int x = 3;
float y = 3.5f; //1.65 as
0.165x10+1
int *p;
p = &age; |
|
1 |
assignment |
age = 50; //
height = weight = 50;
height = (weight = 50); |
|
1 |
comments |
//.....comment
int a; //.....comment
/*
.....comments...........
.........
*/ |
|
1 |
if selection |
if (age > 30)
cout << "Too
old"; |
|
1 |
if/else selection |
if (age > 30)
cout << "Cold";
else
cout << "Okay"; |
|
1 |
beginning main program |
void main(void)
int main()
int main(int argc, char **arg) |
|
1 |
returning a value from a function |
return x;
return;
int add(int a, int b)
{
return a+b;
}
float add(float a, float b)
{
return a+b;
}
x=add(4,5);
y=add(4.5f,6.5f);
return a+b;
return 1;
return;
void swap(int &a, int
&b)
{
if (a != b)
{
int t = a;
a = b;
b = t;
}
}
swap(x,y); |
|
1 |
having access to library of functions |
#include <iostream.h>
#include <math.h> |
|
1 |
newline |
cout << \n |
|
1 |
horizontal tabmove the screen cursor to
the next tab stop |
\t |
|
1 |
position cursor to the beginning of the
current line |
\r |
|
1 |
alert; sound the system bell |
\a |
|
1 |
addition |
f + 7;
a = f+7; |
|
1 |
subtraction |
p - c |
|
1 |
multiplication |
b * m |
|
1 |
division |
x / y
1/2
1.0/2
floating point?
0.000006234
0.6234 x 10-5 |
|
1 |
modulus |
r % s
a = 5%2;
a = rand()%7;
a = 1 + rand()%6;
a = 15 + rand()%11; |
|
1 |
equal to |
x == y
if (x==y)
cout <<
"Equal\n";
if (x=y)
cout <<
"Equal\n";
if (5==x)
cout <<
"Equal\n"; |
|
1 |
not equal to |
x != y |
|
1 |
greater than |
x > y |
|
1 |
less than |
x < y |
|
1 |
less than or equal to |
x <= y |
|
1 |
integer division |
x / y |
|
1 |
exponentiation |
z=pow(x, y); //requires <math.h> |
|
1 |
avoiding naming conflicts |
using namespace std; |
|
2 |
conditional expression |
F = (gr >= 60) ? A :
B;
if (gr >= 60)
F = A;
else
F = B;
cout << ((gr >= 60) ?
'A':'B'); |
loop |
2 |
while structure |
int product = 1;
while (product <= 1000)
product = 2 * product;
int product = 1;
while (product <= 1000)
{
product = 2 * product;
cout << product <<
' ';
} |
|
2 |
c = c + 7 |
c += 7;
c=c+1; //c += 1; c++; |
|
2 |
d = d - 7 |
d -= 7; |
|
2 |
preincrement |
++Counter;
i = 1;
x = a[++i]; //x = a[2];
i = 1;
x = a[i++]; //x = a[1];
|
|
2 |
postdecrement |
Counter--
i = 1;
x = a[i++]; //postincrement x =
a[1] |
|
2 |
switch structure |
grade = 'A';
switch (grade)
{
case'A':
++aCount; break;
case'B':
++bCount; break;
default:
cout <<
"Error\n";
} |
|
2 |
do/while |
counter = 1;
do
{
cout << counter <<
" ";
counter++;
}
while (counter <= 10); |
|
3 |
exponential function |
exp(x) |
|
3 |
natural log |
log(x) |
|
3 |
base ten log |
log10(x) |
|
3 |
square root of x |
sqrt(x) |
|
3 |
function |
int square(int y)
{
return y*y;
}
...
i = square(2);
...
void square(int x, int &y )
{
y = x*x;
}
i=2;
square(i, j); |
|
3 |
prototype |
int square(int y);
int square(int);
int sum(int, int);
void swap(int &, int
&);
void swap(int &x, int
&y);
void swap(int &x, int
&y)
{
if (x==y)
return;
else
{
int temp = x;
x = y;
y = temp;
}
}
int square(int z)
{z=9; return z*z;}
x=3;
y=square(x); // inside square
x=35;
cout << x << ' '
y;//x will remain 3
int square(int &z)
{z=9; return z*z;}
x=3;
y=square(x); // inside square
x=35;
cout << x << ' ' y;
//x will be 9 |
|
3 |
standard library header files |
<iostream.h>
<ctype.h>
<float.h>
<limits.h>
<math.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
<iomanip.h>
<fstream.h> |
|
3 |
random number |
i = rand(); //requires <stdlib.h>
i = rand()%100; // [0,99]
i = rand()%6 + 1; //[1,6]
for(int ij=0;ij<5;ij++)
cout << rand()%100
<< endl; |
|
3 |
randomizing |
seed = 16;
srand(seed);
srand(time(NULL)); //stdlib.h,
time.h
i = rand();
i = rand();
for (int j=0; j<100; j++)
cout << (rand()%6 + 1)
<< endl;
1 2 1 5 4 2 ...
2 6 1 1 3 2
|
|
3 |
data types |
long double
double
float
unsigned long int (unsigned
long)
long int (long)
unsigned int (unsigned)
int
unsigned short int (unsigned
short)
short int (short)
unsigned char
short
char |
|
3 |
enumeration |
enum Status {HEAD, TAIL};
//HEAD=0, TAIL=1
Status Result; // int a;
Result = HEAD;
enum Days {Sun, Mon, Tue};
Days today;
today = Sun; |
|
3 |
storage class specifiers |
auto int x;
register int y;
extern int a;
static int b; |
|
3 |
scope rules |
function scope
file scope
block scope
function-prototype scope
class scope |
|
3 |
recursion
n! = n * (n-1)!, 0!=1, 1!=1
5! = 5*4*3*2*1 |
unsigned long fact(int n)
{
if (n <= 1)
return 1;
else
return (n * fact(n-1));
}
cout << fact(5) <<
endl; |
|
3 |
functions with empty parameter lists |
void DisplayHello(void)
{
cout <<
"Hello!" << endl;
}
DisplayHello(); |
|
3 |
inline functions |
inline int Double(int x)
{return 2*x;}
inline int Double(int x)
{
return 2*x;
} |
|
3 |
reference parameter |
void swap(int &x, int
&y)
{
int temp = x;
x = y;
y = temp;
}
swap(a, b); |
|
3 |
reference variable (alias) |
int x = 3;
int &y = x; //y is an alias
of x |
|
3 |
default arguments |
int area(int len=1, int wid=2);
//default values only in prototype
int area(int len, int wid)
{
return len * wid;
}
...
cout << area(3) <<
endl;
cout << area(3,3)
<< endl;
cout << area() <<
endl; //call
|
|
3 |
unary scope resolution operator |
cout << x << endl;
//local value of x
cout << ::x << endl;//global
value of x |
|
3 |
function overloading |
int Sum(int x, int y)
{return x+y;}
float Sum(float x, float y)
{return x+y;}
int Sum(int x, int y, int z)
{return x+y+z;}
a = Sum(3,4,5); |
|
3 |
function templates |
template <class T>
T Sum(T x, T y)
{
return x + y;
}
...
cout << Sum(1,2) <<
endl;
cout << Sum(1.2f, 3.5f)
<< endl;
cout << Sum(10,21)
<< endl;
|
|
3 |
constants |
const int MAX_COUNT = 10; |
|
4 |
Arrays |
const int MAX =5000;
int a[MAX]; //a[0], a[1],
,
a[MAX-1]
for (int i=0; i<MAX; i++)
cin >> a[i]
int s = 0;
for (int i=0; i<MAX; i++)
s = s + a[i];
for (int i=0; i<MAX; i++)
cout << a[i] << '
'; |
|
4 |
declaring one-dimensional arrays |
int c[10];
int a[5], b[7];
int x[5] = {4, 2, 7, 3, 6};
int y[] = {4, 2, 7, 3, 6};
int z[10] = {0};
char name[40];
char city[] = "Pittsburg";
char city2[] = {'J', 'o', 'p',
'\0'}; |
|
4 |
passing arrays |
void sort(int array[], int n);
//prototype
sort(a, 10); //call
void sort(int array[], int n)
{
if (n<=1)
return;
int i, j;
for (i=1; i<=n-1; i++)
{
for (j=0; j<n-1; j++)
if (array[j]>array[j+1])
swap(array[j], array[j+1]);
}
} |
|
4 |
multi-dimensional arrays |
int a[2][3] = {{1, 2, 3}, {4,
5, 6}};
// 1 2 3
// 4 5 6 |
|
5 |
Pointer Variable Declarations and
Initialization |
int *countPtr, count;
float *sPtr, *yPtr;
//John is near Tom |
|
5 |
Pointer Operators |
int y = 5;
int *yPtr;
yPtr = &y;
cout << *yPtr;
*yPtr = 9;
//assign 9 to the variable
pointed to by yPtr
cout << y; |
|
5 |
Calling Functions by Reference |
void cube(int *p);
cube (&n);
void square(int &q);
square(n); |
|
5 |
Using the const Qualifier with Pointers |
void convToUpper(char *c);
void printStr(const char *p);
int lengthStr(const char *ptr); |
|
5 |
Pointer Expressions and Pointer Arithmetic |
char s[] = "Pittsburg";
char *ptr;
ptr = s;
ptr++;
ptr = ptr + 2;
ptr--; |
|
5 |
The Relationship Between Pointers and Arrays |
int b[5]; // 12, 56, 29, 17, 99
int *bPtr;
bPtr = b; // bPtr = &b[0];
x = *bPtr; // x = b[0];
x = *(bPtr+1); // x = b[1];
x = *(bPtr+2) // x = b[2]; //29
x = *bPtr + 2; // x = b[0] + 2;
bPtr++; |
|
5 |
Arrays of Pointers |
char *s[] = {"Pittsburg",
"Joplin"};
char **s = {"Pittsburg",
"Joplin"};
s[0] points to "Pittsburg"
s[1] points to
"Joplin" |
|
5 |
String Manipulation Functions |
char *strcpy(char *s1, const
char *s2);
char s1[10], s2[]="Pittsburg";
strcpy(s1, s2);
s1=s2; //misleading
//length of s2
int a;
a = strlen(s2);
char *p1;
p1 = s1;
a = 0;
while (*p1 != '\0')
{
a++;
p1++;
}
if (strcmp(s1, s2)<0) ... |
|
6 |
Structure Definitions |
struct TimeType
{
int hour;
int minute;
int second;
};
TimeType start, end;
start.hour = 11;
cout << start.hour;
TimeType a[5];
a[0].hour=8; //not a.hour[0]=8;
a[0].minute = 3;
struct studentRecType
{
char name[10+1];
int age;
float gpa;
};
studentRecType srecs[7000];
strcpy(srecs[0].name,
"john");
srecs[0].age = 26;
srecs[0].gpa = 4.0f;
int hour[5];
int minutes[5];
int seconds[5]; |
|
6 |
Implementing Time with a class |
class Time
{
private:
int hour;
int minute;
int secons;
public:
Time(void);
void setTime(int h, int m, int
s);
};
Time start, end;
start.hour = 11;//not allowed
cout << start.hour;//not
allowed |