Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 12, 2016
0 parents
commit 6b57220
Showing
291 changed files
with
10,746 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// hello_world1.cpp | ||
int main() | ||
{ | ||
System::Console::WriteLine("Hello, World!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// hello_world2.cpp | ||
#using "mscorlib.dll" | ||
using namespace System; | ||
|
||
int main() | ||
{ | ||
Console::WriteLine("Hello World!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( /* ... */ ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
}; |
Oops, something went wrong.