From fb60255f1920fe7b59d5cba5364e7e46b2b97173 Mon Sep 17 00:00:00 2001 From: unlockable Date: Tue, 30 May 2023 16:40:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=9F=E6=9C=AB=E5=A4=8D=E4=B9=A0=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP/15Exam/2.1.cpp | 33 +++++++++++++++ OOP/15Exam/2.2.cpp | 59 ++++++++++++++++++++++++++ OOP/15Exam/2.3.cpp | 87 ++++++++++++++++++++++++++++++++++++++ OOP/15Exam/3.1.cpp | 36 ++++++++++++++++ OOP/15Exam/3.2.cpp | 62 +++++++++++++++++++++++++++ OOP/15Exam/3.3.cpp | 21 ++++++++++ OOP/15Exam/3.4.cpp | 52 +++++++++++++++++++++++ OOP/15Exam/3.5.cpp | 39 +++++++++++++++++ OOP/15Exam/3.6.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 491 insertions(+) create mode 100644 OOP/15Exam/2.1.cpp create mode 100644 OOP/15Exam/2.2.cpp create mode 100644 OOP/15Exam/2.3.cpp create mode 100644 OOP/15Exam/3.1.cpp create mode 100644 OOP/15Exam/3.2.cpp create mode 100644 OOP/15Exam/3.3.cpp create mode 100644 OOP/15Exam/3.4.cpp create mode 100644 OOP/15Exam/3.5.cpp create mode 100644 OOP/15Exam/3.6.cpp diff --git a/OOP/15Exam/2.1.cpp b/OOP/15Exam/2.1.cpp new file mode 100644 index 0000000..cf9c8fe --- /dev/null +++ b/OOP/15Exam/2.1.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; +class TS { +public: + TS(int n = 0, int d = 0); + void Print(); + // ____________________________ // Blank 1 + + friend TS Div(TS &r1, TS &r2); +private: + int N, D; +}; + +TS Div(TS &r1, TS &r2) { + return TS(r1.N / r2.D + r1.D / r2.N, r1.D / r2.D); +} + +TS::TS(int n, int d) { + N = n; + D = d; +} + +//__________Print() { //Blank 2 +void TS::Print() { + cout << "N=" << N << " D=" << D << endl; +} + +int main() { + TS a(1, 2), b(3, 4), c; + c = Div(a, b); + c.Print(); + return 0; +} \ No newline at end of file diff --git a/OOP/15Exam/2.2.cpp b/OOP/15Exam/2.2.cpp new file mode 100644 index 0000000..bacd3b8 --- /dev/null +++ b/OOP/15Exam/2.2.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; +class CPerson { +public: + CPerson(char *Name, char *Sex); + void Show() const; + +protected: + char *m_Name; + char *m_Sex; +}; + +class CStudent : public CPerson { +public: + CStudent(int iNumber, char *Name, char *Sex); + void Show() const; + +protected: + int m_iNumber; +}; + +//________CPerson(char *Name, char *Sex) { //Blank 3 + +CPerson::CPerson(char *Name, char *Sex) { + m_Name = Name; + m_Sex = Sex; +} + +// void CPerson::Show() ______ { // Blank 4 + +void CPerson::Show() const { + cout << m_Name << "|" << m_Sex << endl; +} + +// CStudent::CStudent(int iNumber, char *Name, char *Sex): ___________ { // +// Blank 5 + +CStudent::CStudent(int iNumber, char *Name, char *Sex) : CPerson(Name, Sex) { + m_iNumber = iNumber; +} + +void CStudent::Show() const { + cout << m_Name << "|" << m_Sex << "|" << m_iNumber << endl; +} + +int main() { + CPerson oCPerson("德华刘", "男"); + + //CPerson ________ // Blank 6 + + CPerson *pCPerson; + CStudent oCStudent(20050101, "学友张", "男"); + pCPerson = &oCPerson; + pCPerson->Show(); + pCPerson = &oCStudent; + pCPerson->Show(); + cin.get(); + return 0; +} \ No newline at end of file diff --git a/OOP/15Exam/2.3.cpp b/OOP/15Exam/2.3.cpp new file mode 100644 index 0000000..53c2f11 --- /dev/null +++ b/OOP/15Exam/2.3.cpp @@ -0,0 +1,87 @@ +#include +using namespace std; +class Point { +public: + Point(int x, int y); + Point(Point &p); + ~Point(); + void Set(double x, double y); + void Print(); + +private: + double X, Y; +}; + +Point::Point(int x, int y) { + X = x; + Y = y; +} + +Point::Point(Point &p) { + X = p.X; + Y = p.Y; +} + +void Point::Set(double x, double y) { + X = x; + Y = y; +} + +void Point::Print() { + cout << '(' << X << "," << Y << ")" << endl; +} + +Point::~Point() { + cout << "Point的析构函数被调用!" << endl; +} + +class Line : public Point { +public: + Line(int x, int y, int s); + Line(Line &p); + ~Line(); + void Set(double x, double y, double s); + void Print(); + +private: + double S; +}; +// Line::Line(int x, int y, int s) ____________ { // Blank 7 + +Line::Line(int x, int y, int s) : Point(x, y) { + S = s; +} + +// Line::Line(Line &p) __________ { // Blanck 8 + +Line::Line(Line &p) : Point(p) { + S = p.S; +} + +void Line::Set(double x, double y, double s) { + // _________________; // Blank 9 + + Point::Set(x, y); + S = s; +} + +void Line::Print() { + cout << "直线经过的点:"; + //_______________________; //Blank 10 + + Point::Print(); + cout << "斜率为:S=" << S << endl; +} + +Line::~Line() { + cout << "Line析构函数被调用!" << endl; +} + +int main() { + Line C1(1, 1, 5); + C1.Print(); + Line C2(C1); + C2.Set(3, -3, 10); + C2.Print(); + return 0; +} \ No newline at end of file diff --git a/OOP/15Exam/3.1.cpp b/OOP/15Exam/3.1.cpp new file mode 100644 index 0000000..927e440 --- /dev/null +++ b/OOP/15Exam/3.1.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +int Num = 1; + +namespace Name1 { +int Num = 2; +int Add(int Num) { + Num = ::Num + Num; + return Num; +} +} // namespace Name1 + +namespace Name2 { +int Num = 3; +int Add(int Num) { + Num = ::Num + Num; + return Num; +} +} // namespace Name2 + +int main() { + cout << Name1::Add(4) + ::Num << endl; + cout << Name2::Add(5) + Num << endl; + namespace N2 = Name1; + cout << N2::Add(6) + N2::Num << endl; + using namespace Name2; + cout << Add(7) + Name2::Num << endl; + return 0; +} + +/* +6 +7 +9 +11 +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.2.cpp b/OOP/15Exam/3.2.cpp new file mode 100644 index 0000000..c70ed59 --- /dev/null +++ b/OOP/15Exam/3.2.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; +class A1 { +public: + A1() { + x = 2; + y = 2; + } + A1(int i) { + x = i; + y = 18; + } + A1(int i, int j) { + x = i; + y = j; + } + void display() { + cout << "x=" << x << " y=" << y; + } + +private: + int x; + int y; +}; + +class A2 : public A1 { +public: + A2() { + z = 20; + } + A2(int i) : A1(i) { + z = 1; + } + A2(int i, int j) : A1(i + 1, j) { + z = 2; + } + A2(int i, int j, int k) : A1(i, j + 3) { + z = k; + } + void display1() { + display(); + cout << "z=" << z << endl; + } + +private: + int z; +}; + +int main() { + A1 b1(2, 3); + A2 b2(5); + A2 b3(3, 2, 1); + b1.display(); + b2.display1(); + b3.display1(); + return 0; +} + +/* +x=2 y=3x=5 y=18z=1 +x=3 y=5z=1 +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.3.cpp b/OOP/15Exam/3.3.cpp new file mode 100644 index 0000000..336fbab --- /dev/null +++ b/OOP/15Exam/3.3.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; +int main() { // 说明:在VS2008编译系统上,ios_base::与ios::功能相同 + int dd[] = {1, 2, 3, 4, 5}; + ofstream f1("data1.dat", ios_base::binary); + f1.write((char *)dd, sizeof(dd)); + f1.close(); + ifstream f2("data1.dat", ios_base::binary); + f2.seekg(sizeof(int) * 1, ios::beg); + f2.read((char*) &dd[0], sizeof(int) * 2); + f2.close(); + for (int k = 0; k < 4; k++) { + cout << dd[k] << ','; + } + return 0; +} + +/* +2,3,3,4, +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.4.cpp b/OOP/15Exam/3.4.cpp new file mode 100644 index 0000000..f60aef1 --- /dev/null +++ b/OOP/15Exam/3.4.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; +class matrix { // 该类的定义 +public: + matrix(double a1 = 1, double a2 = 2, double a3 = 3, double a4 = 4) { + a[0][0] = a1; + a[0][1] = a2; + a[1][0] = a3; + a[1][1] = a4; + } + matrix operator+(matrix &); + friend ostream &operator<<(ostream &, matrix &); // 输出流运算符重载 + +private: + double a[2][2]; +}; + +matrix matrix::operator+(matrix &b) { // 加法的定义 + int i, j; + matrix c(0, 0, 0, 0); + for (i = 0; i < 2; i++) { + for (j = 0; j < 2; j++) { + c.a[i][j] = a[i][j] + b.a[i][j]; + } + } + return c; +} + +ostream &operator<<(ostream &out, matrix &b) { // “<<”的定义 + int i, j; + for (i = 0; i < 2; i++) { + for (j = 0; j < 2; j++) { + out << b.a[i][j] << " "; + } + out << endl; + } + return out; +} + +int main() { + matrix a(1.1, 1.2, 1.3, 1.4), b(2.1, 2.2, 2.3, 2.4), c; + c = a + b; + cout << "矩阵a + b等于" << endl; + cout << c; + return 0; +} + +/* +矩阵a + b等于 +3.2 3.4 +3.6 3.8 +*/ diff --git a/OOP/15Exam/3.5.cpp b/OOP/15Exam/3.5.cpp new file mode 100644 index 0000000..17e18bf --- /dev/null +++ b/OOP/15Exam/3.5.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +class Complex { +public: + Complex() { + real = 0; + imag = 0; + } + Complex(double r) { + real = r, imag = 0; + } + Complex(double r, double i) { + real = r, imag = i; + } + operator double() { + return real + 2; // 强制转换Complex对象为其实部real并返回 + } + +private: + double real; + double imag; +}; + +int main() { + Complex A(1, 2.3), B = A; + int D; + double E = 2.1; + D = 10 - A; + cout << "D=" << D << endl; + B = Complex(D); + B = E + 1 - B; + cout << "B=" << B; + return 0; +} + +/* +D=7 +B=-3.9 +*/ \ No newline at end of file diff --git a/OOP/15Exam/3.6.cpp b/OOP/15Exam/3.6.cpp new file mode 100644 index 0000000..9a2892b --- /dev/null +++ b/OOP/15Exam/3.6.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; +class complex { // 复数类声明 +public: // 外部接口 + complex(double r = 0, double i = 0) { + this->r = r; + this->i = i; + + // this->num = ++count; + // cout << "(Num: " << this->num << ")"; + + cout << "Constructor!"; + display(); + } + complex(complex &c) : r(c.r), i(c.i) { + // this->num = ++count; + // cout << "(Num: " << this->num << ")"; + + cout << "Copy Constructor!"; + display(); + } + ~complex() { + // cout << "(Num: " << this->num << ")"; + + cout << "Destructor!"; + display(); + } + complex operator+(complex c1); + void display(); // 输出复数 + +private: + // static int count; + // int num; + + double r; // 复数实部 + double i; // 复数虚部 +}; + +complex complex::operator+(complex c2) { + complex result(r + c2.r, i + c2.i); + return result; + + // 原文:return complex(r + c2.r, i + c2.i),不能编译=_= +} + +void complex::display() { + cout << "(" << r << "," << i << ")" << endl; +} + +// int complex::count = 0; + +int main() { // 主函数 + complex c1(3, 2), c2(2, 5), c3; + cout << "c1="; + c1.display(); + cout << "c2="; + c2.display(); + c3 = c1 + c2; + cout << "c3=c1+c2="; + c3.display(); + return 0; +} + +// 这个题我愿称之为史上最烂烂题。他的输出与编译器有没有开右值引用优化有很大的关系。 + +// 中间一些注释掉的代码是我补的,可以帮助理解一下到底是哪个对象在做这些操作。 + +/* +关闭右值引用优化: +Constructor!(3,2) +Constructor!(2,5) +Constructor!(0,0) +c1=(3,2) +c2=(2,5) +Copy Constructor!(2,5) +Constructor!(5,7) +Copy Constructor!(5,7) +Destructor!(5,7) +Destructor!(5,7) +Destructor!(2,5) +c3=c1+c2=(5,7) +Destructor!(5,7) +Destructor!(2,5) +Destructor!(3,2) +*/ + +/* +开启右值引用优化: +Constructor!(3,2) +Constructor!(2,5) +Constructor!(0,0) +c1=(3,2) +c2=(2,5) +Copy Constructor!(2,5) +Constructor!(5,7) +Destructor!(5,7) +Destructor!(2,5) +c3=c1+c2=(5,7) +Destructor!(5,7) +Destructor!(2,5) +Destructor!(3,2) +*/ \ No newline at end of file