首页 > 试题广场 >

阅读该程序,给出程序的输出结果。

[问答题]

阅读该程序,给出程序的输出结果。

#include <iostream.h>

class A

{

public:

    A()

    {

        a1=a2=0;

        cout<<"Default constructor called.\n";

    }

    A(int i,int j);

    ~A()

    {

        cout<<"Destructor called.\n";

    }

    void Print()

    {

        cout<<"a1="<<a1<<','<<"a2="<<a2<<endl;

    }

private:

    int a1,a2;

};

A::A(int i,int j)

{

    a1=i;

    a2=j;

    cout<<"Constructor called.\n";

}

void main()

{

    A a,b(5,8);

    a.Print();

    b.Print();

}
A a调用默认构造函数,A b调用带参构造函数,在分别调用Print()输出a1,a2的值,之后隐形调用析构函数。

Default constructor called.
Constructor called.
a1=0,a2=0
a1=5,a2=8
Destructor called.
Destructor called.
发表于 2020-12-21 21:04:43 回复(0)