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

实验3 类与数组、指针

时间:2023-11-05 10:55:20浏览次数:32  
标签:std const vectorPoint Point int 实验 数组 size 指针

实验任务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;
}

task.cpp


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


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


// 输出vector<Point>对象内所有点的坐标
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();
}

 

 

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

 

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

 

实验任务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();
}

 

 

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

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

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

 

实验任务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();
}

 

 

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

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

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

浅复制:只复制指针本身,而不复制指针指向的内存区域。这意味着如果两个对象都包含指向同一个对象的指针,那么浅复制后这两个对象将共享同一个对象。这可能会导致一些问题,例如当一个对象修改指向的对象时,另一个对象也会受到影响。
深复制:不仅复制指针本身,还复制指针指向的内存区域。深复制确保每个对象都有自己独立的副本,不会出现共享同一个对象的问题。

 

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_2.cpp

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

int main() {
    int a;
    
    int &ra = a;
    ra = 4;

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


    cout << "&a = " << hex << &a << endl;
    cout << "&ra = " << hex << &ra << endl;
    cout << "&pa = " << hex << &pa << "\n\n";
    
    cout << "a = " << a << endl;
    cout << "ra = " << a << endl;
    cout << "pa = " << hex << pa << endl;
    
    cout << "*pa = " << *pa << "\n\n";

    cout << "type a: " << typeid(a).name() << endl;
    cout << "type ra: " << typeid(ra).name() << endl;
    cout << "type pa: " << typeid(pa).name() << endl;
}

 

 

task4_3

#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;
}

template<typename T>
void square2(T &x) {
    for(auto &i: x)  
        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();
}

 

 

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

引用类型是一个变量的别名,它是对已存在变量的间接引用。引用类型在声明时必须初始化,并且在整个程序生命周期内不能更改。它不能独立存在,必须与被引用的变量绑定在一起。引用类型可以用来访问和修改被引用的变量的值。
指针类型是一个存储变量地址的类型。指针类型在声明时不一定要初始化,但在使用前必须指向一个已存在的变量。它可以独立存在。

 

task5

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

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


void output(const vectorInt& x) {
    for (int i = 0; i < x.get_size(); ++i) {
        cout << x.at(i) << " ";
    }
    cout << endl;
}


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

    vectorInt x1(n);    
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i*i;
    cout << "vectorInt对象x1: ";
    output(x1);

    vectorInt 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();
}

 

#include <iostream>

using namespace std;

class vectorInt {
public:
    vectorInt(int size, int value = 0) {
        cout << "Constructor called, size = " << size << ", value = " << value << endl;
        data = new int[size];
        for (int i = 0; i < size; ++i) {
            data[i] = value;
        }
    }

    vectorInt(const vectorInt& x) {
        cout << "Copy constructor called" << endl;
        data = new int[x.size()];
        for (int i = 0; i < x.size(); ++i) {
            data[i] = x.data[i];
        }
    }

    ~vectorInt() {
        cout << "Destructor called" << endl;
        delete[] data;
    }

    int at(int i) const {
        if (i >= 0 && i < size()) {
            return data[i];
        } else {
            throw out_of_range("Index out of range");
        }
    }

    int get_size() const {
        return size_;
    }

    void output() const {
        for (int i = 0; i < size(); ++i) {
            cout << data[i] << " ";
        }
        cout << endl;
    }

private:
    int* data;
    int size_;

    int size() const {
        return size_;
    }
}; 

 

 

 

task6

matrix.hpp

#pragma once

#include <iostream>
#include <vector>

class Matrix {
public:
    Matrix(int rows, int cols);
    Matrix(const std::vector<std::vector<int>>& data);

    int getRows() const;
    int getCols() const;
    const std::vector<int>& getRow(int row) const;
    const std::vector<std::vector<int>>& getData() const;

    void setData(const std::vector<std::vector<int>>& data);

private:
    int rows;
    int cols;
    std::vector<std::vector<int>> data;
};

Matrix::Matrix(int rows, int cols) : rows(rows), cols(cols) {
    data.resize(rows, std::vector<int>(cols, 0));
}

Matrix::Matrix(const std::vector<std::vector<int>>& data) : data(data) {
    rows = data.size();
    cols = data[0].size();
}

int Matrix::getRows() const {
    return rows;
}

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

const std::vector<int>& Matrix::getRow(int row) const {
    return data[row];
}

const std::vector<std::vector<int>>& Matrix::getData() const {
    return data;
}

void Matrix::setData(const std::vector<std::vector<int>>& data) {
    this->data = data;
    rows = data.size();
    cols = data[0].size();
}

 

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

using namespace std;

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

// 输出一个矩阵对象中索引为index对应的行的所有元素值
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] = {1, 2, 3, 4, 5, 6};

    Matrix m1(N1, N2);      // 创建一个N1×N2矩阵
    m1.set(x);              // 用一维数组x的值按行为矩阵m1赋值
    cout << "矩阵对象m1: " << endl;
    m1.print();             // 打印矩阵m1的值
    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);      // 用矩阵m2构造新的矩阵m3
    m3.set(0, 0, 999);  // 讲矩阵对象m2索引(0,0)元素设为999
    cout << "矩阵对象m3:" << endl;
    m3.print();
    cout << endl;

    Matrix m4(2);       // 创建一个2*2矩阵对象
    m4.set(x);          // 用一维数组x的值按行为矩阵m4赋值
    cout << "矩阵对象m4:" << endl;
    m4.print();
}

int main() {
    test();
}

 

 

标签:std,const,vectorPoint,Point,int,实验,数组,size,指针
From: https://www.cnblogs.com/yzsya/p/17797441.html

相关文章

  • 【教3妹学编程-算法题】使数组变美的最小增量运算数
    2哥 :3妹,脸上的豆豆好了没呢。3妹:好啦,现在已经没啦2哥 :跟你说很快就会消下去的,还不信~既然你的容颜和心情都如此美丽,那我们就再做一道关于美丽的题吧。3妹:切,2哥就会取笑我,伤心时让我做题,开心时也让我做题! 1题目: 给你一个下标从0开始、长度为n的整数数组nums,和一个整......
  • 数组的练习专项(接上节的数组的详解)
     练习1:求和需求:定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和代码示例:/*定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和*///分析://1.定义一个数组,并添加数据1,2,3,4,5int[]arr={1,2,3,4,5};//求和变量int......
  • 数组的练习专项(接上节的数组的详解)
     练习1:求和需求:定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和代码示例:/*定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和*///分析://1.定义一个数组,并添加数据1,2,3,4,5int[]arr={1,2,3,4,5};//求和变量int......
  • 实验三 类与数组、指针
    实验任务1task1.cpp源码:Point.hpp:1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13......
  • 实验3
    任务1源代码1#include<stdio.h>2#include<time.h>3#include<windows.h>4#include<stdlib.h>5#defineN806voidprint_line(intn);7voidprint_col(intn);8voidprint_text(intline,intcol,chartext[]);9intmain(){10......
  • 数组拷贝
    在a数组索引为1位置插入4然后将a拷贝到b行成新数组[2,5,8]a数组bint[]b=newint[a.length+1];System.arraycopy(a,0,b,0,1)将a数组的0到1索引的数拷贝到b数组起始为0的位置a:原数组0:a开始拷贝的位置b:拷贝到的数组0:拷贝到数组的起始位置1:a数组拷贝的......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain......
  • 面试必刷TOP101:20、数组中的逆序对
    题目题解解法一:暴力法importjava.util.*;publicclassSolution{/***代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可***@paramnumsint整型一维数组*@returnint整型*/publicintInversePairs(in......
  • 第三次实验
    任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain(){ ......
  • 用二分法寻找第7位数(数组)
    #include<stdio.h>intmain(){  intk=7;  intarr[]={1,2,3,4,5,6,7,8,9,10};  intsz=sizeof(arr)/sizeof(arr[0]);    //元素个数的计算公式  intleft=0;                //左下标  intright=sz-1;  ......