Files
BasicsOfComputerSoftwareEng…/OOP/15Exam/3.9.cpp
2023-05-30 19:28:40 +08:00

42 lines
784 B
C++

#include <cstring>
#include <iostream>
using namespace std;
class A {
public:
A(char *name = "HSW") {
strcpy(this->name, name);
}
const char *getName() const {
return name;
}
virtual char *getAddress() const {
return "BEIDA";
}
private:
char name[20];
};
class AB : public A {
public:
AB(char *name) : A(name){};
virtual char *getAddress() const {
return "TSINGHUA";
}
};
int main() {
A *gs[] = {new AB("ZHT"), new A}, **p = gs + 1;
cout << (*p)->getName() << "STAY IN" << (*p)->getAddress() << endl;
p--;
cout << (*p)->getName() << "STAY IN" << (*p)->getAddress() << endl;
for (int i = 0; i < 2; i++) {
delete gs[i];
}
return 0;
}
/*
HSWSTAY INBEIDA
ZHTSTAY INTSINGHUA
*/