From 14b223618e9136e3fc110a2dd5de1473f5699138 Mon Sep 17 00:00:00 2001 From: unlockable Date: Tue, 30 May 2023 19:28:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=AE=8C=E6=89=80=E6=9C=89=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP/15Exam/3.10.cpp | 37 ++++++++++++++++++++++++++++++ OOP/15Exam/3.7.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ OOP/15Exam/3.8.cpp | 26 +++++++++++++++++++++ OOP/15Exam/3.9.cpp | 42 ++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 OOP/15Exam/3.10.cpp create mode 100644 OOP/15Exam/3.7.cpp create mode 100644 OOP/15Exam/3.8.cpp create mode 100644 OOP/15Exam/3.9.cpp diff --git a/OOP/15Exam/3.10.cpp b/OOP/15Exam/3.10.cpp new file mode 100644 index 0000000..366de35 --- /dev/null +++ b/OOP/15Exam/3.10.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +class A { +public: + A(char *aa) { + b = strlen(aa); + a = new char[b + 1]; + strcpy(a, aa); + } + ~A() { + delete[] a; + } + char *Geta() { + return a; + } + int Getb() { + return b; + } + +private: + char *a; + int b; +}; + +int main() { + A x("zhao"), y("zhuang"); + cout << strlen(x.Geta()) << endl; + cout << strlen(y.Geta()) + x.Getb() << endl; + return 0; +} + +/* +4 +10 +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.7.cpp b/OOP/15Exam/3.7.cpp new file mode 100644 index 0000000..d03267c --- /dev/null +++ b/OOP/15Exam/3.7.cpp @@ -0,0 +1,56 @@ +#include +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 +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.8.cpp b/OOP/15Exam/3.8.cpp new file mode 100644 index 0000000..48b9a8d --- /dev/null +++ b/OOP/15Exam/3.8.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +class A { +public: + A(int x = 0) : r1(x){}; + void print() { + cout << 'E' << r1 << '-'; + } + void print(int x) { + cout << 'P' << r1 * r1 * r1 + x << '-'; + } + +private: + int r1; +}; + +int main() { + A a1(3), a2(9); + a1.print(1); + a2.print(); + return 0; +} + +/* +P28-E9- +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.9.cpp b/OOP/15Exam/3.9.cpp new file mode 100644 index 0000000..d10127c --- /dev/null +++ b/OOP/15Exam/3.9.cpp @@ -0,0 +1,42 @@ +#include +#include +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 +*/ \ No newline at end of file