Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 12, 2016
0 parents commit 6b57220
Show file tree
Hide file tree
Showing 291 changed files with 10,746 additions and 0 deletions.
Binary file added 4120.pdf
Binary file not shown.
Binary file added 4121.pdf
Binary file not shown.
Binary file added 9781430210238.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Appendix/detecting_clr.cpp
@@ -0,0 +1,10 @@
// detecting_clr.cpp
#include <stdio.h>
int main()
{
#ifdef _MANAGED
System::Console::WriteLine("Must be compiling with /clr...");
#else
printf("Not compiling with /clr.");
#endif
}
21 changes: 21 additions & 0 deletions Appendix/identifier.cpp
@@ -0,0 +1,21 @@
// identifier.cpp
using namespace System;

int main()
{
int __identifier(switch) = 10;

__identifier(switch)++;

switch( __identifier(switch) )
{
case 10:
break;
case 11:
Console::WriteLine("Switch is {0}", __identifier(switch));
break;
default:
break;
}

}
27 changes: 27 additions & 0 deletions Appendix/xml_comments.cpp
@@ -0,0 +1,27 @@
// xml_comments.cpp
// compile with: /LD /clr /doc
// then run: xdcmake xml_comments.xdc

using namespace System;

/// Ref class R demonstrates XML Documentation Comments.
/// <summary> A class demonstrating documentation comments </summary>
/// <remarks> A detailed description of R goes into the remarks block
/// </remarks>
public ref class R
{
public:
/// <summary>F is a method in the R class.
/// <para>You can break the comments into paragraphs.
/// <see cref="R::G"/> for related information.</para>
/// <seealso cref="R::G"/>
/// </summary>
void F(int i) {}

/// The method G is a method in the R class.
/// <summary>Counts the number of characters in two strings.</summary>
/// <param name="s1"> Description for s1</param>
/// <param name="s2"> Description for s2</param>
/// <returns>The sum of the length of two strings.</returns>
int G(String^ s1, String^ s2){ return s1->Length + s2->Length; }
};
41 changes: 41 additions & 0 deletions Chapter01/greeting.cpp
@@ -0,0 +1,41 @@
// greeting.cpp
using namespace System;

value struct Greeting
{
String^ greeting;
Char punctuator;

void PrintGreeting(String^ name)
{
Console::WriteLine(greeting + name + punctuator);
}
};

int main(array<String^>^ args)
{
Greeting greet;
greet.greeting = "Hi ";
greet.punctuator = '!';

if (args->Length < 1)
{
Console::WriteLine("Enter names on the command line, like this:"
" greeting <name1> <name2> ...");
Console::WriteLine("Use quotes around names with spaces.");
return 1;
}

for (int i = 0; i < args->Length; i++)
{
greet.PrintGreeting(args[i]);
}

greet.greeting = "Hello, ";
greet.punctuator = '.';

for each (String^ s in args)
{
greet.PrintGreeting(s);
}
}
53 changes: 53 additions & 0 deletions Chapter01/hello_interop.cpp
@@ -0,0 +1,53 @@
// hello_interop.cpp

#include <stdio.h>
#include <string>

class HelloNative
{
private:
// string is a Stanard C++ class (actually a typedef for basic_string<char>)
// where each character is represented by a char
std::string* s;

public:
HelloNative()
{
s = new std::string("Hello from native type!");
}
void Greeting()
{
// The C++ basic_string class contains a method that returns a
// “C string” of type “const char *” for the C runtime function printf.
printf("%s\n", s->c_str());
}
~HelloNative()
{
delete s;
}
};

ref class HelloManaged
{
private:
System::String^ s;

public:
HelloManaged()
{
s = "Hello from managed type!";
}
void Greeting()
{
System::Console::WriteLine("{0}", s);
}
};

int main()
{
HelloNative* helloNative = new HelloNative();
HelloManaged^ helloManaged = gcnew HelloManaged();

helloNative->Greeting();
helloManaged->Greeting();
}
5 changes: 5 additions & 0 deletions Chapter01/hello_world1.cpp
@@ -0,0 +1,5 @@
// hello_world1.cpp
int main()
{
System::Console::WriteLine("Hello, World!");
}
8 changes: 8 additions & 0 deletions Chapter01/hello_world2.cpp
@@ -0,0 +1,8 @@
// hello_world2.cpp
#using "mscorlib.dll"
using namespace System;

int main()
{
Console::WriteLine("Hello World!");
}
33 changes: 33 additions & 0 deletions Chapter01/hello_world3.cpp
@@ -0,0 +1,33 @@
// hello_world3.cpp

using namespace System;

ref class Hello
{
String^ greeting;

public:

Hello(String^ str) : greeting(str)
{ }

void Greet()
{
Console::WriteLine(greeting + "!");
}

void SetGreeting(String^ newGreeting)
{
greeting = newGreeting;
}
};

int main()
{
Hello^ hello = gcnew Hello("Hi there!");
hello->SetGreeting("Hello World");
hello->Greet();
hello->SetGreeting("Howdy");
hello->Greet();
}

25 changes: 25 additions & 0 deletions Chapter01/hello_world4.cpp
@@ -0,0 +1,25 @@
// hello_world4.cpp
using namespace System;

value class Hello
{
String^ greeting = "Hi there";

public:
void Greet()
{
Console::WriteLine(greeting + "!");
}

void SetGreeting(String^ newGreeting)
{
greeting = newGreeting;
}

};

int main()
{
Hello hello;
hello.Greet("Hello World");
}
14 changes: 14 additions & 0 deletions Chapter01/value_type_with_members.cpp
@@ -0,0 +1,14 @@
// value_type_with_members.cpp

using namespace System;

value struct Greeting
{
String^ greeting;
Char punctuator;

void PrintGreeting(String^ name)
{
Console::WriteLine(greeting + name + punctuator);
}
};
29 changes: 29 additions & 0 deletions Chapter02/atom.cpp
@@ -0,0 +1,29 @@
// atom.cpp
class Atom
{
private:
double pos[3];
unsigned int atomicNumber;
unsigned int isotopeNumber;

public:
Atom() : atomicNumber(1), isotopeNumber(1)
{
// Let's say we most often use hydrogen atoms,
// so there is a default constructor that assumes you are
// creating a hydrogen atom.
pos[0] = 0; pos[1] = 0; pos[2] = 0;
}

Atom(double x, double y, double z, unsigned int a, unsigned int n)
: atomicNumber(a), isotopeNumber(n)
{
pos[0] = x; pos[1] = y; pos[2] = z;
}
unsigned int GetAtomicNumber() { return atomicNumber; }
void SetAtomicNumber(unsigned int a) { atomicNumber = a; }
unsigned int GetIsotopeNumber() { return isotopeNumber; }
void SetIsotopeNumber(unsigned int n) { isotopeNumber = n; }
double GetPosition(int index) { return pos[index]; }
void SetPosition(int index, double value) { pos[index] = value; }
};
28 changes: 28 additions & 0 deletions Chapter02/atom_managed.cpp
@@ -0,0 +1,28 @@
// atom_managed.cpp
ref class Atom
{
private:
array<double>^ pos; // declare the managed array
unsigned int atomicNumber;
unsigned int isotopeNumber;

public:
Atom()
{
// We'll need to allocate space for the position values.
pos = gcnew array<double>(3);
pos[0] = 0; pos[1] = 0; pos[2] = 0;
atomicNumber = 1;
isotopeNumber = 1;
}
Atom(double x, double y, double z, unsigned int atNo, unsigned int n)
: atomicNumber(atNo), isotopeNumber(n)
{
// create the managed array
pos = gcnew array<double>(3);
pos[0] = x; pos[1] = y; pos[2] = z;
}
// the rest of the class declaration is unchanged
};


31 changes: 31 additions & 0 deletions Chapter02/atom_valuetype.cpp
@@ -0,0 +1,31 @@
// atom_valuetype.cpp
value class Atom
{
private:
array<double>^ pos; // declare the managed array
unsigned int atomicNumber;
unsigned int isotopeNumber;

public:

void Initialize()
{

}
// the rest of the class declaration is unchanged
};

void atoms()
{
int n_atoms = 50;
array<Atom>^ atoms = gcnew array<Atom>(n_atoms);

// Between the array creation and initialization,
// the atoms are in an invalid state.
// Don't call GetAtomicNumber here!

for (int i = 0; i < n_atoms; i++)
{
atoms[i].Initialize( /* ... */ );
}
}
52 changes: 52 additions & 0 deletions Chapter02/atom_with_enum.cpp
@@ -0,0 +1,52 @@
// atom_with_enum.cpp

using namespace System;

value struct Point3D
{
double x;
double y;
double z;

};

enum class Element
{
Hydrogen = 1, Helium, Lithium, Beryllium, Boron, Carbon, Nitrogen, Oxygen,
Fluorine, Neon
// ... 100 or so other elements omitted for brevity
};


ref class Atom
{
private:
Point3D position;
unsigned int atomicNumber;
unsigned int isotopeNumber;

public:
Atom(Point3D pos, unsigned int a, unsigned int n)
: position(pos), atomicNumber(a), isotopeNumber(n)
{ }

// ...

Element GetElementType()
{
return safe_cast<Element>( atomicNumber );
}
void SetElementType(Element element)
{
atomicNumber = safe_cast<unsigned int>(element);
}
String^ GetElementString()
{
return GetElementType().ToString();
}



// the rest of the code is unchanged

};

0 comments on commit 6b57220

Please sign in to comment.