Files
BasicsOfComputerSoftwareEng…/OOP/15Exam/3.7.cpp
2023-05-30 19:37:18 +08:00

56 lines
782 B
C++

#include <iostream>
using namespace std;
class A {
public:
A(int xx = 10) : x(xx) {
++count;
}
virtual void show() const {
cout << count << " " << x << endl;
}
virtual ~A() {
cout << count-- << " " << x << endl;
}
protected:
static int count;
private:
int x;
};
class B : public A {
public:
B(int xx, int yy) : A(xx), y(yy) {
y += 8;
}
virtual void show() const {
cout << count << " " << y << endl;
}
virtual ~B() {
cout << count << " " << y << endl;
}
private:
int y;
};
int A::count = 0;
int main() {
A *ptr[] = {new B(66, 88), new A};
ptr[0]->show();
ptr[1]->show();
delete ptr[0];
delete ptr[1];
return 0;
}
/*
2 96
2 10
2 96
2 66
1 10
*/