//Ex2_09.cpp //Demonstration namespace names #include <iostream> int value=0; int main() { std::cout<<"enter an integer:"; std::cin>>value; std::cout<<"/nYou entered "<<value<<std::endl; return 0; }
有"/n",这段是关于命名空间的声明的
//Ex2_10.cpp //Declaring a namespace 声明命名空间 #include <iostream> namespace myStuff { int value=0; } int main() { std::cout<<"enter an integer:"; std::cin>>myStuff::value; std::cout<<"/nYou entered "<<myStuff::value <<std::endl; return 0; }
//Ex2_11.cpp //using a using directive #include <iostream> namespace myStuff { int value=0; } using namespace myStuff; int main() { std::cout<<"enter an integer:"; std::cin>>value; std::cout<<"/nYou entered "<<value; std::cout<<std::endl; return 0; }
// Ex2_12_CLR.cpp: 主项目文件。
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args) { int apples,oranges; int fruit; apples=5;oranges=6; fruit=apples+oranges; Console::WriteLine(L"/nOranges are not the only fruit...");//“L”的含义指后面的字符是宽字符(每个字符,包括英文占2字节) Console::Write(L" And we have ");//Write 写在同一行,WriteLine换行。 Console::Write(fruit); Console::Write(L" fruits in all./n"); return 0; }
//Ex2_13.cp:main project file. //Calculating the price of a carpet #include "stdafx.h" usingnamespace System; int main(array<System::String ^>^args) ...{ double carpetPriceSqYd=27.95; double roomWidth=13.5; double roomLength=24.75; constint feetPerYard=3; double roomWidthYds=roomWidth/feetPerYard; double roomLengthYds=roomLength/feetPerYard; double carpetPrice=roomWidthYds*roomLengthYds*carpetPriceSqYd; Console::WriteLine(L"Room size is {0:F2} yards by {1:F2} yards",roomLengthYds,roomWidthYds); //{0:F2}中的0表示WriteLine的第一个参数的该位置由WriteLine的第二个参数来填补。F2表示输出形式(格式)是小数,且保留2位小数 Console::WriteLine(L"Room area is {0:F2} square yards",roomLengthYds*roomWidthYds); Console::WriteLine(L"Carpet price is ${0:F2}",carpetPrice); return0; }
//Ex2_14.cpp:main project file //Defining and using a C++/CLI enumeration.枚举 #include "stdafx.h" usingnamespace System; //Define the enumeration at global scope enumclass Suit ...{ Clubs,Diamonds,Hearts,Spades }; int main(array<System::String ^>^args) ...{ Suit suit =Suit::Clubs; int value=safe_cast<int>(suit); Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value); suit=Suit::Diamonds; value=safe_cast<int>(suit); Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value); suit=Suit::Hearts; value=safe_cast<int>(suit); Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value); suit=Suit::Spades; value=safe_cast<int>(suit); Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value); return0; }