2023/12/28 c++ work

  1. 定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息
#include <iostream>

using namespace std;
//1.定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,
//包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,
//并写一个Stu的show函数,显示所有信息。

class Person{

  int *age;
  string &name;
public:
  Person(int age,string &name):age(new int(age)),name(name){
      cout << "person 一个参构造函数" << endl;
  }
  int get_age();
  string get_name();
  //拷贝函数
  Person(const Person &other):age(new int(*(other.age))),name(other.name){
      cout << "person 拷贝函数 " << endl;
  }
  //拷贝赋值函数
    Person &operator=(const Person &other){
        *(this->age) = *(other.age);
        name = other.name;
    }

  //析构函数,释放指针堆空间
  ~Person(){
      cout << "---------------------------------Person delete----------------------" << endl;
      cout << "age 地址信息 " << age << endl;
      cout << "释放age堆空间" << endl;
      delete age;
  }

};

class Stu{
    double *score;
    Person p1;
public:

    Stu(double score,int age,string name):score(new double(score)),p1(age,name){
        cout << "Stu 一个参数构造函数" << endl;
    }
    void show();
    //拷贝函数
    Stu(const Stu &other):score(new double(*(other.score))),p1(other.p1){
        cout << "Stu 拷贝函数" << endl;
    }
    //拷贝赋值函数
    Stu &operator=(const Stu &other){
        *(this->score) = *(other.score);
        this->p1 = other.p1;
    }

    //析构函数,释放指针堆空间
    ~Stu(){
        cout << "---------------------------------Stu delete----------------------" << endl;
        cout << "score 地址信息 " << score << endl;
        cout << "释放score堆空间" << endl;
        delete score;
    }

};

int Person::get_age(){
    return *age;
}

string Person::get_name(){
    return name;
}

void Stu::show(){

    cout << "score = " << *score << endl;
    cout << "person age = " << p1.get_age() << endl;
    cout << "person name = " << p1.get_name() << endl;
}

int main()
{
    Stu str(99.5,30,"张三");
    str.show();
    cout << "---------------------------------str1----------------------" << endl;
    Stu str1 = str; // 调用拷贝函数
    str1.show();
    cout << "---------------------------------str2----------------------" << endl;
    Stu str2(0,0,"");
    str2 = str;
    str2.show();
    return 0;
}

2.思维导图

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的C++代码实现银行家算法模拟: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; // 定义进程结构体 struct Process { int id; // 进程id vector<int> allocation; // 已分配资源数量 vector<int> max; // 最大资源需求量 vector<int> need; // 还需要的资源数量 bool finished; // 进程是否已完成 }; // 定义系统资源结构体 struct System { vector<int> available; // 可用资源数量 }; // 初始化进程信息 void initProcesses(vector<Process>& processes) { for (int i = 0; i < processes.size(); i++) { cout << "请输入进程" << processes[i].id << "的已分配资源数量:" << endl; for (int j = 0; j < processes[i].allocation.size(); j++) { cin >> processes[i].allocation[j]; } cout << "请输入进程" << processes[i].id << "的最大资源需求量:" << endl; for (int j = 0; j < processes[i].max.size(); j++) { cin >> processes[i].max[j]; processes[i].need[j] = processes[i].max[j] - processes[i].allocation[j]; // 计算还需要的资源数量 } } } // 初始化系统资源信息 void initSystem(System& system) { cout << "请输入系统可用资源数量:" << endl; for (int i = 0; i < system.available.size(); i++) { cin >> system.available[i]; } } // 安全性检查 bool isSafe(vector<Process>& processes, System& system) { vector<int> work = system.available; // 复制可用资源数量 vector<bool> finish(processes.size(), false); // 初始化进程完成状态 int count = 0; // 已完成进程数量 while (count < processes.size()) { bool found = false; // 是否找到满足条件的进程 for (int i = 0; i < processes.size(); i++) { if (!finish[i]) { // 进程未完成 bool enoughResources = true; // 是否有足够的资源分配给该进程 for (int j = 0; j < processes[i].need.size(); j++) { if (processes[i].need[j] > work[j]) { enoughResources = false; break; } } if (enoughResources) { // 有足够的资源分配给该进程 finish[i] = true; // 标记进程为已完成 count++; // 已完成进程数量+1 found = true; // 找到满足条件的进程 for (int j = 0; j < processes[i].allocation.size(); j++) { work[j] += processes[i].allocation[j]; // 更新可用资源数量 } } } } if (!found) { // 没有找到满足条件的进程,系统不安全 return false; } } return true; // 所有进程都已完成,系统安全 } // 银行家算法模拟 void bankerAlgorithm(vector<Process>& processes, System& system) { while (true) { bool found = false; // 是否找到满足条件的进程 for (int i = 0; i < processes.size(); i++) { if (!processes[i].finished) { // 进程未完成 bool enoughResources = true; // 是否有足够的资源分配给该进程 for (int j = 0; j < processes[i].need.size(); j++) { if (processes[i].need[j] > system.available[j]) { enoughResources = false; break; } } if (enoughResources) { // 有足够的资源分配给该进程 processes[i].finished = true; // 标记进程为已完成 found = true; // 找到满足条件的进程 for (int j = 0; j < processes[i].allocation.size(); j++) { system.available[j] += processes[i].allocation[j]; // 更新可用资源数量 } cout << "分配资源给进程" << processes[i].id << ",当前可用资源数量为:"; for (int j = 0; j < system.available.size(); j++) { cout << system.available[j] << " "; } cout << endl; } } } if (!found) { // 没有找到满足条件的进程,退出循环 break; } } bool allFinished = true; // 是否所有进程都已完成 for (int i = 0; i < processes.size(); i++) { if (!processes[i].finished) { allFinished = false; break; } } if (allFinished) { cout << "所有进程都已完成,安全!" << endl; } else { cout << "有进程未能完成,不安全!" << endl; } } int main() { int n; // 进程数量 int m; // 资源数量 cout << "请输入进程数量和资源数量:" << endl; cin >> n >> m; vector<Process> processes(n); // 进程数组 vector<int> available(m); // 可用资源数组 System system = { available }; // 系统资源 // 初始化进程信息 for (int i = 0; i < n; i++) { processes[i].id = i; processes[i].allocation.resize(m); processes[i].max.resize(m); processes[i].need.resize(m); } initProcesses(processes); // 初始化系统资源信息 system.available.resize(m); initSystem(system); // 进行安全性检查 if (isSafe(processes, system)) { cout << "系统安全!" << endl; // 进行银行家算法模拟 bankerAlgorithm(processes, system); } else { cout << "系统不安全!" << endl; } return 0; } ``` 在上面的代码中,我们通过定义进程结构体和系统资源结构体来存储进程和系统资源的信息。在main函数中,我们先初始化进程信息和系统资源信息,然后进行安全性检查,如果系统安全,就执行银行家算法模拟。 在模拟过程中,我们先检查每个进程是否已完成,如果未完成并且有足够的资源分配给该进程,就分配资源给该进程,并更新可用资源数量。重复执行该过程,直到所有进程都已完成为止。 希望这个代码能够帮到你。如果你有具体的问题或需求,欢迎随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值