美文网首页
枚举、随机数、嵌套类

枚举、随机数、嵌套类

作者: Hassan_chao | 来源:发表于2017-03-23 18:09 被阅读8次

枚举、随机数


#include <iostream>
#include <random>
#include <time.h>
using namespace std;
int main()
{
    int colo;
    enum Color {red,yellow,green};//声明枚举类型
    //Color pri;//定义Color类型变量pri
    int pri;
    //随机取值
    uniform_int_distribution<unsigned> u(0, 2);
    default_random_engine e(time(0));
    //cout << e << endl;
    for (size_t i = 0; i < 18; i++)
    {
        pri = u(e);
        switch (pri)
        {
        case red:
            cout << "red" << endl;
            break;
        case yellow:
            cout << "yellow" << endl;
            break;
        case green:
            cout << "green" << endl;
            break;
        default:
            break;
        }
    }
    system("pause");
    return 0;
}

嵌套类

#include <iostream>  
using namespace std;

class c1
{
public:
    int a;
    void foo();
    class c2
    {
    public:
        int a;
        void foo();
    } b;
};
void c1::foo()
{
    a = 1;
}
void c1::c2::foo()
{
    a = 2;
}

int main()
{

    class c1 f;
    f.foo();
    f.b.foo();
    cout << f.a << endl;
    cout << f.b.a << endl;
    system("pause");
    return 0;
}

局部类


#include <iostream>
using namespace std;
void fun()
{
    class MyClass
    {
    public:
        void fun1()
        {
            cout << "局部类调用成功" << endl;
        }
    };
    MyClass str;
    str.fun1();

}

int main()
{
    fun();
    system("pause");
}

相关文章

网友评论

      本文标题:枚举、随机数、嵌套类

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