OOP HomeWork

This commit is contained in:
e2hang
2025-08-11 00:01:30 +08:00
commit e8a5ca2363
119 changed files with 3187 additions and 0 deletions

57
oop_hw4/Q123.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include <iostream>
using std::string;
using std::cout;
using std::cin;
class thief{
private:
string name;
double money;
bool caught;
public:
thief(){
name = "";
money = -1;
caught = false;
}
thief(string n,double m,bool c){
name = n;
money = m;
caught = c;
}
thief& steal(walker x){
money = x.money_print();
x.zero();
return this;
}
};
class walker{
private:
string name;
double money;
bool stolen;
public:
walker(){
name = "";
money = -1;
stolen = false;
}
walker(string n,double m,bool c){
name = n;
money = m;
stolen = c;
}
double money_print(){
return money;
}
walker& zero(){
money = 0;
stolen = true;
return this;
}
};
class police{
};

29
oop_hw4/hw123/main.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <iostream>
#include "thief.h"
#include "walker.h"
#include "police.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main() {
thief t1("Dan", 100, false), t2("Trevor", 50.4, false), t3("Mike", 30.2, false);
walker w1("Man", 1000, false), w2("Wan", 5000, false);
t1.steal(w1);
t2.steal(w2);
t1.display();
t2.display();
w1.display();
w2.display();
police p1("Officer", 0, 0), p2("Reciffo", 1, 1), p3("fw", 0, 0);
p1.caught(t1);
p2.caught(t2);
cout << "After Caught" << endl;
p1.display();
p2.display();
t1.display();
t2.display();
return 0;
}

28
oop_hw4/hw123/police.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include <iostream>
#include "walker.h"
#include "police.h"
#include "thief.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
police::police(){
name = "";
money = -1;
respect = -1;
}
police::police(string n, double m, int r) {
name = n ;
money = m ;
respect = r ;
}
police& police::caught(thief& x) {
money += 100;
respect++;
x.becaught();
return *this;
}
void police::display() {
cout << name << " " << money << " " << "Respect :" << respect << endl;
}

22
oop_hw4/hw123/police.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include <iostream>
#include "thief.h"
#include "walker.h"
class thief;
using std::string;
using std::cout;
using std::cin;
class police {
private:
string name;
double money;
int respect;
public:
police();
police(string n, double m, int r);
police& caught(thief& x);
void display();
};

33
oop_hw4/hw123/thief.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
#include "thief.h"
#include "walker.h"
#include "police.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
thief::thief() {
name = "";
money = -1;
caught = false;
}
thief::thief(string n, double m, bool c = false) {
name = n;
money = m;
caught = c;
}
thief& thief::steal(walker& x) {
money += x.money_print();
x.zero();
caught = true;
return *this;
}
thief& thief::becaught() {
money = 0;
caught = false;
return *this;
}
void thief::display() {
cout << name << " " << money << " " << "If caught :" << caught << endl;
}

24
oop_hw4/hw123/thief.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <iostream>
#include "walker.h"
#include "police.h"
class walker;
using std::string;
using std::cout;
using std::cin;
class thief {
private:
string name;
double money;
bool caught;
public:
thief();
thief(string n, double m, bool c);
thief& steal(walker& x);
void display();
thief& becaught();
};

32
oop_hw4/hw123/walker.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include <iostream>
#include "thief.h"
#include "walker.h"
#include "police.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
walker::walker() {
name = "";
money = -1;
stolen = false;
}
walker::walker(string n, double m, bool c = false) {
name = n;
money = m;
stolen = c;
}
double walker::money_print() {
return money;
}
walker& walker::zero() {
money = 0;
stolen = true;
return *this;
}
void walker::display() {
cout << name << " " << money << " " << "If stolen :" << stolen << endl;
}

21
oop_hw4/hw123/walker.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <iostream>
#include "thief.h"
#include "police.h"
using std::string;
using std::cout;
using std::cin;
class walker {
private:
string name;
double money;
bool stolen;
public:
walker();
walker(string n, double m, bool c);
double money_print();
walker& zero();
void display();
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 408 KiB

33
oop_hw4/hw4/main.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
#include <cstring>
#include "man.h"
#include "woman.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main() {
man m1("Zhang", false , NULL), m2("Wang", false , NULL), m3("Li", true , NULL), m4("Zhao", true , NULL);
woman w1("Wan", false, NULL), w2("Wu", false, NULL), w3("Xu", true, NULL), w4("Zhou", true, NULL);
m1.marry(&w1);
m2.marry(&w2);
m3.marry(&w3);
m4.marry(&w4);
cout << "Set" << endl;
m1.show() == NULL ? cout << "Devorced" << endl : cout << m1.show()->display() << endl;
m2.show() == NULL ? cout << "Devorced" << endl : cout << m2.show()->display() << endl;
m3.show() == NULL ? cout << "Devorced" << endl : cout << m3.show()->display() << endl;
m4.show() == NULL ? cout << "Devorced" << endl : cout << m4.show()->display() << endl;
cout << "Arrange" << endl;
m2.devorce(&w2);
m4.devorce(&w4);
m1.show() == NULL ? cout << "Devorced" << endl : cout << m1.show()->display() << endl;
m2.show() == NULL ? cout << "Devorced" << endl : cout << m2.show()->display() << endl;
m3.show() == NULL ? cout << "Devorced" << endl : cout << m3.show()->display() << endl;
m4.show() == NULL ? cout << "Devorced" << endl : cout << m4.show()->display() << endl;
return 0;
}

33
oop_hw4/hw4/man.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <iostream>
#include "woman.h"
#include "man.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
man::man() {
name = "";
married = false;
w = NULL;
}
man::man(string n, bool m, woman* x) {
name = n;
married = m;
w = x;
}
void man::marry(woman* x) {
w = x;
married = true;
}
void man::devorce(woman* x) {
w = NULL;
married = false;
}
string man::display() {
return name;
}
woman* man::show() {
return w;
}

24
oop_hw4/hw4/man.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <iostream>
#include "woman.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
class woman;
class man {
private:
string name;
bool married;
woman* w;
public:
man();
man(string n, bool m, woman* x);
void marry(woman* x);
void devorce(woman* x);
string display();
woman* show();
};

34
oop_hw4/hw4/woman.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <iostream>
#include "man.h"
#include "woman.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
woman::woman() {
name = "";
married = false;
w = NULL;
}
woman::woman(string n, bool m, man* x) {
name = n;
married = m;
w = x;
}
void woman::marry(man* x) {
w = x;
married = true;
}
void woman::devorce(man* x) {
w = NULL;
married = false;
}
string woman::display() {
return name;
}
man* woman::show() {
return w;
}

24
oop_hw4/hw4/woman.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <iostream>
#include "man.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
class man;
class woman {
private:
string name;
bool married;
man* w;
public:
woman();
woman(string n, bool m, man* x);
void marry(man* x);
void devorce(man* x);
string display();
man* show();
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 380 KiB

76
oop_hw4/hw6/main.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include <iostream>
#include <cstddef>
class A {
private:
int data[10]; // ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>
// <20><>̬<EFBFBD><CCAC>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>ͳ<EFBFBD><CDB3>
static size_t createdCount;
static size_t destroyedCount;
static size_t totalAllocatedBytes;
static size_t currentUsedBytes;
public:
// <20><><EFBFBD><EFBFBD> new
void* operator new(size_t size) {
createdCount++;
totalAllocatedBytes += size;
currentUsedBytes += size;
std::cout << "[new] size = " << size << std::endl;
return ::operator new(size);
}
// <20><><EFBFBD><EFBFBD> delete
void operator delete(void* ptr, size_t size) {
destroyedCount++;
currentUsedBytes -= size;
std::cout << "[delete] size = " << size << std::endl;
::operator delete(ptr);
}
// <20><><EFBFBD><EFBFBD> new[]
void* operator new[](size_t size) {
// <20><><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><DABF>ܺ<EFBFBD><DCBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬼<EFBFBD><F3A3ACBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
createdCount++; // ע<><EFBFBD><E2A3BA><EFBFBD><EFBFBD>ֻͳ<D6BB><CDB3>һ<EFBFBD>Σ<EFBFBD><CEA3>ɸ<EFBFBD><C9B8><EFBFBD>ϸͳ<CFB8><CDB3>
totalAllocatedBytes += size;
currentUsedBytes += size;
std::cout << "[new[]] size = " << size << std::endl;
return ::operator new[](size);
}
// <20><><EFBFBD><EFBFBD> delete[]
void operator delete[](void* ptr, size_t size) {
destroyedCount++; // ͬ<><CDAC>
currentUsedBytes -= size;
std::cout << "[delete[]] size = " << size << std::endl;
::operator delete[](ptr);
}
// ͳ<><CDB3><EFBFBD><EFBFBD>Ϣ
static void showStats() {
std::cout << "Created: " << createdCount << std::endl;
std::cout << "Destroyed: " << destroyedCount << std::endl;
std::cout << "Total allocated: " << totalAllocatedBytes << " bytes" << std::endl;
std::cout << "Current in use: " << currentUsedBytes << " bytes" << std::endl;
}
};
// <20><>̬<EFBFBD><CCAC>Ա<EFBFBD><D4B1>ʼ<EFBFBD><CABC>
size_t A::createdCount = 0;
size_t A::destroyedCount = 0;
size_t A::totalAllocatedBytes = 0;
size_t A::currentUsedBytes = 0;
int main() {
A* a1 = new A;
A* a2 = new A;
delete a1;
A* arr = new A[3];
delete[] arr;
A::showStats();
return 0;
}

12
oop_hw4/hw7/aws.txt Normal file
View File

@@ -0,0 +1,12 @@
一、为什么重载 + 一般用自由函数形式?
原因:+ 具有交换律,左操作数不一定是你定义的类对象。自由函数可以让任意顺序都合法,因为左右参数都可以指定类型。
二、为什么重载 += 一般用成员函数形式?
原因a += b 本质是修改 a左操作数必须能访问并修改其内部状态。自由函数虽然也能写但不能直接访问私有成员需要提供 setter 或 friend不优雅。
三、为什么重载 = 必须是成员函数?
原因a = b 是赋值,修改的是左操作数的内部状态,必须有 this 指针。C++ 强制 operator= 只能是成员函数(标准规定)。
四、为什么重载 << 要用自由函数?
原因std::cout << obj 左操作数是 std::ostream你没法修改它的定义。如果不用自由函数就会变成 trip << cout不符合逻辑

23
oop_hw4/hw8/main.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include <iostream>
#include "page.h"
using std::cin;
using std::cout;
using std::endl;
int main() {
Paginate pager(13);
for (int i = 1;i <= 13;i++)
{
//i<><69>ǰҳ<C7B0><D2B3>13<31><33>ҳ<EFBFBD><D2B3>
pager.setPage(i).show();
}
cout << "START MOVING...." << endl;
pager.setPage(5).show();
pager.next().show();
pager.prev().show();
//ֱ<>ӷ<EFBFBD>
pager.nextN().show();
pager.next().show();
pager.prevN().show();
return 0;
}

87
oop_hw4/hw8/page.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include <iostream>
#include "page.h"
using std::cin;
using std::cout;
using std::endl;
void out(int n) {
switch (n)
{
case 1:
cout << "<EFBFBD><EFBFBD>ҳ 1+ 2 3 4 5 <20><> 13 <20><>ҳ" << endl;
break;
case 2:
cout << "<EFBFBD><EFBFBD>ҳ 1 2+ 3 4 5 <20><> 13 <20><>ҳ" << endl;
break;
case 3:
cout << "<EFBFBD><EFBFBD>ҳ 1 2 3+ 4 5 <20><> 13 <20><>ҳ" << endl;
break;
case 4:
cout << "<EFBFBD><EFBFBD>ҳ 1 2 3 4+ 5 <20><> 13 <20><>ҳ" << endl;
break;
case 5:
cout << "<EFBFBD><EFBFBD>ҳ 1 2 3 4 5+ <20><> 13 <20><>ҳ" << endl;
break;
case 6:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 6+ 7 8 9 10 <20><> 13 <20><>ҳ" << endl;
break;
case 7:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 6 7+ 8 9 10 <20><> 13 <20><>ҳ" << endl;
break;
case 8:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 6 7 8+ 9 10 <20><> 13 <20><>ҳ" << endl;
break;
case 9:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 6 7 8 9+ 10 <20><> 13 <20><>ҳ" << endl;
break;
case 10:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 6 7 8 9 10+ <20><> 13 <20><>ҳ" << endl;
break;
case 11:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 9 10 11+ 12 13 <20><>ҳ" << endl;
break;
case 12:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 9 10 11 12+ 13 <20><>ҳ" << endl;
break;
case 13:
cout << "<EFBFBD><EFBFBD>ҳ 1 <20><> 9 10 11 12 13+ <20><>ҳ" << endl;
break;
default:
cout << "Fail!" << endl;
break;
}
}
Paginate::Paginate() {
page_now = 0;
page_total = 0;
}
Paginate::Paginate(int n) {
page_total = n;
}
Paginate& Paginate::setPage(int n) {
page_now = n;
return *this;
}
Paginate& Paginate::next() {
page_now += 1;
return *this;
}
Paginate& Paginate::prev() {
page_now -= 1;
return *this;
}
Paginate& Paginate::nextN() {
page_now += 5;
return *this;
}
Paginate& Paginate::prevN() {
page_now -= 5;
return *this;
}
void Paginate::show() {
out(this->page_now);
}

20
oop_hw4/hw8/page.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Paginate {
private:
int page_total;
int page_now;
public:
Paginate();
Paginate(int n);
Paginate& setPage(int n);
void show();
Paginate& next();
Paginate& prev();
Paginate& nextN();
Paginate& prevN();
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 KiB

Binary file not shown.