#ifndef VECTOR_INT
#define VECTOR_INT
#include<iostream>
#include<cassert>
using namespace std;
class Vector_int {
public:
Vector_int(int n);
Vector_int(int n, int m);
Vector_int(const Vector_int& x);
~Vector_int();
int& at(int i);
void show() const;
private:
int size;
int* p;
};
Vector_int::Vector_int(int n) : size(n) {
cout << "Default constructor called." << endl;
p = new int[n];
for(int i=0;i<n;i++)
p[i]=0;
}
Vector_int::Vector_int(int n,int m) : size(m) {
cout << "constructor called." << endl;
p = new int[n];
for (int i = 0; i < m; i++) {
p[i]=m;
}
}
Vector_int::Vector_int(const Vector_int& x) : size(x.size) {
cout << "copy constructor called." << endl;
p = new int[size];
for (int i = 0; i < size; i++)
p[i] = x.p[i];
}
Vector_int::~Vector_int()
{
cout<<"deleting"<<endl;
delete[] p;
}
int &Vector_int::at(int i) {
assert( i>= 0 && i < size);
return p[i];
}
void Vector_int::show() const {
for (int i = 0; i < size; i++) {
cout << p[i] <<endl;
}
}
#endif
#include<iostream>
#include"vector_int.hpp"
using namespace std;
int main()
{
int n;
cin >> n;
Vector_int x1(n);
x1.show();
Vector_int x2(n, 6);
x2.show();
Vector_int y(x2);
y.show();
y.at(0) = 999;
y.show();
return 0;
}
![]()
#include<iostream>
#pragma once
#ifndef TEXTCODER_HPP
#define TEXTCODER_HPP
using namespace std;
class Matrix
{
public:
Matrix(int n);
Matrix(int n, int m);
Matrix(const Matrix& x);
~Matrix();
void set(const double* pvalue);
void set(int i, int j, int value);
double& at(int i, int j);
double at(int i, int j)const;
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];
int length = lines * cols;
for (int i = 0; i < length; ++i)
p[i] = x.p[i];
}
Matrix::~Matrix()
{
delete[] p;
}
void Matrix::set(const double* pvalue)
{
int length = lines * cols;
for (int i = 0; i < length; ++i)
p[i] = pvalue[i];
}
void Matrix::set(int i, int j, int value)
{
p[(i + 1) * (j + 1) - 1] = value;
}
double& Matrix::at(int i, int j)
{
return p[(i + 1) * (j + 1) - 1];
}
double Matrix::at(int i, int j)const
{
return p[(i + 1) * (j + 1) - 1];
}
int Matrix::get_lines()const
{
return lines;
}
int Matrix::get_cols()const
{
return cols;
}
void Matrix::print()const
{
int t = -1;
for (int i = 1; i <= lines; i++)
{
for (int j = 1; j <= cols; j++)
cout << p[++t] << " ";
cout << "\n";
}
}
#endif
int main()
{
double x[] = { 1,2,3,4,5,6,7,8 };
Matrix m1(4, 2);
m1.set(x);
m1.print();
cout << "the first line is:" << endl;
cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
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);
m3.set(0, 0, 999);
m3.print();
}
![]()