The codes:
// 3.Compound Data Types/*****************************************1Arrays*/// arrays example#includeusing namespace std;int billy [] = { 16, 2, 77, 40, 12071};int n, result=0;int main (){ for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0;}// arrays example#include using namespace std;#define WIDTH 5#define HEIGHT 3int jimmy [HEIGHT][WIDTH];int n,m;int main (){ for (n=0;n using namespace std;#define WIDTH 5#define HEIGHT 3int jimmy [HEIGHT * WIDTH];int n,m;int main (){ for (n=0;n using namespace std;void printarray (int arg[], int length) { for (int n=0; n using namespace std;int main (){ char question[] = "Please, enter your first name: "; char greeting[] = "Hello, "; char yourname [80]; cout << question; cin >> yourname; cout << greeting << yourname << "!"; return 0;}/*****************************************3Pointers*/// my first pointer#include using namespace std;int main (){ int firstvalue, secondvalue; int * mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0;}// more pointers#include using namespace std;int main (){ int firstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; // p1 = address of firstvalue p2 = &secondvalue; // p2 = address of secondvalue *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p1 p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20 cout << "firstvalue is " << firstvalue << endl; cout << "secondvalue is " << secondvalue << endl; return 0;}// more pointers#include using namespace std;int main (){ int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) cout << numbers[n] << ", "; return 0;}// more...#include using namespace std;int main (){ char * terry = "hello"; cout << *(terry+4) << endl; cout << terry[4] << endl; return 0;}// more...#include using namespace std;int main (){ char *mychar; short *myshort; long *mylong; cout< <<(unsigned)mychar<<" "< <<" "< < using namespace std;int main (){ char a; char *b; char **c; char ***d=&c; // char ****e=&d; // can.. a='z'; b=&a; c=&b; cout< < using namespace std;void increase (void* data, int psize) //void{ if ( psize == sizeof(char) ) { char* pchar; pchar=(char*)data; // (char*) ++(*pchar); } else if (psize == sizeof(int) ) { int* pint; pint=(int*)data; // (int*) ++(*pint); }}int main (){ char a = 'x'; int b = 1602; increase (&a,sizeof(a)); increase (&b,sizeof(b)); cout << a << ", " << b << endl; return 0;}// NULL pointer, void pointers#include using namespace std;int main (){ int b = 1602; int *p=0; int *t=NULL; // pointint to "nowhere" void *s=0; // point to a value that has no type p=&b; t=&b; s=&b; // cout << s << endl; // just a dress cout << *(int*)s << endl; // can't be *s return 0;}// pointer to functions#include using namespace std;int addition (int a, int b){ return (a+b); }int subtraction (int a, int b){ return (a-b); }int operation (int x, int y, int (*functocall)(int,int)){ int g; g = (*functocall)(x,y); return (g);}int main (){ int m,n; int (*minus)(int,int) = subtraction; // minus is a pointer to a function subtraction m = operation (7, 5, addition); // choice addition n = operation (20, m, minus); // choice minus cout < #include using namespace std;int main (){ int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n > p[n]; } cout << "You have entered: "; for (n=0; n #include #include using namespace std;struct movies_t { string title; int year;} mine, yours;void printmovie (movies_t movie);int main (){ string mystr; mine.title = "2001 A Space Odyssey"; mine.year = 1968; cout << "Enter title: "; getline (cin,yours.title); cout << "Enter year: "; getline (cin,mystr); stringstream(mystr) >> yours.year; cout << "My favorite movie is:\n "; printmovie (mine); cout << "And yours is:\n "; printmovie (yours); return 0;}void printmovie (movies_t movie){ cout << movie.title; cout << " (" << movie.year << ")\n";}// array of structures#include #include #include using namespace std;#define N_MOVIES 3struct movies_t { string title; int year;} films [N_MOVIES];void printmovie (movies_t movie);int main (){ string mystr; int n; for (n=0; n > films[n].year; } cout << "\nYou have entered these movies:\n"; for (n=0; n #include #include using namespace std;struct movies_t { string title; int year;};int main (){ string mystr; movies_t amovie; movies_t * pmovie; pmovie = &amovie; cout << "Enter title: "; getline (cin, pmovie->title); cout << "Enter year: "; getline (cin, mystr); (stringstream) mystr >> pmovie->year; cout << "\nYou have entered:\n"; cout << pmovie->title; cout << " (" << pmovie->year << ")\n"; return 0;}// nesting structures#include #include #include using namespace std;struct movies_t { string title; int year;};struct friends_t { string name; string email; movies_t favorite_movie;} charlie,maria;friends_t * pfriends = &charlie;int main (){ charlie.name="charlie"; cout<< charlie.name << endl; maria.favorite_movie.title="ss"; cout<< maria.favorite_movie.title << endl; charlie.favorite_movie.year = 1984; cout<< charlie.favorite_movie.year << endl; cout<< pfriends->favorite_movie.year << endl; return 0;}/***************************************6Other Data Types*/// typedef#include #include #include using namespace std;typedef struct movies_t { string title; int year;} movies_t;int main (){ typedef char C; typedef unsigned int WORD; typedef char * pChar; typedef char filed [50]; // movies_t movi; movi.title="test"; movi.year=1984; C c; WORD i; pChar p; filed f; // char f[50] f[49]='t'; cout << f[49] << endl; p = f; p[49]='p'; cout << f[49] << endl; return 0;}/*union的作用主要是节省内存空间,但现在内存都比较大,所以在一般的编程中很少用到,而且处理不好容易出错,建议非特殊情况不要使用。*/// union#include using namespace std;union mytypes_t { char c; int i; double f;} myunion;int main (){ cout<< sizeof(myunion) << endl; myunion.c='c'; cout << myunion.c << endl; myunion.i=1; // same physical space in memory cout << myunion.c << endl; return 0;}// enumerations#include using namespace std;enum colors { black, blue, green, cyan, red, purple, yellow, white};int main (){ colors mycolor; mycolor = blue; cout << mycolor << endl; mycolor = red; cout << mycolor << endl; return 0;}