2022.11.02 OOP实验课
实验4 类与数组、指针
任务5
代码:
vectorInt54.hpp
#pragma once
#include <iostream>
#include <cassert>
#include <iomanip>
using namespace std;
class vectorInt
{
private:
int size;
int *n;
public:
vectorInt(int num);
vectorInt(int num, int value);
vectorInt(const vectorInt& obj);
~vectorInt();
int &at( int i){
assert( i >= 0 && i < size);//宏,通常测试用。如果步满足条件,终止程序
return n[i];
}
int get_size(){ return size; }
friend void output( vectorInt &x);
};
vectorInt:: vectorInt(int num): size{num}{
n = new int[size];
cout << "1 para constructor called." << endl;
}
vectorInt:: vectorInt(int num, int value): size{num}{
n = new int[size];
for(int i = 0; i < num; ++i){
n[i] = value;
}
cout << "2 para constructor called." << endl;
}
vectorInt::vectorInt(const vectorInt &obj): size{obj.size}{
n = new int[size];
for(int i = 0; i < size; ++i){
n[i] = obj.n[i];
}
cout << "copy constructor called." << endl;
}
vectorInt::~vectorInt(){
free(n);
cout << "destructor called." << endl;
}
void output(vectorInt &x){
for(int i = 0; i < x.size; ++i){
cout << setw(3) << left << x.n[i];
}
cout << endl;
}
task54.cpp
#include "vectorInt54.hpp"
#include <iostream>
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
代码:
Matrix64.hpp
#pragma once
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
using std::left;
class Matrix{
private:
int lines;//矩阵行数
int cols;//矩阵列数
double** p;//指向存放矩阵数据的内存块首地址
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_line() const; //返回矩阵行数
int get_cols() const; //返回矩阵列数
void print() const; //按行打印输出矩阵
};
Matrix::Matrix(int n): lines{n}, cols{n}{
p = new double*[n];
for(int i = 0; i < n; i++){
p[i] = new double[n];
}
}
Matrix::Matrix(int n, int m): lines{n}, cols{m}{
p = new double*[n];
for(int i = 0; i < n; i++){
p[i] = new double[m];
}
}
Matrix::Matrix(const Matrix &x): lines{x.lines}, cols{x.cols}{
//初始化
p = new double*[x.lines];
for(int i = 0; i < x.lines; i++){
p[i] = new double[x.cols];
}
//复制数值
for(int i = 0; i < x.lines; i++){
for(int j = 0; j < x.cols; j++){
p[i][j] = x.p[i][j];
}
}
}
Matrix::~Matrix(){};
void Matrix::set(const double* pvalue){
int k =0;
for(int i = 0; i < lines; i++){
for(int j = 0; j < cols; j++){
p[i][j] = pvalue[k++];
}
}
}
void Matrix::set(int i, int j, int value){
p[i][j] = value;
}
double& Matrix::at(int i, int j){
return p[i][j];
}
double Matrix::at(int i, int j)const{
return p[i][j];
}
int Matrix::get_line()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 << setw(3) << left << int(p[i][j]);
}
cout << endl;
}
}
task64.cpp
#include "Matrix64.hpp"
#include <iostream>
void test(){
using namespace std;
double x[] = {1,2,3,4,5,6};
Matrix m1(3, 2);//创建一个3x2的矩阵
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();
}
测试: