委派构造

//委派构造函数可以 在一个类中的构造函数的初始化表中
//使用其他构造函数
#include <iostream>
using namespace std;
class father
{
public:
    father() :father(1,'a'){ cout << "father() 无惨构造" << endl; }
    father(int a) :father(a, 'a') { cout << "father(int a) 有参构造 int" << endl; }
    father(char C) :father(1, C) { cout << "father(int C) 有参构造 char" << endl; }
    ~father() { cout << "~father" << endl; }
    void Print() {
        cout << "num = " << num ;
        cout << "   ;val = " << val << endl;
    }
    father(int x, char y) { num = x; val = y; };
private:
    
    int num{ 5 };
    char val{'c'};
};


int main()
{
    {
        father s(55555);
        s.Print();
    }
    std::cout << "Hello World!\n"; 
    std::cin.get();
}
 

猜你喜欢

转载自blog.csdn.net/weixin_42204566/article/details/84642962