Arrays:
int billy[5];
Initializing arrays:
int billy[5] = {16, 2, 77, 40, 12071};
Accessing the values of an array:
billy[2] = 75;
array.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // arrays example
#include <iostream>
using namespace std;
int billy[] = {1, 2, 3, 4, 5};
int n;
int result = 0;
int main(void){
for(n = 0; n < 5; n++){
result += billy[n];
}
cout << result << endl;
return 0;
}
|
array.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./array
15
Multidimensional arrays:
int jimmy[3][5];
jimmy[1][3]
Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices:
int jimmy[3][5]; // is equivalent to
int jimmy[15]; // (3*5=15)
None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:
Arrays as parameters:
void procedure(int arg[]);
int myarray[40];
procedure(myarray);
array-para.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // arrays as parameters
#include <iostream>
using namespace std;
void printarray(int arg[], int length){
for(int n = 0; n < length; n++)
cout << arg[n] << endl;
}
int main(void){
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray(firstarray, 3);
printarray(secondarray, 5);
return 0;
}
|
array-para.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./array-para
5
10
15
2
4
6
8
10
Initialization of null-terminated character sequences:
char myword[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char myword[] = "Hello";
using null-terminated sequences of characters
chara.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // null-terminated sequences of characters
#include <iostream>
using namespace std;
int main(void){
char question[] = "Please, enter your first name: ";
char greeting[] = "Hello, ";
char yourname[80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!" << endl;
return 0;
}
|
chara.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./chara
Please, enter your first name: wtopia
Hello, wtopia!
pointer1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // my first pointer
#include <iostream>
using namespace std;
int main(void){
int firstvalue, secondvalue;
int *mypointer;
mypointer = &firstvalue;
*mypointer = 100;
mypointer = &secondvalue;
*mypointer = 200;
cout << "firstvalue is: " << firstvalue << endl;
cout << "secondvalue is: " << secondvalue << endl;
return 0;
}
|
pointer1.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./pointer1
firstvalue is: 100
secondvalue is: 200
pointer2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // more pointers
#include <iostream>
using namespace std;
int main(void){
int firstvalue = 5;
int secondvalue = 15;
int *p1;
int *p2;
p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10;
p2 = p1; // p2 = address of pointer p1
cout << "firstvalue is: " << firstvalue << endl;
cout << "secondvalue is: " << secondvalue << endl;
return 0;
}
|
pointer2.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./pointer2
firstvalue is: 10
secondvalue is: 15
Pointers and arrays:
int numbers[20];
int *p;
p = numbers;
ptoarray.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // more pointers
#include <iostream>
using namespace std;
int main(void){
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] << endl;
return 0;
}
|
ptoarray.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./ptoarray
10
20
30
40
50
Pointer initialization:
int number;
int *tommy = &number;
int number;
int *tommy;
tommy = &number;
Pointer arithmetics:
char *mychar;
short *myshort;
long *mylong;
mychar++;
myshort++;
mylong++;
Pointers to pointers:
char a;
char *b;
char *c;
a = 'z';
b = &a;
c = &b;
void pointers
voidptr.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // increaser
#include <iostream>
using namespace std;
void increase(void *data, int psize){
if ( psize == sizeof(char) ){
char *pchar;
pchar = (char *)data;
(*pchar)++;
}
else if( psize == sizeof(int) ){
int *pint;
pint = (int *)data;
(*pint)++;
}
}
int main(void){
char a = 'x';
int b = 1999;
increase( &a, sizeof(a) );
increase( &b, sizeof(b) );
cout << a << endl;
cout << b << endl;
return 0;
}
|
voidptr.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./voidptr
y
2000
Null pointer:
int *p;
p = 0; // p has a null pointer value
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.
Do not confuse null pointers with void pointers. A null pointer is a value that any pointer may take to represent that it is pointing to “nowhere”, while a void pointer is a special type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pinter itself and the other to the type of data it points to.
Pointers to functions
ptofunc.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // pointer to functions
#include <iostream>
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(void){
int m, n;
int (*minus)(int, int) = subtraction;
m = operation(7, 5, addition);
n = operation(20, m, minus);
cout << "m = " << m << endl;
cout << "n = " << n << endl;
return 0;
}
|
ptofunc.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./ptofunc
m = 12
n = 8
dyna-mem.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // rememb-o-matic
#include <iostream>
#include <new>
using namespace std;
int main(void){
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 < i; n++){
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for(n = 0; n < i; n++){
cout << p[n] << " ";
}
cout << endl;
delete[] p;
}
return 0;
}
|
dyna-mem.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./dyna-mem
How many numbers would you like to type? 5
Enter number: 10
Enter number: 20
Enter number: 30
Enter number: 40
Enter number: 50
You have entered: 10 20 30 40 50
Data structures
Data structures are declared in c++ using the following syntax:
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
For example1:
struct product{
int weight;
float price;
};
product apple;
product banana;
product melon;
For example2:
struct product{
int weight;
float price;
}apple, banana, melon;
struct.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t{
string title;
int year;
}mine, yours;
void printmovie(movies_t movie);
int main(void){
string mystr;
mine.title = "Biohazard";
mine.year = 2010;
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";
}
|
struct.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./struct
Enter title: Red Hill
Enter year: 2009
My favorite movie is:
Biohazard (2010)
And yours is:
Red Hill (2009)
array-struct.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define N_MOVIES 3
struct movies_t{
string title;
int year;
}films[N_MOVIES];
void printmovie(movies_t movie);
int main(void){
string mystr;
int n;
for(n = 0; n < N_MOVIES; n++){
cout << "Enter title: ";
getline(cin, films[n].title);
cout << "Enter year: ";
getline(cin, mystr);
stringstream(mystr) >> films[n].year;
}
cout << "\nYou have entered these movies:\n";
for(n = 0; n < N_MOVIES; n++)
printmovie(films[n]);
return 0;
}
void printmovie(movies_t movie){
cout << movie.title;
cout << " (" << movie.year << ") \n";
}
|
array-struct.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./array-struct
Enter title: Orphan
Enter year: 2009
Enter title: The Figher
Enter year: 2009
Enter title: Salt
Enter year: 2010
You have entered these movies:
Orphan (2009)
The Figher (2009)
Salt (2010)
Pointers to structures
p-to-struct.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t{
string title;
int year;
};
int main(void){
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;
}
|
p-to-struct.cpp の実行結果は:
[cactus:~/code_c++/cpp_tuts]% ./p-to-struct
Enter title: Love Story
Enter year: 2010
You have entered:
Love Story (2010 )
Nesting structures:
struct movies_t{
string title;
int year;
};
struct friends_t{
string name;
string email;
movies_t favorite_movie;
}charlie, maria;
friends_t *pfriends = &charlie;
charlie.name
maria.favorite_movie.title
charlie.favorite_movie.year
pfriends->favorite_movie.year
Defined data types(typedef)
typedef existing_type new_type_name:
typedef char c;
typedef unsigned int WORD;
typedef char * pChar;
typedef char field[50];
c mychar;
WORD myword;
pChar ptc2;
field name;
Unions:
union union_name{
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
}object_names;
union mytypes_t{
char c;
int i;
float f;
}mytypes;
defines three elements:
mytypes.c;
mytypes.i;
mytypes.f;
One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. For example:
union mix_t{
long l;
struct{
short hi;
short lo;
}s;
char c[4];
}mix;
Anonymous unions
The only difference between the two pieces of code is that in the first one we have given a name to the union (price) and in the second one we have not. The difference is seen when we access the members dollars and yens of an object of this type. For an object of the first type, it would be:
book.price.dollars;
book.price.yens;
whereas for an object of the second type, it would be:
book.dollars; book.yens;
Enumerations(enum)
Enumerations create new data types to contain something different that is not limited to the values fundamental data types may take. Its form is the following:
enum enumeration_name{
value1,
value2,
value3,
.
.
}object_names;
For example, we could create a new type of variable called colors_t to store colors with the following declaration:
enum colors_t{
black,
blue,
green,
cyan,
red,
purple,
yellow,
white
};
colors_t mycolor;
mycolor = blue;
if (mycolor == green) mycolor = red;
Enumerations are type compatible with numeric variables, so their constants are always assigned an integer numerical value internally. If it is not specified, the interger value equivalent to the first possible value is equivalent to 0 and the following ones follow a + 1 progression. Thus, in our data type colors_t that we have defined above, black would be equivalent to 0, blue would be equivalent to 1, green to 2, and so on.
We can explicitly specify an integer value for any of the constant values that our enumerated type can take. If the constant value that follows it is not given an integer value, it is automatically assumed the same value as the previous one plus one. For example:
enum months_t{
january = 1,
february,
march,
april,
may,
june,
july,
august,
september,
october,
november,
december
}y2k;
In this case, variable y2k of enumerated type months_t can contain any of the 12 possible values that go from january to december and that are equivalent to values between 1 and 12 (not between 0 to 11, since we have made janauary equal to 1).