类对象,无参构造

Posted on 2020-12-07 20:23  金色的省略号  阅读(115)  评论(0编辑  收藏  举报
#include <iostream>
using namespace std;

class A{
public:
    int num;
    //A():num(100) {  cout << "无参构造函数" << endl; }
};

int main()
{
    A a;
    cout << ( a.num==0 ) << endl;
    
    cout << ( A().num==0 ) << endl; //无名对象, 类名后带括号
    
    A *p1 = new A;
    cout << ( p1->num==0 ) << endl;
    
    A *p2 = new A();    //动态创建对象, 类名后带括号
    cout << ( p2->num==0 ) << endl;
    
    return 0;
}