实验3 类和对象Ⅱ
实验任务4
vector_int.hpp
#ifndef _VECTOR_INT_HPP_
#define _VECTOR_INT_HPP_
#include <iostream>
#include <cassert>
using namespace std;
class Vector_int
{
public:
Vector_int(int s, int v = 0);
Vector_int(const Vector_int &x);
int& at(int p);
~Vector_int();
void print();
private:
int *base, size, value;
};
Vector_int::Vector_int(int s, int v/*= 0*/) : size{s}, value{v}
{
base = new int[size];
for(int i = 0; i < size; i++)
base[i] = value;
cout << "Constructor called." << endl;
}
Vector_int::Vector_int(const Vector_int &x)
{
size = x.size;
value = x.value;
base = new int[size];
for(int i = 0; i < size; i++)
base[i] = x.base[i];
cout << "Copy constructor called." << endl;
}
inline int& Vector_int::at(int p)
{
assert(p >= 0 && p < size);
return base[p];
}
Vector_int::~Vector_int()
{
delete[] base;
cout << "Destructor called." << endl;
}
void Vector_int::print()
{
for(int i = 0; i < size; i++)
cout << base[i] << " ";
cout << endl;
}
#endif
task4.cpp
#include <iostream>
#include "vector_int.hpp"
using namespace std;
int main()
{
Vector_int v1(10);
Vector_int v2(15, 1);
v1.at(4) = 9;
cout << v2.at(6) << endl;
Vector_int v3(v1);
v3.at(3) = -1;
cout << "v1:" << endl;
v1.print();
cout << "v2:" << endl;
v2.print();
cout << "v3:" << endl;
v3.print();
return 0;
}
运行结果
实验任务5
Matrix.hpp
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <cassert>
using namespace std;
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(int n) : lines(n), cols(n)
{
p = new double[lines * cols];
}
Matrix::Matrix(int n, int m) : lines(n), cols(m)
{
p = new double[lines * cols];
}
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, int value)
{
assert(i >= lines || j >= cols || i < 0 || j < 1);
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];
if(j < cols - 1)
cout << ", ";
}
cout << endl;
}
}
// 类Matrix的实现:待补足
// ×××
#endif
task5.cpp
#include <iostream>
#include "matrix.hpp"
int main()
{
using namespace std;
double x[] = {11, 21, 13, 54, 35, 62, 11, 22, 33};
Matrix m1(3, 3);
m1.set(x);
m1.print();
cout << "the first line is: " << endl;
cout << m1.at(0, 0) << " " << m1.at(0, 1) << m1.at(0, 2) << " " << endl;
cout << endl;
double x2[] = {11, 21, 13, 54, 35, 62};
Matrix m2(2, 3);
m2.set(x2);
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);
m3.set(0, 1, 8848);
m3.print();
}
运行截图
实验总结
以返回引用的形式既可以输出数值,也可以用等号赋值的形式修改数据