一、实验目的
- 会正确定义和使用简单的类模板
- 能够说明指针、引用的联系和区别,能根据问题场景灵活、正确使用指针作为函数参数、引用作为函数参数
- 知道什么是深复制、浅复制,能根据问题场景合理编码实现深复制
- 会正确使用new和delete运算符
- 针对真实、具体问题场景,能正确、合理抽象问题,正确、灵活设计、定义类,并组合使用c++标准库编程求解
二、实验内容
实验任务5
程序代码
vectorInt.hpp
#pragma once
#include<iostream>
using std::cout;
using std::endl;
class vectorInt
{
private:
int* num;
int size;
public:
vectorInt(int s);
vectorInt(int s0, int val);
vectorInt(const vectorInt &t);
~vectorInt();
int &at(int index);
int get_size();
friend void output(vectorInt &p);
};
vectorInt::vectorInt(int s0): size{s0}
{
num = new int[s0];
cout << "vectorInt1 called" << endl;
}
vectorInt::vectorInt(int s0, int val): size{s0}
{
cout << "vectorInt2 called" << endl;
num = new int[s0];
size = s0;
for(int i = 0; i < s0; i++ )
{
num[i] = val;
}
}
vectorInt::vectorInt(const vectorInt &t): size{t.size}
{
num =new int[size];
for(auto i = 0; i < size; ++i)
num[i] = t.num[i];
}
vectorInt::~vectorInt()
{
cout << "~vectorInt called" << endl;
delete[] num;
}
int &vectorInt::at(int index)
{
// assert(index >= 0 && index < size);
return num[index];
}
int vectorInt::get_size(){return size;}
void output(vectorInt &p)
{
int i ;
for( i = 0; i < p.get_size() - 1; i++ )
{
cout << p.num[i] << ", ";
}
cout << p.num[i] << endl;
}
task4-5.cpp
#include <iostream>
#include "vectorInt.hpp"
void test() {
using namespace std;
int n;
cin >> n;
vectorInt x1(n);
for(auto i = 0; i < n; ++i)
x1.at(i) = i*i;
output(x1);
vectorInt x2(n, 42);
vectorInt x3(x2);
output(x2);
output(x3);
x2.at(0) = 77;
output(x2);
output(x3);
}
int main() {
test();
}
结果截图
实验任务6
程序代码
Matrix.hpp
#pragma once
#include <iostream>
using std::cout;
using std::endl;
class Matrix {
public:
Matrix(int n); // 构造函数,构造一个n*n的矩阵
Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵
Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造
~Matrix(); //析构函数
void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值
void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
double &at(int i, int j); //返回矩阵第i行第j列元素的引用
double at(int i, int j) const; // 返回矩阵第i行第j列元素的值
int get_lines() const; //返回矩阵行数
int get_cols() const; //返回矩列数
void print() const; // 按行打印输出矩阵
private:
int lines; // 矩阵行数
int cols; // 矩阵列数
double *p; // 指向存放矩阵数据的内存块的首地址
};
// 类Matrix的实现:待补足
// ×××
Matrix::Matrix(int n): lines{n}, cols{n}
{
cout << "Matrix1 called\n";
p = new double[n * n];
}
Matrix::Matrix(int n, int m): lines{n}, cols{m}
{
cout << "Matrix2 called\n";
p = new double[n * m];
}
Matrix::Matrix(const Matrix &X): lines{X.lines}, cols{X.cols}
{
cout << "Matrix copy called\n";
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()
{
for(int i=0;i<lines*cols;i++)
delete[] p;
}
void Matrix::set(const double *pvalue)
{
for (int i = 0; i < sizeof(pvalue); i++)
{
p[i] = pvalue[i];
}
// int m = 0;
// for(int i = 0; i < lines; i++)
// {
// for (int j = 0; j < cols; j++)
// {
// p[i * lines + j] = *(pvalue + m);
// m++;
// if (m == sizeof(pvalue)/sizeof(double))
// {
// break;
// }
// }
// }
}
void Matrix::set(int i, int j, int value)
{
p[i * cols + j] = value;
}
double &Matrix::at(int i, int j)
{
return p[i * cols + j];
}
double Matrix::at(int i, int j) const
{
return p[i * cols + 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 << p[i * cols + j] << ", ";
}
cout << endl;
}
}
task4-6.cpp
#include <iostream>
#include "matrix.hpp"
void test() {
using namespace std;
double x[] = {1, 2, 3, 4, 5, 6};
Matrix m1(3, 2); // 创建一个3×2的矩阵
m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
m1.print(); // 打印矩阵m1的值
cout << "the first line is: " << endl;
cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值
cout << endl;
Matrix m2(2, 3);
m2.set(x);
m2.print();
cout << "the first line is: " << endl;
cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
cout << endl;
Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999
m3.print();
}
int main() {
test();
}
结果截图
实验任务1
程序代码
point.hpp
#pragma once
#include <iostream>
using std::cout;
using std::endl;
class Point {
public:
Point(): x{0}, y{0} {}
Point(int x0, int y0): x{x0}, y{y0} {}
~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;
};
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;
}
task4-1.cpp
#include <iostream>
#include "point.hpp"
#include <vector>
using std::vector;
using std::cin;
// 测试动态数组类接口
void test1() {
int n;
cin >> n;
vector<Point> x(n); // 创建一个vector<Point>对象,指定包含n个元素
x.at(0).show();
x.at(0).move(30, 50);
x.at(0).show();
}
// 测试复制构造
void test2() {
int n;
cin >> n;
vector<Point> x(n);
x.at(0).show();
vector<Point> y(x); // 基于vector<Point>对象x构建对象y
y.at(0).show();
x.at(0).move(30, 50);
x.at(0).show();
y.at(0).show();
}
int main() {
cout << "测试动态数组类接口...\n";
test1();
cout << "测试复制构造...\n";
test2();
}
结果截图
实验任务2
程序代码
point.hpp
#pragma once
#include <iostream>
using std::cout;
using std::endl;
class Point {
public:
Point(): x{0}, y{0} { cout << "Point: default constructor called.\n"; }
Point(int x0, int y0): x{x0}, y{y0} { cout << "Point: constructor called.\n"; }
~Point() { cout << "Point: destructor called.\n"; }
int get_x() const;
int get_y() const;
void show() const;
void move(int new_x, int new_y);
private:
int x, y;
};
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();
Point &at(int index); // 返回下标为index的元素引用
private:
int size; // 动态数组的大小
Point *p;
};
vectorPoint::vectorPoint(int n) : size{n} {
cout << "dynamic create array..." << endl;
p = new Point[n];
}
vectorPoint::~vectorPoint() {
cout << "dynamic delete array..." << endl;
delete[] p;
}
Point &vectorPoint::at(int index) {
assert(index >= 0 && index < size); // 宏,通常测试用。如果不满足条件,终止程序
return p[index];
}
task4-2.cpp
#include <iostream>
#include <cstdlib>
#include "point.hpp"
#include "vectorPoint.hpp"
using std::cin;
// 测试动态数组类接口
void test1() {
int n;
cin >> n;
vectorPoint x(n); // 创建一个vectorPoint对象,指定包含n个元素
x.at(0).show();
x.at(0).move(30, 50);
x.at(0).show();
}
// 测试复制构造
void test2() {
int n;
cin >> n;
vectorPoint x(n);
x.at(0).show();
vectorPoint y(x); // 基于vectorPoint对象x构建对象y
y.at(0).show();
x.at(0).move(30, 50);
x.at(0).show();
y.at(0).show();
}
int main() {
cout << "测试动态数组类接口...\n";
test1();
system("pause");
cout << "测试复制构造...\n";
test2();
}
结果截图
暂无
实验任务3
程序代码
task4-3-1.cpp
#include <iostream>
// 函数声明
void swap1(int &rx, int &ry); // 引用作为形参
void swap2(int *px, int *py); // 指针作为形参
void print(int x, int y); // 普通变量作为形参
int main() {
using namespace std;
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);
}
// 函数定义
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-3-2.cpp
#include <typeinfo>
#include <iostream>
int main() {
using namespace std;
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 = " << a << 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-3-3.cpp
#include <iostream>
#include <vector>
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;
}
int main() {
using namespace std;
vector<int> x{1,2,3,4,5};
vector<int> y{x};
output(x);
square1(x);
output(x);
output(y);
square2(y);
output(y);
}
结果截图
1.
2.
3.
三、实验小结
标签:std,cout,int,void,实验,vectorInt,include From: https://www.cnblogs.com/civilwen/p/16859126.html本次实验我深刻理解了深复制与浅复制,并且实现了vector动态数组,有了很多收获。