OOP HomeWork
BIN
oop_hw1/01_面向对象程序设计上机实验一.docx
Normal file
BIN
oop_hw1/01_面向对象程序设计上机实验一_参考答案.docx
Normal file
5
oop_hw1/oop_hw1_1/Func1.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "func.h"
|
||||
|
||||
int f1(int num) {
|
||||
return g1(num * 2);
|
||||
}
|
8
oop_hw1/oop_hw1_1/Func2.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "func.h"
|
||||
|
||||
int g1(int num) {
|
||||
return num * num;
|
||||
}
|
||||
int g2(int num) {
|
||||
return f1(num * 3);
|
||||
}
|
7
oop_hw1/oop_hw1_1/MyMain.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <iostream>
|
||||
#include "func.h"
|
||||
using namespace std;
|
||||
int main(int argc, char * argv[]) {
|
||||
cout << g2(10) << endl;
|
||||
return 0;
|
||||
}
|
9
oop_hw1/oop_hw1_1/func.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#ifndef FUNC_H
|
||||
#define FUNC_H
|
||||
|
||||
int f1(int num);
|
||||
int g1(int num);
|
||||
int g2(int num);
|
||||
|
||||
#endif
|
14
oop_hw1/oop_hw1_2/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include "print_this.h"
|
||||
#include "print_that.h"
|
||||
extern int print_count;
|
||||
|
||||
int main() {
|
||||
#ifdef DO_THIS
|
||||
print_this("Hello");
|
||||
#else
|
||||
print_that("Hello");
|
||||
#endif
|
||||
std :: cout << "Print called " << print_count << " times." << std::endl;
|
||||
return 0;
|
||||
}
|
7
oop_hw1/oop_hw1_2/print.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <iostream>
|
||||
#include "print.h"
|
||||
int print_count = 0;
|
||||
void print(const std::string& s) {
|
||||
++print_count;
|
||||
std::cout << s << std::endl;
|
||||
}
|
6
oop_hw1/oop_hw1_2/print.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#ifndef PRINT_H
|
||||
#define PRINT_H
|
||||
void print(const std::string& s);
|
||||
#endif
|
5
oop_hw1/oop_hw1_2/print_that.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "print.h"
|
||||
#include "print_that.h"
|
||||
void print_that(const std::string& s) {
|
||||
print("That: " + s);
|
||||
}
|
7
oop_hw1/oop_hw1_2/print_that.h
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
#pragma once
|
||||
#include <string>
|
||||
#ifndef PRINT_THAT_H
|
||||
#define PRINT_THAT_H
|
||||
void print_that(const std::string& s);
|
||||
#endif
|
5
oop_hw1/oop_hw1_2/print_this.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "print.h"
|
||||
#include "print_this.h"
|
||||
void print_this(const std::string& s) {
|
||||
print("This: " + s);
|
||||
}
|
6
oop_hw1/oop_hw1_2/print_this.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#ifndef PRINT_THIS_H
|
||||
#define PRINT_THIS_H
|
||||
void print_this(const std::string& s);
|
||||
#endif
|
17
oop_hw1/oop_hw1_3/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
double average(int arr[], int size) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < size; ++i) sum += arr[i];
|
||||
return (double)sum / size;
|
||||
}
|
||||
using namespace std;
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
int * a = new int[n];
|
||||
for (int i = 0;i < n;i++) {
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << average(a, n);
|
||||
return 0;
|
||||
}
|
11
oop_hw1/oop_hw1_4/main.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <iostream>
|
||||
#include "t_area.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
double a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
cout << triangle_area(a,b,c) << endl;
|
||||
return 0;
|
||||
}
|
5
oop_hw1/oop_hw1_4/t_area.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include <cmath>
|
||||
double triangle_area(double a, double b, double c) {
|
||||
double s = (a + b + c) / 2;
|
||||
return sqrt(s * (s - a) * (s - b) * (s - c));
|
||||
}
|
8
oop_hw1/oop_hw1_4/t_area.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#ifndef tarea
|
||||
#define tarea
|
||||
|
||||
double triangle_area(double a, double b, double c);
|
||||
|
||||
|
||||
#endif // !tarea
|
7
oop_hw1/oop_hw1_5/check_num.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#ifndef testchar
|
||||
#define testchar
|
||||
|
||||
bool is_digit(char ch);
|
||||
|
||||
#endif // !testchar
|
13
oop_hw1/oop_hw1_5/main.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <iostream>
|
||||
#include "check_num.h"
|
||||
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char ch;
|
||||
for (int i = 0;i < 10;i++) {
|
||||
cin >> ch;
|
||||
cout << is_digit(ch)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
3
oop_hw1/oop_hw1_5/testchar.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
bool is_digit(char ch) {
|
||||
return ch >= '0' && ch <= '9';
|
||||
}
|
BIN
oop_hw1/oop_hw1_5/测试截图1_5.jpg
Normal file
After Width: | Height: | Size: 193 KiB |
16
oop_hw1/oop_hw1_6/main.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include "sort.h"
|
||||
|
||||
using namespace std;
|
||||
int main() {
|
||||
int d, e;
|
||||
cin >> d >> e;
|
||||
sort_two(d, e);
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
sort_three(a, b, c);
|
||||
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||
cout << d << e <<endl;
|
||||
cout << a << b << c;
|
||||
return 0;
|
||||
}
|
9
oop_hw1/oop_hw1_6/sort.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <algorithm>
|
||||
void sort_two(int& a, int& b) {
|
||||
if (a < b) std::swap(a, b);
|
||||
}
|
||||
void sort_three(int& a, int& b, int& c) {
|
||||
if (a < b) std::swap(a, b);
|
||||
if (a < c) std::swap(a, c);
|
||||
if (b < c) std::swap(b, c);
|
||||
}
|
7
oop_hw1/oop_hw1_6/sort.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#ifndef sort
|
||||
#define sort
|
||||
void sort_three(int& a, int& b, int& c);
|
||||
void sort_two(int& d, int& e);
|
||||
|
||||
#endif // !sort
|
BIN
oop_hw1/oop_hw1_6/测试截图1_6.jpg
Normal file
After Width: | Height: | Size: 220 KiB |
14
oop_hw1/oop_hw1_7/fib.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
long& smaller(long& a, long& b) { return (a < b) ? a : b; }
|
||||
long& bigger(long& a, long& b) { return (a >= b) ? a : b; }
|
||||
|
||||
void fibe(int count) {
|
||||
long a = 1, b = 1;
|
||||
for (int i = 0; i < count; i++) {
|
||||
long& min = smaller(a, b);
|
||||
long& max = bigger(a, b);
|
||||
min = a + b;
|
||||
std::cout << max << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
9
oop_hw1/oop_hw1_7/fib.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#ifndef fib
|
||||
#define fib
|
||||
|
||||
long& smaller(long& a, long& b);
|
||||
long& bigger(long& a, long& b);
|
||||
void fibe(int count);
|
||||
|
||||
#endif // !fib
|
10
oop_hw1/oop_hw1_7/main.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
#include "fib.h"
|
||||
|
||||
using namespace std;
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
fibe(n);
|
||||
return 0;
|
||||
}
|
43
oop_hw1/oop_hw1_9/main.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Pen {
|
||||
public:
|
||||
void write() {}
|
||||
};
|
||||
|
||||
class FountainPen : public Pen {
|
||||
public:
|
||||
void refill() {}
|
||||
};
|
||||
|
||||
class File {
|
||||
public:
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class Directory {
|
||||
public:
|
||||
std::vector<File> files;
|
||||
};
|
||||
|
||||
class Printer {
|
||||
public:
|
||||
void print(const std::string& doc) {}
|
||||
};
|
||||
|
||||
class Monitor {
|
||||
public:
|
||||
void display(const std::string& img) {}
|
||||
};
|
||||
|
||||
class Sun {
|
||||
public:
|
||||
void shine() {}
|
||||
};
|
||||
|
||||
class Moon {
|
||||
public:
|
||||
void reflect() {}
|
||||
};
|
BIN
oop_hw1/上机测试1 (1).pdf
Normal file
BIN
oop_hw2/02_面向对象程序设计上机实验二.docx
Normal file
BIN
oop_hw2/02_面向对象程序设计上机实验二_参考答案.docx
Normal file
61
oop_hw2/oop_hw2_1/Q1.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
class TRandom {
|
||||
private:
|
||||
unsigned long seed;
|
||||
public:
|
||||
TRandom(unsigned long s) : seed(s) {}
|
||||
unsigned char NextByte() {
|
||||
seed = seed * 1103515245 + 12345;
|
||||
return static_cast<unsigned char>((seed / 65536) % 256);
|
||||
}
|
||||
};
|
||||
|
||||
void Coder(unsigned char data[], int len, unsigned long key) {
|
||||
TRandom rand(key);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
data[i] ^= rand.NextByte();
|
||||
}
|
||||
}
|
||||
|
||||
void Coder(unsigned char data[], int len, TRandom& rand, unsigned long key) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
data[i] ^= rand.NextByte();
|
||||
}
|
||||
}
|
||||
|
||||
class Crypter {
|
||||
private:
|
||||
TRandom rand;
|
||||
public:
|
||||
Crypter(unsigned long seed) : rand(seed) {}
|
||||
void Process(unsigned char data[], int len) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
data[i] ^= rand.NextByte();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void Test() {
|
||||
unsigned char msg[] = "Hello World";
|
||||
int len = sizeof(msg);
|
||||
unsigned long key = 12345;
|
||||
std::cout << "Original: " << msg << std::endl;
|
||||
Coder(msg, len, key);
|
||||
std::cout << "Encrypted: "; for (int i = 0; i < len; ++i) std::cout << msg[i]; std::cout << std::endl;
|
||||
Coder(msg, len, key);
|
||||
std::cout << "Decrypted: " << msg << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD> rand <20><>Ϊ TRandom rand<6E><64><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ᶪʧԭ״̬<D7B4><CCAC><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// <20><><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD>Ϊ const TRandom& rand<6E><64><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>Ϊ NextByte <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> const
|
BIN
oop_hw2/oop_hw2_1/Q1.exe
Normal file
BIN
oop_hw2/oop_hw2_1/测试截图1.png
Normal file
After Width: | Height: | Size: 101 KiB |
27
oop_hw2/oop_hw2_2/Q2.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Book {
|
||||
private:
|
||||
int totalPages;
|
||||
int pagesRead;
|
||||
public:
|
||||
Book(int total) : totalPages(total), pagesRead(0) {}
|
||||
void Read(int pages) { pagesRead += pages; if (pagesRead > totalPages) pagesRead = totalPages; }
|
||||
int GetTotalPages() const { return totalPages; }
|
||||
int GetPagesRead() const { return pagesRead; }
|
||||
int GetPagesLeft() const { return totalPages - pagesRead; }
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Book b(100);
|
||||
b.Read(30);
|
||||
std::cout << "Read: " << b.GetPagesRead() << ", Left: " << b.GetPagesLeft() << std::endl;
|
||||
b.Read(80);
|
||||
std::cout << "Read: " << b.GetPagesRead() << ", Left: " << b.GetPagesLeft() << std::endl;
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_2/Q2.exe
Normal file
BIN
oop_hw2/oop_hw2_2/测试截图2.png
Normal file
After Width: | Height: | Size: 104 KiB |
43
oop_hw2/oop_hw2_3/Q3.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
enum Suit { Spade, Heart, Diamond, Club };
|
||||
enum Rank { Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace };
|
||||
|
||||
class Card {
|
||||
private:
|
||||
int id;
|
||||
Suit suit;
|
||||
Rank rank;
|
||||
int backId;
|
||||
int width, height;
|
||||
std::pair<int, int> topLeft;
|
||||
public:
|
||||
Card(int _id, Suit _suit, Rank _rank, int _backId, int _w, int _h, std::pair<int, int> _tl)
|
||||
: id(_id), suit(_suit), rank(_rank), backId(_backId), width(_w), height(_h), topLeft(_tl) {}
|
||||
|
||||
Card(const Card& other) : id(other.id), suit(other.suit), rank(other.rank), backId(other.backId),
|
||||
width(other.width), height(other.height), topLeft(other.topLeft) {}
|
||||
|
||||
int GetBackId() const { return backId; }
|
||||
void SetBackId(int id) { backId = id; }
|
||||
bool IsSameSuit(const Card& other) const { return suit == other.suit; }
|
||||
bool IsSameRank(const Card& other) const { return rank == other.rank; }
|
||||
bool IsSuit(Suit s) const { return suit == s; }
|
||||
bool IsRank(Rank r) const { return rank == r; }
|
||||
void SetPosition(int x, int y) { topLeft = {x, y}; }
|
||||
std::pair<int, int> GetBottomRight() const { return {topLeft.first + width, topLeft.second + height}; }
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Card c1(0, Spade, Ace, 1, 100, 150, {0, 0});
|
||||
Card c2(1, Spade, King, 2, 100, 150, {100, 0});
|
||||
std::cout << "Same Suit: " << c1.IsSameSuit(c2) << std::endl;
|
||||
std::cout << "Bottom Right: (" << c1.GetBottomRight().first << ", " << c1.GetBottomRight().second << ")\n";
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_3/Q3.exe
Normal file
BIN
oop_hw2/oop_hw2_3/测试截图3.png
Normal file
After Width: | Height: | Size: 122 KiB |
51
oop_hw2/oop_hw2_4/Q4.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
class Hero {
|
||||
private:
|
||||
int charm, fame, attack, defense, magic;
|
||||
int items[5] = {0};
|
||||
public:
|
||||
Hero() : charm(0), fame(0), attack(0), defense(0), magic(0) {}
|
||||
|
||||
void Equip(int pos, int item) {
|
||||
if (pos >= 0 && pos < 5) items[pos] = item;
|
||||
ApplyItem(item);
|
||||
}
|
||||
|
||||
void Remove(int pos) {
|
||||
if (pos >= 0 && pos < 5) items[pos] = 0;
|
||||
}
|
||||
|
||||
void ApplyItem(int item) {
|
||||
switch(item) {
|
||||
case 1: charm += 2; break;
|
||||
case 2: fame += 3; break;
|
||||
case 3: attack += 1; break;
|
||||
case 4: defense += 2; break;
|
||||
case 5: magic += 4; break;
|
||||
case 6: charm += 1; magic += 1; break;
|
||||
}
|
||||
}
|
||||
|
||||
void ShowStats() const {
|
||||
std::cout << "Charm: " << charm << ", Fame: " << fame
|
||||
<< ", Attack: " << attack << ", Defense: " << defense
|
||||
<< ", Magic: " << magic << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Hero h;
|
||||
h.Equip(0, 1);
|
||||
h.Equip(1, 3);
|
||||
h.Equip(2, 5);
|
||||
h.ShowStats();
|
||||
h.Remove(1);
|
||||
h.ShowStats();
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_4/Q4.exe
Normal file
BIN
oop_hw2/oop_hw2_4/测试截图4.png
Normal file
After Width: | Height: | Size: 97 KiB |
27
oop_hw2/oop_hw2_5/Q5.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
class Demo {
|
||||
private:
|
||||
int mNum;
|
||||
static Demo* instance;
|
||||
Demo() : mNum(0) {}
|
||||
public:
|
||||
static Demo* GetInstance() {
|
||||
if (!instance) instance = new Demo();
|
||||
return instance;
|
||||
}
|
||||
void AddValue(int value) { mNum += value; }
|
||||
void ShowValue() const { std::cout << "Value=" << mNum << std::endl; }
|
||||
};
|
||||
Demo* Demo::instance = NULL;
|
||||
|
||||
void Test() {
|
||||
Demo::GetInstance()->AddValue(10);
|
||||
Demo::GetInstance()->AddValue(5);
|
||||
Demo::GetInstance()->ShowValue();
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw2/oop_hw2_5/Q5.exe
Normal file
BIN
oop_hw2/oop_hw2_5/测试截图5.png
Normal file
After Width: | Height: | Size: 93 KiB |
46
oop_hw2/oop_hw2_6/Q6.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Monster {
|
||||
private:
|
||||
int speed, hitpoint, damage, defense;
|
||||
public:
|
||||
Monster(int s, int h, int d, int def) : speed(s), hitpoint(h), damage(d), defense(def) {}
|
||||
|
||||
bool operator>(const Monster& other) const {
|
||||
if (speed != other.speed) return speed > other.speed;
|
||||
if (hitpoint != other.hitpoint) return hitpoint > other.hitpoint;
|
||||
if (damage != other.damage) return damage > other.damage;
|
||||
return defense > other.defense;
|
||||
}
|
||||
|
||||
void Attack(Monster& other) const {
|
||||
int harm = std::max(2 * damage - other.defense, 1);
|
||||
other.hitpoint -= harm;
|
||||
std::cout << "Monster attacks for " << harm << " damage. Opponent HP: " << other.hitpoint << std::endl;
|
||||
}
|
||||
|
||||
void Fight(Monster& other) {
|
||||
Monster* first = this;
|
||||
Monster* second = &other;
|
||||
if (!(*this > other)) std::swap(first, second);
|
||||
|
||||
while (first->hitpoint > 0 && second->hitpoint > 0) {
|
||||
first->Attack(*second);
|
||||
if (second->hitpoint <= 0) break;
|
||||
second->Attack(*first);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void Test() {
|
||||
Monster m1(10, 30, 5, 2);
|
||||
Monster m2(8, 35, 4, 3);
|
||||
m1.Fight(m2);
|
||||
}
|
||||
|
||||
int main(){
|
||||
Test();
|
||||
return 0;
|
||||
}
|
||||
|
BIN
oop_hw2/oop_hw2_6/Q6.exe
Normal file
BIN
oop_hw2/oop_hw2_6/测试截图6.png
Normal file
After Width: | Height: | Size: 146 KiB |
BIN
oop_hw2/上机测试2.pdf
Normal file
74
oop_hw3/hw1/main.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
class MyString {
|
||||
private:
|
||||
char* mpData;
|
||||
public:
|
||||
MyString(const char* pData = nullptr) {
|
||||
if (pData == nullptr) {
|
||||
mpData = new char[1];
|
||||
mpData[0] = '\0';
|
||||
}
|
||||
else {
|
||||
int len = strlen(pData);
|
||||
mpData = new char[len + 1];
|
||||
strcpy(mpData, pData);
|
||||
}
|
||||
|
||||
}
|
||||
MyString(const MyString& x) {
|
||||
int len = strlen(x.mpData);
|
||||
mpData = new char[len + 1];
|
||||
strcpy(mpData, x.mpData);
|
||||
}
|
||||
~MyString() {
|
||||
delete[] mpData;
|
||||
}
|
||||
MyString& operator=(const MyString& x) {
|
||||
if (this == &x) return *this;
|
||||
delete[] mpData;
|
||||
int len = strlen(x.mpData);
|
||||
mpData = new char[len + 1];
|
||||
strcpy(mpData, x.mpData);
|
||||
return *this;
|
||||
}
|
||||
MyString& operator+=(const MyString& x) {
|
||||
int len = strlen(mpData) + strlen(x.mpData);
|
||||
char* tmp = new char[len + 1];
|
||||
strcpy(tmp, mpData);
|
||||
strcat(tmp, x.mpData);
|
||||
delete[] mpData;
|
||||
mpData = tmp;
|
||||
return *this;
|
||||
}
|
||||
friend const MyString operator+(const MyString& x, const MyString& y) {
|
||||
int len = strlen(x.mpData) + strlen(y.mpData);
|
||||
char* tmp = new char[len + 1];
|
||||
strcpy(tmp, x.mpData);
|
||||
strcat(tmp, y.mpData);
|
||||
MyString r(tmp);
|
||||
delete[] tmp;
|
||||
return r;
|
||||
}
|
||||
friend ostream& operator<<(ostream& os, const MyString& x) {
|
||||
os << x.mpData;
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
char p[] = "Hello, world!";
|
||||
char q[] = "What are you doing?";
|
||||
char r[] = "How are you!";
|
||||
MyString a(p), b, c;
|
||||
b = MyString(q);
|
||||
c = b;
|
||||
MyString d;
|
||||
d = a + b;
|
||||
cout << d << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
BIN
oop_hw3/hw1/测试截图1.png
Normal file
After Width: | Height: | Size: 329 KiB |
91
oop_hw3/hw2/main.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#define M_PI 3.14159265358979323846
|
||||
using namespace std;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
class Fraction {
|
||||
private:
|
||||
int* p;
|
||||
public:
|
||||
Fraction():p(nullptr){}
|
||||
//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>СΪ30<33><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Fraction(int* x) {
|
||||
if (p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30 ;i++) {
|
||||
p[i] = x[i];
|
||||
}
|
||||
}
|
||||
Fraction(const Fraction& x) {
|
||||
if(p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30;i++) {
|
||||
p[i] = x.p[i];
|
||||
}
|
||||
}
|
||||
Fraction& operator=(const Fraction& x) {
|
||||
if(p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30;i++) {
|
||||
p[i] = x.p[i];
|
||||
}
|
||||
}
|
||||
friend ostream& operator<<(ostream& os, const Fraction& x) {
|
||||
for (int i = 0;i < 30;i++) {
|
||||
os << x.p[i] << " ";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
~Fraction() {
|
||||
delete[] p;
|
||||
}
|
||||
void DuiYingFenshu(int x) {
|
||||
long long fenzi = 1, fenmu = 0;
|
||||
for (int i = x - 1; i >= 0; --i) {
|
||||
long long temp = fenzi;
|
||||
fenzi = p[i] * fenzi + fenmu;
|
||||
fenmu = temp;
|
||||
}
|
||||
double val = (double)fenzi / fenmu;
|
||||
double cha = M_PI - val;
|
||||
cout << setprecision(16);
|
||||
cout << "<EFBFBD><EFBFBD>" << x << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>" << fenzi << "/" << fenmu << endl;
|
||||
cout << "<EFBFBD><EFBFBD>" << x << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>" << val << endl;
|
||||
cout << "<EFBFBD><EFBFBD> PI <20><><EFBFBD>" << cha << endl;
|
||||
}
|
||||
};
|
||||
//<2F><>һ<EFBFBD><D2BB>С<EFBFBD><D0A1>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
||||
int* out(double x) {
|
||||
double t = x;
|
||||
double temp;
|
||||
int* tmp;
|
||||
tmp = new int[30];
|
||||
tmp[0] = floor(t);
|
||||
t -= tmp[0];
|
||||
for (int i = 1;i < 30;i++) {
|
||||
temp = 1 / (t);
|
||||
tmp[i] = floor(temp);
|
||||
temp -= tmp[i];
|
||||
t = temp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//<2F><><EFBFBD><EFBFBD>out<75><74><EFBFBD><EFBFBD>
|
||||
int* a;
|
||||
a = out(M_PI);
|
||||
for (int i = 0;i < 30;i++) {
|
||||
cout << a[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Fraction test(a);
|
||||
for (int i = 1;i < 30;i++) {
|
||||
test.DuiYingFenshu(i);
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
oop_hw3/hw2/测试截图2.png
Normal file
After Width: | Height: | Size: 378 KiB |
33
oop_hw3/hw3/main.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
template <class T>
|
||||
void f(int m, int n) {
|
||||
T** p;
|
||||
p = new T* [m];
|
||||
for (int i = 0;i < m;i++) {
|
||||
p[i] = new T[n];
|
||||
}
|
||||
int cnt = 1;
|
||||
for (int i = 0;i < m;i++) {
|
||||
for (int j = 0;j < n;j++) {
|
||||
p[i][j] = cnt;
|
||||
cout << cnt << " ";
|
||||
cnt++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
if (p) {
|
||||
for (int i = 0;i < m;i++) {
|
||||
delete[] p[i];
|
||||
}
|
||||
delete[] p;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
f<int>(a, b);
|
||||
return 0;
|
||||
}
|
BIN
oop_hw3/hw3/测试截图3.png
Normal file
After Width: | Height: | Size: 334 KiB |
35
oop_hw3/hw4/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() {
|
||||
data = -1;
|
||||
}
|
||||
A(int n) :data(n) {
|
||||
|
||||
}
|
||||
int& Data(){
|
||||
return data;
|
||||
}
|
||||
private:
|
||||
int data;
|
||||
};
|
||||
|
||||
auto g = [](int n) {
|
||||
A* p = new A[n];
|
||||
for (int i = 0;i < n;i++) {
|
||||
p[i].Data() = i + 1;
|
||||
}
|
||||
for (int i = n-1;i >= 0;i--) {
|
||||
cout << p[i].Data() << " ";
|
||||
}
|
||||
cout << endl;
|
||||
};
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
cin >> a;
|
||||
g(a);
|
||||
return 0;
|
||||
}
|
BIN
oop_hw3/hw4/测试截图4.png
Normal file
After Width: | Height: | Size: 287 KiB |
39
oop_hw3/hw5/main.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "vector.h"
|
||||
|
||||
using namespace std;
|
||||
int main() {
|
||||
Vector<int> test;
|
||||
Vector<string> stest;
|
||||
int m, n;
|
||||
cin >> m >> n;
|
||||
test = Vector<int>(m, n);
|
||||
stest = Vector<string>(m, n);
|
||||
for (int i = 0;i < m;i++) {
|
||||
for (int j = 0;j < n;j++) {
|
||||
cin >> test[i][j];
|
||||
}
|
||||
}
|
||||
for (int i = 0;i < m;i++) {
|
||||
for (int j = 0;j < n;j++) {
|
||||
cin >> stest[i][j];
|
||||
}
|
||||
}
|
||||
test[0][0] = -1;
|
||||
stest[0][0] = "Hello, world!";
|
||||
for (int i = 0;i < m;i++) {
|
||||
for (int j = 0;j < n;j++) {
|
||||
cout << setw(4) << test[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
for (int i = 0;i < m;i++) {
|
||||
for (int j = 0;j < n;j++) {
|
||||
cout << setw(20) << right << stest[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
81
oop_hw3/hw5/vector.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
#include <iostream>
|
||||
|
||||
template<class T>
|
||||
class Vector {
|
||||
private:
|
||||
T** p;
|
||||
int hang;
|
||||
int lie;
|
||||
public:
|
||||
Vector();
|
||||
Vector(int m, int n);
|
||||
Vector& operator=(const Vector& x);
|
||||
T* operator[](int x);
|
||||
void AlterElement(int m, int n, T x);
|
||||
~Vector();
|
||||
};
|
||||
|
||||
template<class T>
|
||||
Vector<T>::Vector() {
|
||||
p = nullptr;
|
||||
hang = 0;
|
||||
lie = 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector<T>::Vector(int m, int n) {
|
||||
hang = m;
|
||||
lie = n;
|
||||
p = new T * [m];
|
||||
for (int i = 0;i < m;i++) {
|
||||
p[i] = new T[n];
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector<T>::~Vector() {
|
||||
if (p) {
|
||||
for (int i = 0; i < hang; i++) {
|
||||
delete[] p[i];
|
||||
}
|
||||
delete[] p;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector<T>& Vector<T>::operator=(const Vector& x) {
|
||||
if (p) {
|
||||
for (int i = 0; i < hang; i++) {
|
||||
delete[] p[i];
|
||||
}
|
||||
delete[] p;
|
||||
}
|
||||
hang = x.hang;
|
||||
lie = x.lie;
|
||||
p = new T * [hang];
|
||||
for (int i = 0; i < hang; i++) {
|
||||
p[i] = new T[lie];
|
||||
for (int j = 0; j < lie; j++) {
|
||||
p[i][j] = x.p[i][j];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T* Vector<T>::operator[](int x)
|
||||
{
|
||||
return p[x];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void Vector<T>::AlterElement(int m, int n, T x)
|
||||
{
|
||||
p[m][n] = x;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // !VECTOR_H
|
BIN
oop_hw4/03_面向对象程序设计上机实验三.docx
Normal file
57
oop_hw4/Q123.cpp
Normal 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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();
|
||||
};
|
BIN
oop_hw4/hw123/测试截图123.png
Normal file
After Width: | Height: | Size: 408 KiB |
33
oop_hw4/hw4/main.cpp
Normal 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
@@ -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
@@ -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
@@ -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
@@ -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();
|
||||
};
|
BIN
oop_hw4/hw4/测试截图4.png
Normal file
After Width: | Height: | Size: 380 KiB |
76
oop_hw4/hw6/main.cpp
Normal 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
@@ -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
@@ -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>5ҳ
|
||||
pager.nextN().show();
|
||||
pager.next().show();
|
||||
pager.prevN().show();
|
||||
return 0;
|
||||
}
|
87
oop_hw4/hw8/page.cpp
Normal 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
@@ -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();
|
||||
};
|
BIN
oop_hw4/hw8/测试截图8.png
Normal file
After Width: | Height: | Size: 393 KiB |
BIN
oop_hw4/上机测试4 (1).pdf
Normal file
BIN
oop_hw5/04_面向对象程序设计上机实验四_参考答案.docx
Normal file
152
oop_hw5/hw1/ans.md
Normal file
@@ -0,0 +1,152 @@
|
||||
## **1)第一段代码分析:递归构造与输出**
|
||||
|
||||
```cpp
|
||||
class A {
|
||||
public:
|
||||
A(int n):val(n){}
|
||||
protected:
|
||||
int val;
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
B(int n) :A(n)
|
||||
{
|
||||
pB = (n > 0 ? new B(n-1) : 0);
|
||||
}
|
||||
|
||||
~B() { delete pB; }
|
||||
|
||||
void Display()
|
||||
{
|
||||
cout << val << endl;
|
||||
if (pB != 0)
|
||||
pB->Display();
|
||||
}
|
||||
|
||||
private:
|
||||
B* pB;
|
||||
};
|
||||
```
|
||||
|
||||
### 📌 构造逻辑:
|
||||
|
||||
* `B(4)` 被创建时,会递归构造 `B(3) → B(2) → B(1) → B(0) → B(-1)` 停止(因 `n == 0` 时 `pB = 0`)
|
||||
* 每一层的 `val` 都是当前 `n`
|
||||
|
||||
### 📌 输出逻辑:
|
||||
|
||||
`b.Display()` 调用后会:
|
||||
|
||||
```cpp
|
||||
cout << 4
|
||||
-> Display() on B(3)
|
||||
cout << 3
|
||||
-> Display() on B(2)
|
||||
cout << 2
|
||||
-> Display() on B(1)
|
||||
cout << 1
|
||||
-> Display() on B(0)
|
||||
cout << 0
|
||||
```
|
||||
|
||||
所以最终输出是:
|
||||
|
||||
```
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
||||
0
|
||||
```
|
||||
|
||||
### ✅ **运行结果1**:
|
||||
|
||||
```
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
||||
0
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## **2)第二段代码分析:构造顺序与拷贝构造**
|
||||
|
||||
```cpp
|
||||
class A {
|
||||
public:
|
||||
A(int n):num(n) { Out(); }
|
||||
A(const A& rhs):num(rhs.num) { Out(); }
|
||||
void Out() { cout << num << endl; }
|
||||
private:
|
||||
int num;
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
B(A& a) : obj(a), A(1) { }
|
||||
void Out() { obj.Out(); }
|
||||
|
||||
private:
|
||||
A obj;
|
||||
};
|
||||
```
|
||||
|
||||
### 📌 构造顺序注意:
|
||||
|
||||
在 `B(A& a) : obj(a), A(1)` 中,尽管初始化顺序写的是先 `obj`,再 `A`,**但实际构造顺序是:**
|
||||
|
||||
1. 先构造基类 A(即 `A(1)`)
|
||||
2. 再构造成员对象 `obj(a)`(调用拷贝构造函数)
|
||||
|
||||
### 📌 main 函数:
|
||||
|
||||
```cpp
|
||||
A a(8); // 输出:8
|
||||
B b1(a); // 顺序:
|
||||
// A(1) → 输出:1
|
||||
// obj(a) 拷贝构造 → 输出:8
|
||||
B b2(b1); // 和上面类似,只是复制的是 b1 对象的成员 obj
|
||||
// A(1) → 输出:1
|
||||
// obj(b1.obj) 拷贝构造 → 输出:8
|
||||
|
||||
b2.Out(); // 调用 obj.Out() → 输出:8
|
||||
```
|
||||
|
||||
### ✅ **运行结果2**:
|
||||
|
||||
```
|
||||
8
|
||||
1
|
||||
8
|
||||
1
|
||||
8
|
||||
8
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 两段程序的输出分别是:
|
||||
|
||||
### **程序1 输出:**
|
||||
|
||||
```
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
||||
0
|
||||
```
|
||||
|
||||
### **程序2 输出:**
|
||||
|
||||
```
|
||||
8
|
||||
1
|
||||
8
|
||||
1
|
||||
8
|
||||
8
|
||||
```
|
57
oop_hw5/hw2/animal.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "animal.h"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
Animal::Animal() {
|
||||
name = "#UNDEFINED";
|
||||
weight = -1;
|
||||
}
|
||||
Animal::Animal(std::string n, int w) {
|
||||
name = n;
|
||||
weight = w;
|
||||
}
|
||||
std::string Animal::show_name()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
int Animal::show_weight()
|
||||
{
|
||||
return weight;
|
||||
}
|
||||
void Animal::who() {
|
||||
std::cout << "This is an Animal" << std::endl;
|
||||
std::cout << "Name : " << name << std::endl
|
||||
<< "Weight : " << weight << std::endl;
|
||||
}
|
||||
Lion::Lion() {
|
||||
this->change_name("#UNDEFINED_LION_NAME");
|
||||
this->change_weight(-1);
|
||||
}
|
||||
Lion::Lion(std::string n,int w) {
|
||||
this->change_name(n);
|
||||
this->change_weight(w);
|
||||
}
|
||||
void Lion::who() {
|
||||
std::cout << "This is a Lion" << std::endl;
|
||||
std::cout << "Name : " << this->show_name() << std::endl
|
||||
<< "Weight : " << this->show_weight() << std::endl;
|
||||
}
|
||||
Aardvark::Aardvark() {
|
||||
this->change_name("#UNDEFINED_LION_NAME");
|
||||
this->change_weight(-1);
|
||||
}
|
||||
Aardvark::Aardvark(std::string n, int w) {
|
||||
this->change_name(n);
|
||||
this->change_weight(w);
|
||||
}
|
||||
void Aardvark::who() {
|
||||
std::cout << "This is a Aardvark" << std::endl;
|
||||
std::cout << "Name : " << this->show_name() << std::endl
|
||||
<< "Weight : " << this->show_weight() << std::endl;
|
||||
}
|
||||
void Animal::change_name(std::string cn) {
|
||||
name = cn;
|
||||
}
|
||||
void Animal::change_weight(int cw) {
|
||||
weight = cw;
|
||||
}
|
34
oop_hw5/hw2/animal.h
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
class Animal {
|
||||
private:
|
||||
std::string name;
|
||||
int weight;
|
||||
public:
|
||||
Animal();
|
||||
Animal(std::string n, int w);
|
||||
std::string show_name();
|
||||
int show_weight();
|
||||
void change_name(std::string cn);
|
||||
void change_weight(int cw);
|
||||
virtual void who();
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
class Lion : public Animal {
|
||||
public:
|
||||
Lion();
|
||||
Lion(std::string n, int w);
|
||||
void who();
|
||||
};
|
||||
|
||||
class Aardvark : public Animal {
|
||||
public:
|
||||
Aardvark();
|
||||
Aardvark(std::string n, int w);
|
||||
void who();
|
||||
};
|
17
oop_hw5/hw2/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include "animal.h"
|
||||
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
using std::endl;
|
||||
|
||||
int main() {
|
||||
Animal a("Animal!", 100);
|
||||
Lion b("Lion W", 200);
|
||||
Aardvark c("Aardvark", 300);
|
||||
a.who();
|
||||
b.who();
|
||||
c.who();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw5/hw2/测试截图2.png
Normal file
After Width: | Height: | Size: 329 KiB |
22
oop_hw5/hw3/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include "person.h"
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::string;
|
||||
|
||||
int main() {
|
||||
Employee a[5];
|
||||
Executive b[5];
|
||||
a[0] = Employee(20, "Zhang", 1, 1001);
|
||||
a[1] = Employee(21, "Wang", 1, 1002);
|
||||
a[2] = Employee(19, "Zhao", 0, 1003);
|
||||
a[3] = Employee(22, "Li", 1, 1004);
|
||||
a[4] = Employee(28, "Zh", 0, 1145);
|
||||
a[0].display();
|
||||
a[1].display();
|
||||
a[2].display();
|
||||
a[3].display();
|
||||
a[4].display();
|
||||
return 0;
|
||||
}
|
72
oop_hw5/hw3/person.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "person.h"
|
||||
|
||||
Person::Person(int a, string n, bool g)
|
||||
{
|
||||
age = a;
|
||||
name = n;
|
||||
gender = g;
|
||||
}
|
||||
|
||||
void Person::change_age(int ca)
|
||||
{
|
||||
age = ca;
|
||||
}
|
||||
|
||||
void Person::change_name(string cn)
|
||||
{
|
||||
name = cn;
|
||||
}
|
||||
|
||||
void Person::change_gender(bool x)
|
||||
{
|
||||
gender = x;
|
||||
}
|
||||
|
||||
int Person::show_age()
|
||||
{
|
||||
return age;
|
||||
}
|
||||
|
||||
string Person::show_name()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
bool Person::show_gender()
|
||||
{
|
||||
return gender;
|
||||
}
|
||||
|
||||
void Person::display()
|
||||
{
|
||||
cout << "This is a Person" << endl
|
||||
<< "Name : " << name << endl
|
||||
<< "Age : " << age << endl
|
||||
<< "Gender : ";
|
||||
gender == false ? cout << "Woman" << endl : cout << "Man" << endl;
|
||||
}
|
||||
|
||||
void Employee::add_number(int n)
|
||||
{
|
||||
number = n;
|
||||
}
|
||||
|
||||
void Employee::display()
|
||||
{
|
||||
cout << "This is an Employee" << endl
|
||||
<< "Name : " << this->show_name() << endl
|
||||
<< "Age : " << this->show_age() << endl
|
||||
<< "Gender : ";
|
||||
this->show_gender() == false ? cout << "Woman" << endl : cout << "Man" << endl;
|
||||
cout << "Number ID : " << number << endl;
|
||||
}
|
||||
|
||||
void Executive::display()
|
||||
{
|
||||
cout << "This is an Executive" << endl
|
||||
<< "Name : " << this->show_name() << endl
|
||||
<< "Age : " << this->show_age() << endl
|
||||
<< "Gender : ";
|
||||
this->show_gender() == false ? cout << "Woman" << endl : cout << "Man" << endl;
|
||||
//cout << "Number ID : " << number << endl;
|
||||
}
|
41
oop_hw5/hw3/person.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
using std::endl;
|
||||
using std::string;
|
||||
|
||||
class Person {
|
||||
private:
|
||||
int age;
|
||||
string name;
|
||||
//enum string gender = { "Male","Female" }; <20>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ôд<C3B4><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
bool gender;//1 = Male , 0 = Female
|
||||
public:
|
||||
Person() :age(-1), name("#UNDEFINED"), gender(true) {};
|
||||
Person(int a, string n, bool g);
|
||||
void change_age(int ca);
|
||||
void change_name(string cn);
|
||||
void change_gender(bool x);
|
||||
int show_age();
|
||||
string show_name();
|
||||
bool show_gender();
|
||||
virtual void display();
|
||||
};
|
||||
|
||||
class Employee : public Person {
|
||||
private:
|
||||
int number;
|
||||
public:
|
||||
Employee() :Person(-1, "#UNDEFINED", true), number(-1) {};
|
||||
Employee(int a, string n, bool g, int num) : Person(a, n, g), number(num) {};
|
||||
void add_number(int n);
|
||||
void display();
|
||||
};
|
||||
class Executive : public Employee {
|
||||
public:
|
||||
void display();
|
||||
};
|
||||
|
BIN
oop_hw5/hw3/测试截图3.png
Normal file
After Width: | Height: | Size: 391 KiB |
51
oop_hw5/hw4/code/ab.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A(int num) :data1(num) {}
|
||||
~A() {
|
||||
cout << " Destory A" << endl;
|
||||
}
|
||||
void f() const {
|
||||
cout << " Excute A::f() ";
|
||||
cout << " Data1=" << data1 << endl;
|
||||
}
|
||||
void g()
|
||||
{
|
||||
cout << " Excute A::g() " << endl;
|
||||
}
|
||||
protected:
|
||||
int data1;
|
||||
};
|
||||
|
||||
class B : public A
|
||||
{
|
||||
public:
|
||||
B(int num1, int num2) :A(num1), data2(num2) {}
|
||||
~B() {
|
||||
cout << " Destory B" << endl;
|
||||
}
|
||||
void f() const {
|
||||
cout << " Excute B::f() ";
|
||||
cout << " Data1=" << data1;
|
||||
cout << " Data2=" << data2 << endl;
|
||||
}
|
||||
void f(int n) const {
|
||||
cout << " Excute B::f(int) ";
|
||||
cout << " n=" << n;
|
||||
cout << " Data1=" << data1;
|
||||
cout << " Data2=" << data2 << endl;
|
||||
}
|
||||
void h() {
|
||||
cout << " Excute B::h() " << endl;
|
||||
}
|
||||
private:
|
||||
int data2;
|
||||
};
|
72
oop_hw5/hw4/code/main.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A(int num) :data1(num) {}
|
||||
virtual ~A() {
|
||||
cout << " Destory A" << endl;
|
||||
}
|
||||
void f() const {
|
||||
cout << " Excute A::f() ";
|
||||
cout << " Data1=" << data1 << endl;
|
||||
}
|
||||
void g()
|
||||
{
|
||||
cout << " Excute A::g() " << endl;
|
||||
}
|
||||
A& operator=(A& x) {
|
||||
data1 = x.data1;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
int data1;
|
||||
};
|
||||
|
||||
class B : public A
|
||||
{
|
||||
public:
|
||||
B(int num1, int num2) :A(num1), data2(num2) {}
|
||||
virtual ~B() {
|
||||
cout << " Destory B" << endl;
|
||||
}
|
||||
void f() const {
|
||||
cout << " Excute B::f() ";
|
||||
cout << " Data1=" << data1;
|
||||
cout << " Data2=" << data2 << endl;
|
||||
}
|
||||
void f(int n) const {
|
||||
cout << " Excute B::f(int) ";
|
||||
cout << " n=" << n;
|
||||
cout << " Data1=" << data1;
|
||||
cout << " Data2=" << data2 << endl;
|
||||
}
|
||||
void h() {
|
||||
cout << " Excute B::h() " << endl;
|
||||
}
|
||||
B& operator=(B& x) {
|
||||
data2 = x.data2;
|
||||
//data1 = x.data1;
|
||||
this->A::operator=(x);
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
int data2;
|
||||
};
|
||||
|
||||
int main() {
|
||||
B b(1, 2);
|
||||
A* p = new B(1, 2);
|
||||
b.f();
|
||||
b.g();
|
||||
b.f(3);
|
||||
b.h();
|
||||
delete p;
|
||||
return 0;
|
||||
}
|
BIN
oop_hw5/hw4/测试截图4.png
Normal file
After Width: | Height: | Size: 314 KiB |
345
oop_hw5/hw4/题目以及答案文字部分.md
Normal file
@@ -0,0 +1,345 @@
|
||||
<!--
|
||||
* @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
|
||||
* @Date: 2025-05-12 13:19:46
|
||||
* @LastEditors: error: git config user.name & please set dead value or install git
|
||||
* @LastEditTime: 2025-05-12 14:10:25
|
||||
-->
|
||||
## 4.阅读代码,并按要求练习。
|
||||
``` cpp
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A(int num):data1(num) {}
|
||||
~A(){
|
||||
cout<<" Destory A"<<endl;
|
||||
}
|
||||
void f() const{
|
||||
cout<<" Excute A::f() ";
|
||||
cout<<" Data1="<<data1<<endl;
|
||||
}
|
||||
void g()
|
||||
{
|
||||
cout<<" Excute A::g() "<<endl;
|
||||
}
|
||||
private:
|
||||
int data1;
|
||||
};
|
||||
|
||||
class B : public A
|
||||
{
|
||||
public:
|
||||
B(int num1,int num2):A(num1),data2(num2) {}
|
||||
~B(){
|
||||
cout<<" Destory B"<<endl;
|
||||
}
|
||||
void f( ) const{
|
||||
cout<<" Excute B::f() ";
|
||||
cout<<" Data1="<< data1;
|
||||
cout<<" Data2="<<data2<<endl;
|
||||
}
|
||||
void f(int n) const{
|
||||
cout<<" Excute B::f(int) ";
|
||||
cout<<" n="<<n;
|
||||
cout<<" Data1="<< data1;
|
||||
cout<<" Data2="<<data2<<endl;
|
||||
}
|
||||
void h(){
|
||||
cout<<" Excute B::h() "<<endl;
|
||||
}
|
||||
private:
|
||||
int data2;
|
||||
};
|
||||
```
|
||||
|
||||
## 下面是问题
|
||||
- ##### 1)完成 B 类的构造函数,使得参数 num1 对应 data1,num2 对应 data2;
|
||||
|
||||
✅已完成:
|
||||
|
||||
```cpp
|
||||
B(int num1, int num2) : A(num1), data2(num2) {}
|
||||
```
|
||||
|
||||
这正确地将 `num1` 传递给基类 A,`num2` 初始化 `data2`。✅
|
||||
|
||||
---
|
||||
|
||||
- ##### 2)尝试在 main 函数中使用这两个类,编译程序看是否有编译错误?指出错误的原因。
|
||||
|
||||
|
||||
✅已完成:
|
||||
|
||||
```cpp
|
||||
B b(1, 2);
|
||||
b.f();
|
||||
b.g();
|
||||
b.f(3);
|
||||
b.h();
|
||||
```
|
||||
|
||||
若此时 A 中 `data1` 是 `private`,则:
|
||||
|
||||
* ✅ `b.f()`、`b.g()`、`b.f(3)`、`b.h()` 都能编译通过。
|
||||
* ⚠️ 但 `B::f()` 中访问 `data1` 会报错(因为 `B` 不能访问 `A::data1` 的 `private` 成员)。
|
||||
---
|
||||
|
||||
- ##### 3)将基类中的 private 改为 protected,再编译。理解 protected 访问权限,在public 继承方式下的可访问性。
|
||||
|
||||
|
||||
|
||||
✅ 将 A 的成员 `private → protected` 后
|
||||
|
||||
* `B` 就可以访问 `A::data1`。
|
||||
* 所以 `B::f()` 和 `f(int)` 中访问 `data1` 合法。
|
||||
* `protected` 在 `public` 继承下,**对子类可见,对外仍然不可见**。
|
||||
|
||||
---
|
||||
- ##### 4)修改 main 函数,如下所示,看看哪些语句合法?为什么?执行的是基类的实现,还是派生类的实现?
|
||||
``` cpp
|
||||
int main()
|
||||
{
|
||||
B b(1,2);
|
||||
b.f();
|
||||
b.g();
|
||||
b.f(3);
|
||||
b.h();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
✅已完成:
|
||||
|
||||
```cpp
|
||||
int main() {
|
||||
B b(1, 2);
|
||||
b.f(); // ✅ 调用 B::f()
|
||||
b.g(); // ✅ 调用 A::g()
|
||||
b.f(3); // ✅ 调用 B::f(int)
|
||||
b.h(); // ✅ 调用 B::h()
|
||||
}
|
||||
```
|
||||
|
||||
**全部合法**,执行的都是定义在类中的相应成员函数,优先使用派生类的版本。
|
||||
|
||||
---
|
||||
- ##### 5)将继承 A 类的继承方式改为 private,编译能通过吗?再执行 4)中的main 函数,看看哪些语句变得不合法了?为什么?
|
||||
✅ 改为 `private` 继承后:
|
||||
|
||||
```cpp
|
||||
class B : private A
|
||||
```
|
||||
|
||||
现在,`A` 的成员都变成 `B` 的私有成员。
|
||||
|
||||
* `main()` 中 `b.g();` 不合法(`A::g()` 是 `private` 了)
|
||||
* `b.f();` 也不合法,因为 `A::f()` 被隐藏
|
||||
* ❌ **除了 `b.f(3)` 和 `b.h()` 还行,其余都报错**
|
||||
|
||||
---
|
||||
|
||||
- ##### 6)将继承 A 类的继承方式改回 public,并实现 B 类自定义的拷贝构造和赋值函数。
|
||||
|
||||
✅已构造A类的operator+
|
||||
|
||||
```cpp
|
||||
class B : public A {
|
||||
// ...
|
||||
public:
|
||||
B(const B& other) : A(other), data2(other.data2) {}
|
||||
B& operator=(const B& other) {
|
||||
if (this != &other) {
|
||||
A::operator=(other);
|
||||
//或者
|
||||
this->A::operator=(other);
|
||||
//这种写法也可以
|
||||
data2 = other.data2;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
- ##### 7)分别创建 A 和 B 类的两个对象 a 和 b,分别执行 a.f(),b.f(),a.g(),b.g(),a.f(1),b.f(1),a.h(),b.h(),请问哪些可以通过编译,执行结果如何?
|
||||
✅ 调用:
|
||||
|
||||
```cpp
|
||||
A a(1);
|
||||
B b(2, 3);
|
||||
a.f(); // ✅ A::f()
|
||||
b.f(); // ✅ B::f()
|
||||
a.g(); // ✅ A::g()
|
||||
b.g(); // ✅ A::g() 继承而来
|
||||
a.f(1); // ❌ 无此函数
|
||||
b.f(1); // ✅ B::f(int)
|
||||
a.h(); // ❌ 无此函数
|
||||
b.h(); // ✅ B::h()
|
||||
```
|
||||
|
||||
---
|
||||
- ##### 8)增加代码 A * p=new B(1,2);,理解向上类型转换的安全性。
|
||||
✅已完成:
|
||||
```cpp
|
||||
A* p = new B(1, 2);
|
||||
```
|
||||
|
||||
✅ 安全,称为“向上转型”(upcasting),**B 是 A 的子类**。安全的主要原因:
|
||||
“向上类型转换的安全性”是面向对象编程中一个很关键但也容易被忽略的点,尤其在 C++ 中。
|
||||
|
||||
---
|
||||
|
||||
## ✅ 什么是向上类型转换(Upcasting)?
|
||||
|
||||
向上类型转换:**将派生类对象的指针或引用转换为基类类型的指针或引用**。
|
||||
|
||||
比如:
|
||||
|
||||
```cpp
|
||||
class A { };
|
||||
class B : public A { };
|
||||
|
||||
B b;
|
||||
A* pa = &b; // 向上类型转换(Upcasting)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 安全性的本质
|
||||
|
||||
### ✅ **向上转换是安全的、隐式允许的**
|
||||
|
||||
因为:
|
||||
|
||||
* 每个派生类对象**本质上包含一个完整的基类子对象部分**。
|
||||
* 所以把 `B*` 转为 `A*` 是有意义的:`A` 的部分总是存在的。
|
||||
|
||||
> 换句话说,你永远可以“安全地把一个更具体的对象视为一个更抽象的对象”。
|
||||
|
||||
---
|
||||
|
||||
## ✅ 示例
|
||||
|
||||
```cpp
|
||||
class A {
|
||||
public:
|
||||
void foo() { cout << "A::foo\n"; }
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
void bar() { cout << "B::bar\n"; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
B b;
|
||||
A* pa = &b; // 向上转换,安全
|
||||
|
||||
pa->foo(); // ✅合法,调用 A 的函数
|
||||
// pa->bar(); // ❌错误:A 类型中没有 bar()
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ❗ 注意事项:类型“退化”
|
||||
|
||||
向上转换后:
|
||||
|
||||
* **只能访问基类中的成员**,即便实际指向的是一个派生类对象;
|
||||
* 若函数是非虚函数,则会“静态绑定”到基类版本;
|
||||
* 若函数是虚函数,则会“动态绑定”到派生类重写版本(见多态);
|
||||
|
||||
---
|
||||
|
||||
## ✅ 与向下转换对比
|
||||
|
||||
| 类型转换方式 | 安全性 | 是否隐式 | 风险 |
|
||||
| --------- | ----- | ---- | ---------------- |
|
||||
| 向上转换(B→A) | ✅ 安全 | ✅ 是 | 无 |
|
||||
| 向下转换(A→B) | ❌ 不安全 | ❌ 否 | 若对象本非 B 类型,结果未定义 |
|
||||
|
||||
|
||||
|
||||
---
|
||||
- ##### 9)在 8)的基础上,执行 p->f(),输出是什么?与 B* p=new B(1,2); p->f();的结果一样吗?
|
||||
✅执行后:
|
||||
|
||||
默认情况下(`f()` 非虚函数):
|
||||
|
||||
```cpp
|
||||
p->f(); // 输出 A::f()
|
||||
```
|
||||
|
||||
若你希望执行的是 `B::f()`,**应将 A 的 `f()` 声明为 `virtual`**。
|
||||
|
||||
---
|
||||
- ##### 10)在 8)的基础上,执行 p->f(1),能通过编译吗?为什么?
|
||||
✅编译?
|
||||
|
||||
```cpp
|
||||
p->f(1); // ❌ 报错:A 类中无 f(int)
|
||||
```
|
||||
|
||||
即使实际对象是 B,编译器只看指针类型(A),而 A 没有 `f(int)`。
|
||||
|
||||
---
|
||||
- ##### 11)在 8)的基础上,执行 p->g()和 p->h(),能行吗?为什么?
|
||||
✅编译?
|
||||
|
||||
```cpp
|
||||
p->f(1); // ❌ 报错:A 类中无 f(int)
|
||||
```
|
||||
|
||||
即使实际对象是 B,编译器只看指针类型(A),而 A 没有 `f(int)`。
|
||||
|
||||
---
|
||||
- ##### 12)在 8)的基础上,执行 delete p;,输出是什么?B 类的析构函数执行了吗?
|
||||
✅输出:
|
||||
|
||||
如果 `A` 的析构函数不是 `virtual`,那么:
|
||||
|
||||
```cpp
|
||||
delete p; // 只会调用 A 的析构函数,❌ 不会调用 B 的析构函数
|
||||
```
|
||||
|
||||
输出:
|
||||
|
||||
```
|
||||
Destory A
|
||||
```
|
||||
|
||||
⚠️ 此时会发生**资源泄漏**,因为 `B` 中资源未释放!
|
||||
|
||||
✅ 正确写法是:
|
||||
|
||||
```cpp
|
||||
class A {
|
||||
public:
|
||||
virtual ~A() {
|
||||
cout << "Destory A" << endl;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
这样 `delete p;` 才会调用 B 的析构函数 → 输出顺序:
|
||||
|
||||
```
|
||||
Destory B
|
||||
Destory A
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 总结重点
|
||||
|
||||
| 问题 | 核心知识 |
|
||||
| ---- | -------------------------- |
|
||||
| 2–3 | 访问权限(private vs protected) |
|
||||
| 4–5 | 继承方式(public/private)影响成员访问 |
|
||||
| 6 | 拷贝构造函数 & 赋值操作符 |
|
||||
| 7–11 | 多态、向上转型、成员函数隐藏 |
|
||||
| 12 | 虚析构函数,资源释放 |
|
||||
|
||||
---
|
||||
|
128
oop_hw5/hw5/main.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A(int num) : data(num) {
|
||||
cout << "A constructed with data = " << data << endl;
|
||||
}
|
||||
void AFuncs() {
|
||||
cout << "This is A's public function!" << endl;
|
||||
}
|
||||
A(const A& other) : data(other.data) {
|
||||
cout << "A copy constructor called." << endl;
|
||||
}
|
||||
A& operator=(const A& other) {
|
||||
if (this != &other) {
|
||||
data = other.data;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~A() {
|
||||
cout << "A destructor called." << endl;
|
||||
}
|
||||
|
||||
protected:
|
||||
int data;
|
||||
};
|
||||
|
||||
class B {
|
||||
public:
|
||||
B(int num) : value(num) {
|
||||
cout << "B constructed with value = " << value << endl;
|
||||
}
|
||||
void BFuncs() {
|
||||
cout << "This is B's public function!" << endl;
|
||||
}
|
||||
B(const B& other) : value(other.value) {
|
||||
cout << "B copy constructor called." << endl;
|
||||
}
|
||||
B& operator=(const B& other) {
|
||||
if (this != &other) {
|
||||
value = other.value;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~B() {
|
||||
cout << "B destructor called." << endl;
|
||||
}
|
||||
|
||||
protected:
|
||||
int value;
|
||||
};
|
||||
|
||||
// === ԭʼ<D4AD><CABC><EFBFBD>̳а汾 ===
|
||||
class C1 : public A, private B {
|
||||
public:
|
||||
C1(int num1, int num2, int y) : A(num1), B(num2), yyy(y) {
|
||||
cout << "C1 constructed with yyy = " << yyy << endl;
|
||||
}
|
||||
void MyFuncs() {
|
||||
BFuncs();
|
||||
cout << "This function calls B::BFuncs() !" << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int yyy;
|
||||
};
|
||||
|
||||
// === <20><><EFBFBD>̳<EFBFBD> + <20><><EFBFBD>ϰ汾 ===
|
||||
class C2 : public A {
|
||||
public:
|
||||
C2(int num1, int num2, int y)
|
||||
: A(num1), b(num2), yyy(y) {
|
||||
cout << "C2 constructed with yyy = " << yyy << endl;
|
||||
}
|
||||
|
||||
C2(const C2& other)
|
||||
: A(other), b(other.b), yyy(other.yyy) {
|
||||
cout << "C2 copy constructor called." << endl;
|
||||
}
|
||||
|
||||
C2& operator=(const C2& other) {
|
||||
if (this != &other) {
|
||||
A::operator=(other);
|
||||
b = other.b;
|
||||
yyy = other.yyy;
|
||||
}
|
||||
cout << "C2 assignment operator called." << endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~C2() {
|
||||
cout << "C2 destructor called." << endl;
|
||||
}
|
||||
|
||||
void MyFuncs() {
|
||||
b.BFuncs();
|
||||
cout << "This function calls B::BFuncs() !" << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
B b;
|
||||
int yyy;
|
||||
};
|
||||
|
||||
// === <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ===
|
||||
int main() {
|
||||
cout << "=== <20><><EFBFBD>̳в<CCB3><D0B2><EFBFBD> ===" << endl;
|
||||
C1 obj1(10, 20, 30);
|
||||
obj1.AFuncs();
|
||||
obj1.MyFuncs();
|
||||
cout << endl;
|
||||
|
||||
cout << "=== <20><><EFBFBD>̳<EFBFBD> + <20><><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD> ===" << endl;
|
||||
C2 obj2(100, 200, 300);
|
||||
obj2.AFuncs();
|
||||
obj2.MyFuncs();
|
||||
|
||||
cout << "--- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ---" << endl;
|
||||
C2 obj3 = obj2;
|
||||
|
||||
cout << "--- <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> ---" << endl;
|
||||
C2 obj4(1, 2, 3);
|
||||
obj4 = obj3;
|
||||
|
||||
cout << "--- <20><><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ʼ ---" << endl;
|
||||
return 0;
|
||||
}
|