Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################Array2D.h#####################3
- --------------------------
- #pragma once
- #include <stdexcept>
- namespace collections {
- template <typename T>
- class Array2D {
- protected:
- T* array;
- int w, k; //1.
- public:
- Array2D(int w, int k) : array(new T[w*k]), w(w), k(k) {} //2.a
- Array2D(int w, int k, const T& value) : w(w), k(k) {
- array = new T[w*k];
- for (int i = 0; i < w*k; i++) { array[i] = value; }
- } //2.b <-- koniec
- //konstruktor kopiuj¹cy:
- Array2D& operator=(const Array2D& copy) {
- if (this != ©) {
- w = copy.w; //przypisanie wartosci do kopii
- k = copy.k;
- delete[] array; //usuwanie starej
- array = new T[w*k]; //tworzenie nowej tablicy
- for (int i = 0; i < w*k; ++i) { array[i] = copy.array[i]; } //wypelnienie nowej wartosciami kopii - array1 -> kopia, this -> nowa
- return *this; //zwraca now¹ tablice
- }
- else
- return *this;
- }
- Array2D<T>(const Array2D& copy) : w(w), k(k) { //g³êboka kopia
- this->array = new T[w*k];
- for (int i = 0; i < copy.w*copy.k; ++i) {
- this->array[i] = copy.array[i];
- }
- }
- T* operator[](const int copy) const {
- return &array[w*copy];
- }
- T& at(int rn, int cn) {
- if (rn >= w || rn < 0 || cn >= k || cn < 0) {
- throw std::runtime_error("poza zakresem");
- }
- return array[rn*w + cn];
- }
- ~Array2D() {
- delete[] array;
- }
- };
- }
- --------------------
- ####################Matrix.h#####################
- #pragma once
- #include "Array2D.h"
- #include"ITextRepresentable.h"
- #include <sstream>
- namespace collections {
- class Matrix : public Array2D<double>, public ITextRepresentable {
- public:
- Matrix(int w, int k) : Array2D(w,k) {} //2.a
- Matrix(int w, int k, const double value) :Array2D(w,k,value) {}
- std::string asText() const override {
- std::ostringstream display;
- for (int i = 0; i < k; ++i) {
- for (int j = 0; j < w; ++j) {
- display << array[i*w + j] << " ";
- }
- display << std::endl;
- }
- return display.str();
- }
- };
- }
- ---------------------------------
- #####################ITextRepresentable#################
- -------------------------
- #pragma once
- #include <string>
- class ITextRepresentable {
- public:
- virtual std::string asText() const = 0;
- virtual ~ITextRepresentable() {
- }
- };
- inline std::ostream& operator<<(std::ostream& os, const ITextRepresentable& rep) {
- os << rep.asText();
- return os;
- }
- --------------------------
- ####################source.cpp#########################
- --------------------
- #include "Array2D.h"
- #include "Matrix.h"
- #include "ITextRepresentable.h"
- #include <iostream>
- #include <cstdlib>
- int main() {
- collections::Array2D<int> test(2,4);
- collections::Array2D<int> test2(3, 2, 6);
- collections::Array2D<int> test3(test);
- test3 = test;
- collections::Matrix test5(2, 3);
- collections::Matrix test6(3, 2, 4.5);
- test6.at(1,2) = 33;
- std::cout << test6;
- std::cout << std::endl;
- //std::cout << test;
- //std::cout << std::endl;
- /*test[1][1]= 1;
- test[0][0] = 0;
- test[10][30] = 69;
- std::cout << test;
- std::cout << std::endl;*/
- system("pause");
- return 0;
- }-------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement