上嵌学习笔记

构造函数

2016-11-09  本文已影响6人  ie大博

构造函数

class fen
{
    int m_fenzi;
    int m_fenmu;
public:
      fen(int fenzi,int fenmu)//可以传参,这里是形参,实参在主函数一开始定义的时候传
    {
        m_fenzi=2;
        m_fenmu=10;
    }

、、、、、、、、、、、、、、、、、、、、、、、、、、
    fen()//构造函数省略了set函数,它的名字是类的名字,自动调用
    {
        m_fenzi=2;
        m_fenmu=10;
    }
    void set(int fenzi,int fenmu)
    {
        m_fenzi=fenzi;
        m_fenmu=fenmu;
    }
    void bianli()
    {
        int i;
        for(i=m_fenzi;i>=1;i--)
        {
            if(m_fenzi%i==0&&m_fenmu%i==0)
            {
                m_fenzi=m_fenzi/i;
                m_fenmu=m_fenmu/i;
                break;
            }
        
        }
    }
    void show()
    {
        cout<<m_fenzi<<"/"<<m_fenmu<<endl;
    }
    
};
fen ::  fen(int fenzi,int fenmu)///构造函数的封装,它比较不一样。
{
    m_fenzi=2;
    m_fenmu=10;
}

int main()
{
        fen t(12,15);//这里可以向构造函数传实参。
////////////////////////////////////////////////////////////////////////
    fen t;
//  t.set(12,16);
        t.bianli();
    t.show();
    return 0;
}

构造函数的数组的应用

class fen
{
    int m_fenzi;
    int m_fenmu;
public:
    fen()//他会调用的这个函数五次
    {
        m_fenzi=10;
        m_fenmu=100;
        cout<<"alex"<<endl;
    }
    void set(int fenzi,int fenmu)
    {
        m_fenzi=fenzi;
        m_fenmu=fenmu;
    }
    void show()
    {
        cout<<m_fenzi<<"/"<<m_fenmu<<endl;
    }
    
};
int main()
{
    fen t2[5];//调用无参的构造函数五次。
    return 0;
}

类中包含类,并且给类传参

class A
{
    public :
        A(int a)//这个位置需要传参
        {
            cout<<"A .alex"<<a<<endl;
        }
                 A(){}////这个是保证编译可以通过,无参的类。
};
class test
{
        A m_t;
    public :
        test():m_t(222)//给上面的类传实参
        {
            cout<<"test.alex"<<endl;
        }
};
int main()
{
    test t;
}

析构函数(逆构造函数):每个构造函数都析构函数,谁最先构造,谁最后析构

class A
{
    public :
        A(int a)//这个位置需要传参
        {
            cout<<"A .alex"<<a<<endl;
        }
};
class test
{
        A m_t1;//m_t1和m_t2的顺序决定了他的显示的结果
        A m_t2;
    public :
        test():m_t2(111),m_t1(222)//这里的m_t1和m_t2的顺序对上面不影响,给上面的类传实参。
        {
            cout<<"test.alex"<<endl;
        }
        ~test()//在主函数里面的t要消亡之前出现的函数,叫做析构造函数。
        {
            cout<<"lunch"<<endl;    
        }
};
int main()
{
    test t;
    cout<<"早餐"<<endl;
}

析构函数的指针应用

class test
{
    int *p;
public :
    test()
    {
        p=(int *)malloc(sizeof(int));//在构造函数,给p建造空间
    }
    ~test()//在主函数里面的t要消亡之前出现的函数,叫做系构造函数。
    {
        free(p);//在析构函数释放空间
    }
    void printf()
    {
        *p=4;
        cout<<"alex"<<*p<<endl;
    }
};
int main()
{
    test t;
    t.printf();
}

一些顺序的问题

using namespace std;
class test
{
    int m_data;
public:
    test(int data):m_data(data)//这里直接给m_data赋值
    {
        cout <<"test("<<m_data<<")"<<endl;
    }
    ~test()
    {
        cout<<"~test("<<m_data<<")"<<endl;
    }
};
test t1(10);
void fun()
{
    test t4(40);
    return;
}
void show()
{
    static test t5(50);
    return;
}
int main()
{
    test t2(20);
    test t3(30);
    fun();
    show;
}
test t6(60);
上一篇 下一篇

猜你喜欢

热点阅读