13年题。
This commit is contained in:
43
OOP/13Exam/3.2.cpp
Normal file
43
OOP/13Exam/3.2.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() {
|
||||
cout << "A::A() called.\n";
|
||||
}
|
||||
virtual ~A() {
|
||||
cout << "A::~A() called.\n";
|
||||
}
|
||||
};
|
||||
|
||||
class B: public A {
|
||||
public:
|
||||
B(int i) {
|
||||
cout << "B::B() called.\n";
|
||||
buf = new char[i];
|
||||
}
|
||||
virtual ~B() {
|
||||
delete [] buf;
|
||||
cout << "B::~B() called.\n";
|
||||
}
|
||||
|
||||
private:
|
||||
char *buf;
|
||||
};
|
||||
|
||||
void fun(A *a) {
|
||||
delete a;
|
||||
}
|
||||
|
||||
int main() {
|
||||
A *a = new B(15);
|
||||
fun(a);
|
||||
}
|
||||
|
||||
/*
|
||||
A::A() called.
|
||||
B::B() called.
|
||||
B::~B() called.
|
||||
A::~A() called.
|
||||
*/
|
||||
Reference in New Issue
Block a user