#include<cmath>
#include<iostream>
using namespace std;

class Vector {
private:
    double* array = new double[4];
    double length;
public:
    Vector(double arr[4])
    {
        this->array[0] = arr[0];
        this->array[1] = arr[1];
        this->array[2] = arr[2];
        this->array[3] = arr[3];
        this->length = sqrt(((arr[0] - arr[1]) * (arr[0] - arr[1])) + ((arr[2] - arr[3]) * (arr[2] - arr[3])));
    }
    ~Vector()
    {
        delete[]array;
        this->length = 0;
    }
    Vector* clone()
    {
        return new Vector(*this);
    }

    Vector(const Vector& vector)
    {
        //浅克隆
        this->array = vector.array;
        this->length = vector.length;

    }
    void show()
    {
        cout << "向量长度:" << this->length << endl;
    }
};
int main()
{
    double s[4] = { 1, 2, 3, 4 };
    Vector* v1 = new Vector(s);
    Vector* v2 = v1->clone();
    v1->show();
    v2->show();
    return 0;
}
实验六

#include<iostream>
#include<string>
#include <string.h>
using namespace std;
//单例角色——StudentNo类
class StudentNo {
private:
    static StudentNo* instance;
    string no;
    StudentNo() {}
    void setStudentNo(string n) {
        no = n;
    }
public:
    static StudentNo* getInstance() {
        if (instance == NULL) {
            cout << "第一次注册,分配新的学号" << endl;
            instance = new StudentNo();
            instance->setStudentNo("20203935");
        }
        else {
            cout << "重复注册,获取旧的学号" << endl;
        }
        return instance;
    }
    string getStudentNo() {
        return no;
    }
};
StudentNo* StudentNo::instance = NULL;
int main() {
    StudentNo* x, * y;
    x = StudentNo::getInstance();
    y = StudentNo::getInstance();
    cout << "学号是否一致";
    if (x == y) {
        cout << "true" << endl;
    }

    string a, b;
    a = x->getStudentNo();
    b = y->getStudentNo();
    cout << "第一次学号是" << a << endl;
    cout << "第二次学号是" << b << endl;
    cout << "内容是否相等";
    if (strcmp(a.c_str(), b.c_str()) == 0) {
        cout << "true" << endl;
    }
    cout << "是否是相同对象";
    if (a == b) {
        cout << "true" << endl;
    }
    return 0;
}
实验七

 

posted on 2023-11-02 08:51  夜的第七章i  阅读(5)  评论(0编辑  收藏  举报