补完所有的代码。

This commit is contained in:
unlockable
2023-05-30 19:28:40 +08:00
parent fb60255f19
commit 14b223618e
4 changed files with 161 additions and 0 deletions

37
OOP/15Exam/3.10.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <cstring>
#include <iostream>
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
*/

56
OOP/15Exam/3.7.cpp Normal file
View File

@@ -0,0 +1,56 @@
#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
*/

26
OOP/15Exam/3.8.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <iostream>
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-
*/

42
OOP/15Exam/3.9.cpp Normal file
View File

@@ -0,0 +1,42 @@
#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
*/