任务一
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;
cin >> n;
vector<Point> x(n);
cout << "x" << endl;
output(x);
vector<Point> y(x);
cout << "\nx " << endl;
output(y);
cout << "\nupdate x" << endl;
x.at(0).move(30, 50);
x.push_back(Point(2, 2));
cout << "\nx total:" << endl;
output(x);
cout << "\ny total: " << endl;
output(y);
}
int main() {
test();
}
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;
}
1:对x对象进行更新时,基于 vector
答:不发生变化。
2:标准库模板类vector在复制一个动态数组对象时,实现的是深复制还是浅复制?
答:深复制。
任务二
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 yuansugeshu: ";
cin >> n;
vectorPoint x(n);
cout << "x information: " << endl;
output(x);
vectorPoint y(x);
cout << "\ny information: " << endl;
output(y);
cout << "\nupdate x information......" << endl;
x.at(0).move(30, 50);
x.at(1).move(-1, -1);
cout << "x information: " << endl;
output(x);
cout << "\ny information: " << endl;
output(y);
}
int main() {
test();
}
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); // 返回下标为index的元素引用
Point& at(int index) const; // 返回下标为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];
}
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;
}
1:观察更新对象x后,基于 vectorPoint 对象x创建的对象y是否发生变化?
答:发生变化
2:编译器为vectorPoint类创建的默认复制构造函数,在复制一个动态数组对象时,实现
的是深复制还是浅复制?
答:浅复制
3:在类vectorPoint内部,手动增加的以下复制构造函数声明和定义,实现的是浅复制还
是深复制?
答:浅复制
任务三
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.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;
}
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:用文字总结引用类型、指针类型的区别。
答:指针类型:本质是地址,可以独立存在,用于存放数据的地址。可以对指针本身进行操作,也可以通过指针去间接操作数据。引用类型:本质是别名,不能独立存在,依赖于源数据。所有操作同步反馈到源数据上。
任务五
vectorInt.hpp
点击查看代码
#include<iostream>
#include<cassert>
using namespace std;
class vectorInt
{
public:
vectorInt(int n);
vectorInt(int n,int m);
vectorInt(const vectorInt & vc);
int get_size() const;
~vectorInt();
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 m)
{
size=n;ptr= new int[n];
for(int i=0;i<size;i++)
ptr[i]=m;
cout<<"constructor vectorInt(int n,int value) called"<<endl;
}
vectorInt::vectorInt(const vectorInt & vc)
{
size=vc.size;
ptr=new int[size];
for(int i = 0; i < size; ++i)
ptr[i] = vc.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
点击查看代码
#include "vectorInt.hpp"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void output(const vectorInt &vi) {
for(int 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(int i = 0; i < n; ++i)
x1.at(i) = i*i;
cout << "vectorInt对象x1: ";
output(x1);
vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42
cout << "vectorInt对象x2: ";
output(x2);
vectorInt x3(x2); // 使用x2构造x3
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();
}
任务六
Matrix.hpp
点击查看代码
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <cassert>
using namespace std;
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 *p;
};
Matrix::Matrix(int n) {
lines=n;
cols=n;
p=new double[n*n];
}
Matrix::Matrix(int n,int m) {
lines=n;
cols=m;
p=new double[n*m];
}
Matrix::Matrix(const Matrix &X) {
lines=X.lines;
cols=X.cols;
p=new double[lines*cols];
for(int i=0; i<lines*cols; i++) {
p[i]=X.p[i];
}
}
Matrix::~Matrix() {
delete[] p;
}
void Matrix::set(const double *pvalue) {
for(int i=0; i<lines*cols; i++) {
p[i]=pvalue[i];
}
}
void Matrix::set(int i, int j, double value) {
p[cols*i+j]=value;
}
double & Matrix::at(int i, int j) {
assert(i*cols+j<(lines*cols)&&i*cols+j>=0);
return p[cols*i+j];
}
double &Matrix::at(int i, int j) const {
return p[cols*i+j];
}
int Matrix::get_lines() const {
return lines;
}
int Matrix::get_cols() const {
return cols;
}
void Matrix::print() const {
int i,j;
for(i=0; i<lines; i++) {
for(j=0; j<cols-1; j++) {
cout<<p[i*cols+j]<<",";
}
cout<<p[i*cols+j];
cout<<endl;
}
}
#endif
task6.cpp
点击查看代码
#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] = {2,4, 5, 7, 8, 9};
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);
cout << "矩阵对象m3:" << endl;
m3.print();
cout << endl;
Matrix m4(2);
m4.set(x);
cout << "矩阵对象m4:" << endl;
m4.print();
}
int main() {
test();
}