首页 > 其他分享 >实验3 类与数组、指针

实验3 类与数组、指针

时间:2023-11-05 21:34:07浏览次数:30  
标签:const 数组 Point int void 实验 vectorPoint ptr 指针

1、实验任务1

源码Point.hpp

#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0): x{x0}, y{y0} {   
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << ", " << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}

 

源码task1.cpp

#include <iostream>
#include "point.hpp"
#include <vector>

using std::vector;
using std::cin;

void output(const vector<Point> &v) {
    for(auto &t: v)
        t.show();
}

void test() {
    int n;
    cout << "输入动态Point数组类对象中元素个数: ";
    cin >> n;

    vector<Point> x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    vector<Point> y(x); 
    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y);
    
    cout << "\n更新x对象......" << endl;
    x.at(0).move(30, 50);  
    x.push_back(Point(2, 2));  

    cout << "\nx对象中所有点坐标信息: " << endl;
    output(x);          
    cout << "\ny对象中所有点坐标信息: " << endl; 
    output(y);           
}

int main() {
    test();
}

 

task1运行测试截图:

 

Task 1 (问答)

问题1:对x对象进行更新时,基于 vector 对象x创建的对象y是否发生变化?

答:不发生变化

问题2:标准库模板类vector在复制一个动态数组对象时,实现的是深复制还是浅复制?

答:深复制

 

 

2、实验任务2

源码Point.hpp

#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0): x{x0}, y{y0} {   
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << ", " << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}

 

源码vectorPoint.hpp

#pragma once

#include "point.hpp"
#include <cassert>
#include <iostream>

class vectorPoint{
public:
    vectorPoint(int n);
    ~vectorPoint();
    
    int get_size() const;  
    Point& at(int index);   
    Point& at(int index) const; 

private:
    int size;
    Point *ptr;
};

vectorPoint::vectorPoint(int n) : size{n} {
    ptr = new Point[n];
}

vectorPoint::~vectorPoint() {
    delete[] ptr;
}

int vectorPoint::get_size() const {
    return size;
}

Point& vectorPoint::at(int index) {
    assert(index >= 0 && index < size);  
    return ptr[index];
}

Point& vectorPoint::at(int index) const {
    assert(index >= 0 && index < size);  
    return ptr[index];
}

 

源码task2.cpp

#include "vectorPoint.hpp"
#include <iostream>

void output(const vectorPoint &v) {
    for(auto i = 0; i < v.get_size(); ++i)
        v.at(i).show();
}

void test() {
    using namespace std;

    int n;
    cout << "输入vectorPoint对象中元素个数: ";
    cin >> n;

    vectorPoint x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    vectorPoint y(x);
    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 

    cout << "\n更新x对象中点坐标信息......" << endl;
    x.at(0).move(30, 50);
    x.at(1).move(-1, -1);

    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 
}

int main() {
    test();
}

 

task2运行测试截图:

 

Task 2(问答)

问题1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化?

答:发生变化

问题2:编译器为vectorPoint类创建的默认复制构造函数,在复制一个动态数组对象时,实现的是深复制还是浅复制?

答:浅复制

问题3:在类vectorPoint内部,手动增加的以下复制构造函数声明和定义,实现的是浅复制还是深复制?

答:浅复制

*构造函数声明:vectorPoint(const vectorPoint &vp);

*构造函数定义:vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{vp.ptr}

 

 

3、实验任务3

源码Point.hpp

#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0): x{x0}, y{y0} {   
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << ", " << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}

 

源码vectorPoint.hpp

#pragma once

#include "point.hpp"
#include <cassert>
#include <iostream>

class vectorPoint{
public:
    vectorPoint(int n);
    vectorPoint(const vectorPoint &vp);
    ~vectorPoint();
    
    int get_size() const;  
    Point& at(int index);  
    Point& at(int index) const;  

private:
    int size; 
    Point *ptr;
};

vectorPoint::vectorPoint(int n) : size{n} {
    ptr = new Point[n];
}

vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new Point[size]} {
    for(auto i = 0; i < size; ++i)
        ptr[i] = vp.ptr[i];
}

vectorPoint::~vectorPoint() {
    delete[] ptr;
}

int vectorPoint::get_size() const {
    return size;
}

Point& vectorPoint::at(int index) {
    assert(index >= 0 && index < size); 
    return ptr[index];
}

Point& vectorPoint::at(int index) const {
    assert(index >= 0 && index < size);  
    return ptr[index];
}

 

源码task3.cpp

#include "vectorPoint.hpp"
#include <iostream>

void output(const vectorPoint &v) {
    for(auto i = 0; i < v.get_size(); ++i)
        v.at(i).show();
}

void test() {
    using namespace std;

    int n;
    cout << "输入vectorPoint对象中元素个数: ";
    cin >> n;

    vectorPoint x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    vectorPoint y(x);
    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 

    cout << "\n更新x对象中点坐标信息......" << endl;
    x.at(0).move(30, 50);
    x.at(1).move(-1, -1);

    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 
}

int main() {
    test();
}

 

task3运行测试截图:

 

Task 3(问答)

问题1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化?

答:不发生变化

问题2:这个vectorPoint 类的实现中,复制构造函数实现的是深复制还是浅复制?

答:深复制

问题3:基于实验任务2和3,总结当类的成员中包含指针域成员时深复制与浅复制的区别。

答:在进行深复制时,我们需要将原先被复制的数据内容重新分配新的空间,这样在存储数据的旧存储空间被系统回收释放时,新指针指向的新地址存放的数据不会收到影响,这样才能够进行成员内容的深复制。需要注意的是,仅将原来的旧地址复制分配给新指针指向的新地址,新旧指针指向同一个地址,这样只能实现成员内容的浅复制,当原有的数据内容被更改后,新地址的数据会受到影响。

 

 

4、实验任务4

源码task4_1.cpp

# include<iostream>
using namespace std;

void swap1(int& rx, int& ry);
void swap2(int* px, int* py);
void print(int x, int y);

void test() {
    int x = 3, y = 4;

    print(x, y);
    swap1(x, y);
    print(x, y);

    cout << endl;

    x = 3, y = 4;
    print(x, y);
    swap2(&x, &y);
    print(x, y);
}

int main() {
    test();
}

void swap1(int& rx, int& ry) {
    int t;

    t = rx; rx = ry; ry = t;
}

void swap2(int* px, int* py) {
    int t;

    t = *px; *px = *py; *py = t;
}

void print(int x, int y) {
    std::cout << "x = " << x << ", y = " << y << "\n";
}

 

task4_1运行测试截图:

 

源码task4_2.cpp

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

int main() {
    int a;

    int& ra = a;
    ra = 4;

    int* pa = &a;
    *pa = 5;

    //以十六进制形式输出普通变量a,引用变量ra,指针变量pa的地址
    cout << "&a = " << hex << &a << endl;
    cout << "&ra = " << hex << &ra << endl;
    cout << "&pa = " << hex << &pa << "\n\n";

    //输出普通变量a,引用变量ra,指针变量pa的值
    cout << "a = " << a << endl;
    cout << "ra = " << ra << endl;
    cout << "pa = " << hex << pa << endl;

    //输出指针变量pa指向的变量的值
    cout << "*pa = " << *pa << "\n\n";

    //输出普通变量a,引用变量ra,指针变量pa的类型信息
    cout << "type a: " << typeid(a).name() << endl;
    cout << "type ra: " << typeid(ra).name() << endl;
    cout << "type pa: " << typeid(pa).name() << endl;
}

 

task4_2运行测试截图:

 

源码task4_3.cpp

# include<iostream>
# include<vector>

using namespace std;

template<typename T>
void output(const T& x) {
    for (auto i : x)
        std::cout << i << ",";
    std::cout << "\b \b \n";
}

template<typename T>
void square1(T& x) {
    for (auto i : x)//i是普通类型
        i *= i;
}

template <typename T>
void square2(T& x) {
    for (auto& i : x)//i是引用类型
        i *= i;
}

void test1() {
    vector<int> x{ 1,2,3,4,5 };

    cout << "动态int型数组对象x内的元素值: ";
    output(x);

    cout << "调用函数square1()......" << endl;
    square1(x);

    cout << "动态int型数组对象x内的元素值: ";
    output(x);
}

void test2() {
    vector<int> x{ 1,2,3,4,5 };

    cout << "动态int型数组对象x内的元素值: ";
    output(x);

    cout << "调用函数square2()......" << endl;
    square2(x);

    cout << "动态int型数组对象x内的元素值: ";
    output(x);
}

int main() {
    cout << "测试1: " << endl;
    test1();

    cout << "\n测试2: " << endl;
    test2();
}

 

task4_3运行测试截图:

 

Task 4(问答)

问题:用文字总结引用类型、指针类型的区别。

答:引用类型:是对原有类型的复制,可以认为是原类型的“新名字”,两者数据内容均存储在同一个地址内,数据内容也完全一致

       指针类型:是通过新指针指向新的空间,将原类型的数据内容存进这个新地址当中,和原类型的存储地址不同。指针本质是存储的地址,但是通过“*”我们可以间接访问该指针指向地址中存储的数据内容。所以当一个指针定义为int *p,我们可以通过“*p”实现访问原类型int的数据内容。

 

 

5、实验任务5

源码vectorInt.hpp(动态int型数组类vectorInt定义和实现源码)

# pragma once
# include<iostream>
# include<cassert>

using namespace std;

class vectorInt {
public:
    vectorInt(int n);
    vectorInt(int n ,int value);
    vectorInt(const vectorInt& vi);
    ~vectorInt();

    int get_size()const;
    int& at(int index);
    int& at(int index) const;

private:
    int size;
    int* ptr;
};

vectorInt::vectorInt(int n) : size{ n } {
    ptr = new int[n];
    cout << "constructor vectorInt(int n) called." << endl;
}

vectorInt::vectorInt(int n, int value) : size{ n } {
    ptr = new int[n];
    for(auto i = 0; i < size; ++i)
    ptr[i] = value;
    cout << "constructor vectorInt(int n, int value) called." << endl;
}

vectorInt::vectorInt(const vectorInt& vi) : size{ vi.size }, ptr{ new int[size] } {
    for (auto i = 0; i < size; ++i)
        ptr[i] = vi.ptr[i];
    cout << "copy constructor called." << endl;
}

vectorInt::~vectorInt() {
    delete[] ptr;
    cout << "destructor called." << endl;
}

int vectorInt::get_size() const {
    return size;
}

int & vectorInt::at(int index) {
    assert(index >= 0 && index < size);
    return ptr[index];
}

int& vectorInt::at(int index) const {
    assert(index >= 0 && index < size);
    return ptr[index];
}

 

源码task5.cpp测试(vectorInt类的完整代码)

# include"vectorInt.hpp"
# include<iostream>

using std::cout;
using std::cin;
using std::endl;

void output(const vectorInt& vi) {
    for (auto i = 0; i < vi.get_size(); ++i)
        cout << vi.at(i) << ",";
    cout << "\b \b \n";
}

void test() {
    int n;
    cout << "输出vectorInt对象中元素个数:";
    cin >> n;

    vectorInt x1(n);  //构造动态int数组对象x1,包含n个元素,不对元素初始化
    for (auto i = 0; i < n; ++i)
    x1.at(i) = i * i;
    cout << "vectorInt对象x1: ";
    output(x1);

    vectorInt x2(n, 42); //构造动态int数组对象x2,包含n个元素,每个元素初始值为42
    cout << "vectorInt对象x2: ";
    output(x2);
    vectorInt x3(x2);
    cout << "vectorInt对象x3: ";
    output(x3);

    cout << "更新vectorInt对象x2......\n";
    x2.at(0) = 77;
    x2.at(1) = -999;

    cout << "vectorInt对象x2: ";
    output(x2);
    cout << "vectorInt对象x3: ";
    output(x3);
}

int main() {
    test();
}

 

task5运行测试结果截图:

 

 

6、实验任务6

源码matrix.hpp(类Matrix的定义和实现完整代码)

#pragma once

#include <iostream>
#include <cassert>

using std::cout;
using std::endl;

class Matrix {
public:
    Matrix(int n, int m);  
    Matrix(int n);  
    Matrix(const Matrix& x); 
    ~Matrix();

    void set(const double* pvalue);  
    void set(int i, int j, double value);  

    double& at(int i, int j) const; 
    double& at(int i, int j); 

    int get_lines() const; 
    int get_cols() const; 

    void print() const; 

private:
    int lines;
    int cols;
    double* ptr;
};

Matrix::Matrix(int n, int m):lines{n}, cols{m}{
    ptr = new double[n * m];
}
Matrix::Matrix(int n):lines{n}, cols{n}{
    ptr = new double[n * n];
}
Matrix::Matrix(const Matrix& x) :lines{ x.lines }, cols{ x.cols }, ptr{ new double[lines * cols] } {
    for (int i = 0; i < lines; ++i) {
        for (int j = 0; j < cols; ++j) {
           ptr[cols * i + j] = x.ptr[cols * i + j];
        }
    }
}

Matrix::~Matrix() {
    delete [] ptr;
};

void Matrix::set(const double* pvalue) {
    for (int i = 0; i < (lines * cols); ++i)
        ptr[i] = pvalue[i];
}

void Matrix::set(int i, int j, double value) {
    ptr[cols * i + j] = value;
}

double& Matrix::at(int i, int j) const {
    assert(i * j >= 0 && i * j < lines * cols);
    return ptr[cols * i + j];
}

double& Matrix::at(int i, int j)  {
    assert(i * j >= 0 && i * j < lines * cols);
    return ptr[cols * i + j];
}

int Matrix::get_lines() const {
    return lines;
}

int Matrix::get_cols() const {
    return cols;
}

void Matrix::print() const {
    for (int i = 0; i < lines; ++i){
        for (int j = 0; j < cols; ++j) {
            cout << ptr[cols * i + j] << ",";
        }
        cout << "\b \b \n";
     }

 

源码task6.cpp测试(测试代码)(备注:数组x换一组测试数据)

#include <iostream>
#include "matrix.hpp"

using namespace std;

const int N1 = 3;
const int N2 = 2;

void output(const Matrix& m, int index) {
    for (auto j = 0; j < m.get_cols(); ++j)
        cout << m.at(index, j) << ",";
    cout << "\b \b \n";
}

void test() {


    double x[N1 * N2] = { 3,5,2,7,6,8 }; 

    Matrix m1(N1, N2);  
    m1.set(x); 
    cout << "矩阵对象m1: " << endl;
    m1.print();  
    cout << "矩阵对象m1第0行是: " << endl;
    output(m1, 0);
    cout << endl;

    Matrix m2(N2, N1);
    m2.set(x);
    cout << "矩阵对象m2: " << endl;
    m2.print();
    cout << "矩阵对象m2第0行是: " << endl;
    output(m2, 0);
    cout << endl;

    Matrix m3(m2);  
    m3.set(0, 0, 999);  // 将矩阵对象m2索引(0,0)元素设为999
    cout << "矩阵对象m3:" << endl;
    m3.print();
    cout << endl;

    Matrix m4(2);   
    m4.set(x);   
    cout << "矩阵对象m4:" << endl;
    m4.print();
}

int main() {
    test();
}

 

task6运行测试截图:

 

 

实验总结:对于深复制和浅复制、引用类型和指针类型的区分有了更深刻的学习和理解;同时在程序设立类的过程中,可以简单通过申请新空间存储内容、释放回收空间、指针的建立和深复制等,实现对数据内容的传递应用。

标签:const,数组,Point,int,void,实验,vectorPoint,ptr,指针
From: https://www.cnblogs.com/Rainbow-forest/p/17811078.html

相关文章

  • 实验三
    任务一task1.cpp点击查看代码#include<iostream>#include"point.hpp"#include<vector>usingstd::vector;usingstd::cin;voidoutput(constvector<Point>&v){for(auto&t:v)t.show();}voidtest(){intn;cin>>n;v......
  • 用线段树来接树状数组类的问题
    大致解决的问题就是区间查询以及单点的修改#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintN=5e5+10;inta[N],tag[N<<2];struct{ struct{ intl,r,sum; }tr[N<<2]; voidpush_up(inti){ tr[i].sum=tr[i<<1].sum+tr[i<......
  • 实验三 类与数组,指针
    任务1:1.代码:point.hpp:1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;6classPoint{7public:8Point(intx0=0,inty0=0);9~Point()=default;10intget_x()const;11intget_y()const;12voidshow(......
  • 实验一 类和对象_基础编程1
    任务一task1.cpp#include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";st......
  • 实验三
    task1源代码:#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmove(int......
  • js把json字符串转成json数组
    如何将JSON字符串转换为JSON数组。假设你有以下JSON字符串,它表示一个简单的数组,其中包含两个对象:'[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]'要将这个JSON字符串转换为JavaScript中的数组对象,你可以使用JSON.parse()方法。这个方法接受一个J......
  • 查找数组中元素
    1.代码#include<stdio.h>intmain(){inta[6]={60,75,95,80,65,90},b;scanf("%d",&b);if(b!=a[0]&&b!=a[1]&&b!=a[2]&&b!=a[3]&&b!=a[4]&&b!=a[5]){printf("NotIncluded......
  • 实验3 类与数组指针
    task1 point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;void......
  • 实验3 C语言函数应用编程
    实验任务1源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);//函数声明9voidprint_blan......
  • 实验三 类与数组、指针
    任务一point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmove(intnew_x,intnew_y);pri......