写出下面程序的运行结果: #include <iostream> using namespace std; class A { int x,y; public: A() { cout << "in A's default constructor\n"; f(); } A(const A&) { cout << "in A's copy constructor\n"; f(); } ~A() { cout << "in A's destructor\n"; } virtual void f() { cout << "in A's f\n"; } void g() { cout << "in A's g\n"; } void h() { f(); g(); } }; class B: public A { int z; public: B() { cout << "in B's default constructor\n"; } B(const B&) { cout << "in B's copy constructor\n"; } ~B() { cout << "in B's destructor\n"; } void f() { cout << "in B's f\n"; } void g() { cout << "in B's g\n"; } }; void func1(A x) { x.f(); x.g(); x.h(); } void func2(A &x) { x.f(); x.g(); x.h(); } int main() { cout << "------Section 1------\n"; A a; A *p=new B; cout << "------Section 2------\n"; func1(a); cout << "------Section 3------\n"; func1(*p); cout << "------Section 4------\n"; func2(a); cout << "------Section 5------\n"; func2(*p); cout << "------Section 6------\n"; delete p; cout << "------Section 7------\n"; return 0; }
时间: 2024-01-03 09:05:32 浏览: 73
使用include <iostream>时一定要加using namespace std
程序运行结果:
------Section 1------
in A's default constructor
in A's f
in A's g
in A's f
in A's g
------Section 2------
in A's default constructor
in A's f
in A's g
in A's f
in A's g
------Section 3------
in A's copy constructor
in A's f
in A's g
in A's f
in A's g
in A's destructor
------Section 4------
in A's f
in A's g
in A's f
in A's g
------Section 5------
in B's f
in A's g
in A's f
in B's g
------Section 6------
in B's destructor
in A's destructor
------Section 7------
阅读全文