{ return 0; }
答:运行结果是 Counting at0 Counting at1 Counting at2 Counting at3 Counting at4 Counting at5 Counting at6 Counting at7 Counting at8 Counting at9
【3.22】 写出下面程序的运行结果。 #include
using namespace std; class A{ int a,b;
public: A() {a=0; b=0;
cout<<\ }
A(int i,int j) {a=i; b=j;
cout<<\ } }; int main() {A a[3];
A b[3]={A(1,2),A(3,4),A(5,6)}; return 0; }
答:运行结果是
Default constructor called. Default constructor called. Default constructor called. Constructor: a=1,b=2 Constructor: a=3,b=4 Constructor: a=5,b=6
【3.23】 写出下面程序的运行结果。 #include
using namespace std; class Test{ private: int val; public: Test()
{ cout<<\ } Test(int n) { val=n;
cout<<\ }
Test(const Test& t) {val=t.val;
cout<<\ }
}; int main() {Test t1(6); Test t2=t1; Test t3; t3=t1; return 0; }
答:运行结果是 Con. Copy con. default.
【3.24】 写出下面程序的运行结果。 #include
using namespace std; class N{ private: int A;
static int B; public: N(int a) {A=a; B+=a; }
static void f1(N m); };
void N::f1(N m)
{cout<<\cout<<\}
int N::B=0; int main() { N P(5),Q(9); N::f1(P); N::f1(Q); return 0; }
答:运行结果是 A=5 B=14 9=9 B=14