Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 13, 2016
0 parents commit d295d52
Show file tree
Hide file tree
Showing 1,113 changed files with 77,761 additions and 0 deletions.
Binary file added 4238.pdf
Binary file not shown.
Binary file added 4239.pdf
Binary file not shown.
Binary file added 9781430210535.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions Chapter02/Arrays.cpp
@@ -0,0 +1,76 @@
using namespace System;

// Arrays in Action
void main()
{
// Single dimension
array<int>^ a = gcnew array<int>(4);
array<String^>^ b = gcnew array<String^>(4);

for (int i = 0; i < a->Length; i++)
{
a[i] = i;
}

for (int i = 0; i < b->Length; i++)
{
b[i] = a[i].ToString();
}

for (int i = 0; i < b->Length; i++)
{
Console::WriteLine(b[i]);
}

Console::WriteLine();
Array::Reverse(b);
for (int i = 0; i < b->Length; i++)
{
Console::WriteLine(b[i]);
}

// Multi dimension uniform
array<int,2>^ c = gcnew array<int,2>(4,3);
array<String^,2>^ d = gcnew array<String^,2>(4,3);

for (int x = 0; x < c->GetLength(0); x++)
{
for (int y = 0; y < c->GetLength(1); y++)
{
c[x,y] = (x*10)+y;
}
}

Console::WriteLine();
for (int x = 0; x < d->GetLength(0); x++)
{
for (int y = 0; y < d->GetLength(1); y++)
{
Console::Write("{0,-5:00}", c[x,y]);
}
Console::WriteLine();
}

// Multi dimension jagged
array< array<int>^ >^ e = gcnew array<array<int>^>(4);

for (int x = 0; x < e->Length; x++)
{
e[x] = gcnew array<int>(4+(x*2)); // each row 2 bigger
for(int y = 0; y < e[x]->Length; y++)
{
e[x][y] = (x*10)+y;
}
}

Console::WriteLine();

for (int x = 0; x < e->Length; x++)
{
for (int y = 0; y < e[x]->Length; y++)
{
Console::Write("{0,-5:00}", e[x][y]);
}
Console::WriteLine();
}
}
15 changes: 15 additions & 0 deletions Chapter02/Boolean.cpp
@@ -0,0 +1,15 @@
using namespace System;

// Boolean Fundamental Type in Action
void main()
{
bool a = 18757; // will give a warning but set to true
bool b = 0; // false
bool c = true; // obviously true
bool d = false; // obviously false

Console::WriteLine( a );
Console::WriteLine( b );
Console::WriteLine( c );
Console::WriteLine( d );
}
15 changes: 15 additions & 0 deletions Chapter02/BooleanLiteral.cpp
@@ -0,0 +1,15 @@
using namespace System;

// Boolean Literals in Action
void main()
{
bool isTrue = true;
bool isFalse = false;

Console::WriteLine ( isTrue );
Console::WriteLine ( isFalse );

// This is kind of neat. Boolean literals are objects too!
Console::WriteLine ( true.ToString () );
Console::WriteLine ( false.ToString () );
}
20 changes: 20 additions & 0 deletions Chapter02/Boxing.cpp
@@ -0,0 +1,20 @@
using namespace System;

// Boxing in Action
value class POINT
{
public:
int x, y;
POINT(int x, int y) : x(x) , y(y) {}
};

void main()
{
POINT p1(1,2);
Object ^o = p1;
POINT ^p2 = (POINT)o;

Console::WriteLine("p1 x={0} y={1}\n", p1.x, p1.y);
Console::WriteLine("o x={0} y={1}\n", ((POINT)o).x, ((POINT)o).y);
Console::WriteLine("p2 x={0} y={1}\n", p2->x, p2->y);
}
18 changes: 18 additions & 0 deletions Chapter02/BuildAll.bat
@@ -0,0 +1,18 @@
cl Arrays.cpp /clr:safe
cl Boolean.cpp /clr:safe
cl BooleanLiteral.cpp /clr:safe
cl Boxing.cpp /clr:safe
cl CharLiteral.cpp /clr:safe
cl Chars.cpp /clr:safe
cl Decimal.cpp /clr:safe
cl Enums.cpp /clr:safe
cl FloatingPoint.cpp /clr:safe
cl Hello.cpp /clr:safe
cl IntegerLiteral.cpp /clr:safe
cl IntegerTypes.cpp /clr:safe
cl MainArgsNew.cpp /clr:safe
cl MainArgsTrad.cpp /clr:pure
cl ReferenceIndirect.cpp /clr:pure
cl StringFun.cpp /clr:safe
cl StringLiteral.cpp /clr:safe
cl ValueClass.cpp /clr:safe
21 changes: 21 additions & 0 deletions Chapter02/CharLiteral.cpp
@@ -0,0 +1,21 @@
using namespace System;

// Character Literals in Action
void main()
{
char a = 'a'; // character 'a'
Char b = L'b'; // Unicode 'b'

char t = '\t'; // tab escape
Char s = L'\\'; // Unicode backslash escape

char d = '\45'; // octal escape
Char e = L'\x0045'; // Unicode hexadecimal escape

Console::WriteLine ( a ); // displays numeric equiv of 'A'
Console::WriteLine ( b ); // displays the letter 'b'
Console::WriteLine ( t ); // displays numeric equiv of tab
Console::WriteLine ( s ); // displays backslash
Console::WriteLine ( d ); // displays decimal equiv of octal 45
Console::WriteLine ( e ); // displays the letter 'e'
}
12 changes: 12 additions & 0 deletions Chapter02/Chars.cpp
@@ -0,0 +1,12 @@
using namespace System;

// Character Fundamental Type in Action
void main()
{
Char a = L'A'; // character literal 'A'
Char b = L'\x0041'; // hex notation for hex 41 which happens to be 'A'

Console::WriteLine ( a );
Console::WriteLine ( b ); //Even though I put hex 41 in b, the ASCII 'A'
//is printed due to b being a Char
}
39 changes: 39 additions & 0 deletions Chapter02/Decimal.cpp
@@ -0,0 +1,39 @@
using namespace System;

// Decimal Fundamental Type in Action
void main()
{
Decimal w = System::Convert::ToDecimal("123456789012345678901.2345678");
Console::WriteLine( w );

Decimal x = (Decimal)0.1234567890123456789012345678; // will get truncated
Decimal y = (Decimal)0.0000000000000000789012345678; // works fine

Console::WriteLine( x );
Console::WriteLine( y );

// Decimal constructor
Decimal z(0xeb1f0ad2, 0xab54a98c, 0, false, 0); // = 12345678901234567890
Console::WriteLine( z );

// Create a 28 significate digit number
Decimal a = (Decimal)123456789012345000000.00000000;
Decimal b = (Decimal)678901.23456780;
Decimal c = -(a + b);

Console::WriteLine( c ); // display pre broken Decimal

// Break it up into 4 parts
array<int>^ d = Decimal::GetBits(c);

// Reassemble using Decimal constructor
Decimal e(d[0], d[1], d[2], // digits
((d[3] & 0x80000000) == 0x80000000), // sign
((d[3] >> 16) & 0xff) ); // decimal location

Console::WriteLine( d[0] ); // display part 1
Console::WriteLine( d[1] ); // display part 2
Console::WriteLine( d[2] ); // display part 3
Console::WriteLine( d[3].ToString("X") ); // display part 4
Console::WriteLine( e ); // display reassembled Decimal
}
26 changes: 26 additions & 0 deletions Chapter02/Enums.cpp
@@ -0,0 +1,26 @@
using namespace System;

enum class PrimeColors { Red, Blue, Yellow };

// Enum Type in Action
void main()
{
PrimeColors color;

color = PrimeColors::Blue;

switch (color)
{
case PrimeColors::Red :
Console::WriteLine("Red");
break;
case PrimeColors::Blue :
Console::WriteLine("Blue");
break;
case PrimeColors::Yellow :
Console::WriteLine("Yellow");
break;
}

Console::WriteLine(color.ToString());
}
15 changes: 15 additions & 0 deletions Chapter02/FloatingPoint.cpp
@@ -0,0 +1,15 @@
using namespace System;

// Floating-point Fundamental Types in Action
void main()
{
float w = 123.456f; // standard decimal notation
float x = 7890e3f; // exponent notation
double y = 34525425432525764765.76476476547654; // too big will truncate
double z = 123456789012345e-300; // exponent will be reset

Console::WriteLine( w ); // Write out Single
Console::WriteLine( x ); // Write out Single with more zeros
Console::WriteLine( y ); // Write out Double truncated
Console::WriteLine( z ); // Write out Double shift back decimal
}
7 changes: 7 additions & 0 deletions Chapter02/Hello.cpp
@@ -0,0 +1,7 @@
using namespace System;

// The Obligatory Hello World!
void main(void)
{
Console::WriteLine("Hello C++/CLI World");
}
15 changes: 15 additions & 0 deletions Chapter02/IntegerLiteral.cpp
@@ -0,0 +1,15 @@
using namespace System;

// Integer Literals in Action
void main()
{
Console::WriteLine ( 010 ); // An Octal 10 is a base-10 8
Console::WriteLine ( -010 ); // Negative Octal 10 is a base-10 -8

Console::WriteLine ( 0x10 ); // A Hex 10 is a base-10 16
Console::WriteLine ( -0x10 ); // Negative Hex 10 is a base-10 -16

// This is kind of neat. Number literals are objects too!
Console::WriteLine ( (1234567890).ToString() );
Console::WriteLine ( (0xABCDEF).ToString("X") );
}
18 changes: 18 additions & 0 deletions Chapter02/IntegerTypes.cpp
@@ -0,0 +1,18 @@
using namespace System;

// Integer Fundamental Types in Action
void main()
{
char v = 'F'; // Intialize using charater literal
short w(123); // Intializing using Functional Notation
int x = 456789; // Decimal literal assigned
long y = 987654321l; // long integer literal assigned
Int64 z = 0xFEDCBA9876543210; // Hex literal assigned

Console::WriteLine( v ); // Write out a char
Console::WriteLine( w ); // Write out a short
Console::WriteLine( x ); // Write out a int
Console::WriteLine( y ); // Write out a long
Console::WriteLine( z ); // Write out a Int64
Console::WriteLine( z.ToString("x") ); // Write out a Int64 in Hex
}
14 changes: 14 additions & 0 deletions Chapter02/MainArgsNew.cpp
@@ -0,0 +1,14 @@
using namespace System;

// Passing parameters to main() new method
int main(array<System::String ^> ^args)
{
Console::WriteLine(args->Length);

for each (String^ s in args)
{
Console::WriteLine(s);
}
return 0;
}

12 changes: 12 additions & 0 deletions Chapter02/MainArgsTrad.cpp
@@ -0,0 +1,12 @@
using namespace System;

// Passing parameters to main()
int main ( int argc, char *argv[] )
{
Console::WriteLine ( argc.ToString() );
for (int i = 0; i < argc; i++)
{
Console::WriteLine ( gcnew String(argv[i]) );
}
return 0;
}
38 changes: 38 additions & 0 deletions Chapter02/ReferenceIndirect.cpp
@@ -0,0 +1,38 @@
using namespace System;

ref class RefClass
{
public:
int X;

RefClass(int x)
{
X = x;
}
};

// Reference and Indirection in Action
void main()
{
RefClass rc(10);
RefClass ^o;

o = %rc; // place a reference of rc in the handle o
Console::WriteLine(o->X); // print out object. This should contain 10.

rc.X = 20; // place 50 at the address y points to
Console::WriteLine(o->X); // print out object. This should contain 20.

int %i = rc.X; // assign rc.X to a reference

i = 30; // change value of reference
Console::WriteLine(o->X); // print out object. This should contain 30.

Console::WriteLine();

int ^y = gcnew int(100); // create a handle to an int
Console::WriteLine(y); // print out int.

*y = 110; // Assign new value to dereferenced int
Console::WriteLine(*y); // print out dereferenced int.
}

0 comments on commit d295d52

Please sign in to comment.