[VC++2005入门经典]第二章源代码分析_上部Ex2_10至Ex2_14

这篇博客介绍了VC++2005中关于命名空间的使用,包括基本的全局变量操作,命名空间的声明以及using指令的应用。通过示例代码展示了如何在不同情况下读取和显示整数输入,并在Ex2_12_CLR.cpp中展示了使用System命名空间进行C#风格的Console输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有"/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;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值