美文网首页
2020-12-21 C++ Primer Plus 第九章

2020-12-21 C++ Primer Plus 第九章

作者: Reza_ | 来源:发表于2020-12-22 10:11 被阅读0次

9.5复习题

a.自动
b.静态(外部链接性)
c.静态(内部链接性)static
d.静态(无链接性)

using声明:将特定的名称添加到它所属的声明区域中
using编译:它使名称空间中的所有名称都可用

#include <iostream>
int main()
{
    double x;
    std::cout << "Entre value: ";
    while (!(std::cin>>x))
    {
        std::cout << "Bad input .Please enter a number: ";
        std::cin.clear(); //清除错误
        while (std::cin.get()!='\n')
            continue;
    }
    std::cout << "Value = "<< x << std::endl;
    return 0;

}
#include <iostream>
int main()
{
    using std::cout;
    using std::cin;
    using std::endl;
    double x;
    cout << "Entre value: ";
    while (!(cin>>x))
    {
        cout << "Bad input .Please enter a number: ";
        cin.clear(); //清除错误
        while (cin.get()!='\n')
            continue;
    }
    cout << "Value = "<< x << endl;
    return 0;
}

file1.cpp

#include <iostream>
namespace file1{
    int average(int a,int b)
    {
        return (a+b)/2;
    }
}
int main()
{
    int s1=1,s2=2;
    int mean=file1::average(s1,s2);
    std::cout << mean << std::endl;
}

file2.cpp

#include <iostream>
namespace file2{
    double average(int a,int b)
    {
        double sum=a+b;
        return sum/2;
    }
}
int main()
{
    int s1=1,s2=2;
    double mean=file2::average(s1,s2);
    std::cout << mean << std::endl;
}

file1.cpp

#include <iostream>
using namespace std;
void other();
void another();
int x = 10;
int y;
int main()
{
    cout << x << endl;      //x=10
    {
        int x = 4;
        cout << x << endl;   //x=4
        cout << y << endl;   //y=0,未被初始化的静态变量的所有位都被设置为 0
    }
    other();    //x=10,y=1
    another();  //x=10,y=4
    return 0;
}
void other()
{
    int y=1;
    cout << "Other: " << x << ", "<< y << endl;
}

file2.cpp

#include <iostream>
using namespace std;
extern int x;
namespace        //未命名的名称空间,作用域为:从声明点到该声明区域末尾
{
    int y=-4;
}
void another()
{
    cout << "another(): " << x << ", " << y << endl;
}

输出为
10
4
0
Other: 10,1
another():10, -4

#include <iostream>
using namespace std;
void other();
namespace n1
{
    int x = 1;
}
namespace n2
{
    int x = 2;
}

int main()
{
    using namespace n1;
    cout << x << endl;   //x=1
    {
        int x = 4;
        cout << x << ", " <<n1::x<<", "<<n2::x <<endl; //x=4,1,2
    }
    using n2::x;
    cout << x << endl; //x=2
    other();
    return 0;
}

void other()
{
    using namespace n2;
    cout << x << endl; //x=2
    {
        int x = 4;
        cout << x << ", " << n1::x << ", " << n2::x << endl; //x=4,1,2
    }
    using n2::x;
    cout << x << endl;//x=2
}

输出为:
1
4,1,2
2
2
4,1,2
2

相关文章

网友评论

      本文标题:2020-12-21 C++ Primer Plus 第九章

      本文链接:https://www.haomeiwen.com/subject/wsnrnktx.html