Кристинке
.docКристин, ты извини, но проверить я не могу, т.к. Visual C у меня нет, поэтому если какие то ошибки, то ты мне скрином кидай, буду пытаться поправить, там могут быть описки, т.к. набирала я с клавы. У меня просто именно сомнения на счет класса class MatrixBase. Ну если что не так, то извини.
3. Создать иерархию классов представляющих простое наследование. Базовый класс – квадратная матрица (динамическая (int **mt или int* или int *mt[константа])). Производный класс – методы, работающие с данными базового класса. Реализовать в производном классе метод – траспонирования матрицы относительно главной диагонали.
Код программы:
MatrixBase.h
#ifndef MATRIXBASE
#define MATRIXBASE
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <time.h>
using namespace std;
class MatrixBase //Базовый класс
{
public:
MatrixBase():colsCount(0), rowsCount(0), a(0){}
MatrixBase(int, int, bool=true);
MatrixBase(const MatrixBase &);
MatrixBase & RandomFilling(int=1, int=100);
~MatrixBase();
void PrintMatrix() const;
int ** Get() const;
void Set(const int**, int, int);
protected:
int colsCount,rowsCount;
int **a;
int Random(int=1, int=100) const;
};
#endif
MatrixBase.cpp
#include “MatrixBase.h”
MatrixBase::MatrixBase(int rows, int cols, bool fillRand)
{
colsCount = cols;
rowsCount = rows;
a = new int * [rowsCount];
for(int i=0; i<colsCount; a[i++] = new int [colsCount]);
if(fillRand) RandomFilling();
}
MatrixBase::MatrixBase(const MatrixBase & src)
{
rowsCount = src.rowsCount;
colsCount = src.colsCount;
a = new int * [rowsCount];
for(int i=0; i<colsCount; a[i++] = new int [colsCount]);
for(int i=0; i<rowsCount; ++i)
for(int j=0; j<colsCount; ++j) a[i][j] = src.a[i][j];
}
MatrixBase::~MatrixBase()
{
for(int i=0; i<rowsCount; delete [] a[i++]);
delete [] a;
return *this;
}
void MatrixBase::PrintMatrix() const
{
for(int i=0; i<rowsCount; ++i, cout << endl)
for(int j=0; j<colsCount; cout << a[i][j++] << "\t");
cout << endl;
}
int ** MatrixBase::Get() const
{
return a;
}
void MatrixBase::Set(const int ** arr, int NewColsCount, int NewRowsCount)
{
for(int i=0; i<this->rowsCount; delete [] a[i++]);
delete [] a;
colsCount = NewColsCount;
rowsCount = NewRowsCount;
a = new int * [rowsCount];
for(int i=0; i<colsCount; a[i++] = new int [colsCount]);
for(int i=0; i<rowsCount; ++i)
for(int j=0; j<colsCount; ++j) a[i][j] = arr[i][j];
}
MatrixExt.h
#include “MatrixBase.h”
#ifndef MATRIXEXT
#define MATRIXEXT
Class MatrixExt: public MatrixBase //производный класс
{
public:
MatrixExt():MatrixBase(){}
MatrixExt(int rows, int cols, bool fillRand=true):MatrixBase(rows,cols,fillRand) {}
MatrixExt(const MatrixBase & src):MatrixBase(src){}
MatrixExt & SpawCols(int, int);
};
#endif
MatrixExt.cpp
#include “MatrixExt.h”
int n, j, n1, d;
float a, sum;
{
for(int j=0; j<colsCount;j++)
}
Cout<<”Transponirovannaya matrica”<<endl;
{
int t=a[j][i]; //транспонирование матрицы относительно главной диагонали
a[j][i]=a[i][j];
a[i][j]=t; }
return *this;}
main.cpp // печать матрицы
#include “MatrixExt.h”
int main()
{
MatrixExt a(5,5);
a.PrintMatrix();
return 0;
}