有"/n",这段用的是全局变量
//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"
using namespace System;
int main(array<System::String ^> ^args)
...{
double carpetPriceSqYd=27.95;
double roomWidth=13.5;
double roomLength=24.75;
const int 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);
return 0;
}
//Ex2_14.cpp:main project file
//Defining and using a C++/CLI enumeration.枚举
#include "stdafx.h"
using namespace System;
//Define the enumeration at global scope
enum class 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);
return 0;
}
这篇博客介绍了VC++2005中关于命名空间的使用,包括基本的全局变量操作,命名空间的声明以及using指令的应用。通过示例代码展示了如何在不同情况下读取和显示整数输入,并在Ex2_12_CLR.cpp中展示了使用System命名空间进行C#风格的Console输出。
1488

被折叠的 条评论
为什么被折叠?



