Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- using namespace std;
- template <class T>
- class Para {
- public:
- T x, y;
- Para<T> & operator = (Para<T> &o1) {
- x = o1.x;
- y = o1.y;
- return *this;
- }
- Para<T> & operator + (Para<T> &o2) {
- static Para<T> wynik;
- wynik.x = this->x + o2.x;
- wynik.y = this->y + o2.y;
- return wynik;
- }
- };
- class Macierze {
- friend ostream &operator << (ostream &s1, Macierze &o1);
- friend istream &operator >> (istream &s1, Macierze &o1);
- double *wsk;
- int wymiar;
- public:
- Macierze() { //konstruktor
- wsk = NULL; //wartość niekoniecznie równa 0
- wymiar = 0;
- };
- ~Macierze(){ //destruktor
- if (wsk != NULL)
- delete [] wsk;
- wsk = NULL;
- wymiar = 0;
- };
- Macierze & operator = (const Macierze &o1) {
- if (this == &o1) {
- return *this;
- }
- //zwolnić obszar
- if (this->wsk != NULL)
- delete [] this->wsk;
- //zarezerwować nowy obszar
- this->wsk = new double [o1.wymiar*o1.wymiar];
- //elementy przepisać
- this->wymiar = o1.wymiar;
- for (int x=0; x < this->wymiar*this->wymiar; x++) {
- this->wsk[x] = o1.wsk[x];
- }
- return *this; // wskaźnik do obiektu na którym operujemy
- };
- Macierze (Macierze &o1) {
- this->wsk = new double [o1.wymiar*o1.wymiar];
- //elementy przepisać
- this->wymiar = o1.wymiar;
- for (int x=0; x < this->wymiar*this->wymiar; x++) {
- this->wsk[x] = o1.wsk[x];
- }
- };
- Macierze operator + (Macierze &o2) {
- Macierze wynik;
- wynik.wymiar = o2.wymiar;
- wynik.wsk = new double [wynik.wymiar*wynik.wymiar];
- for (int x=0; x < o2.wymiar*o2.wymiar; x++) {
- wynik.wsk[x] = this->wsk[x] + o2.wsk[x];
- }
- return wynik;
- };
- };
- template <class T>
- istream &operator >> (istream &s1, Para<T> &o1) {
- s1 >> o1.x >> o1.y;
- return s1;
- }
- template <class T>
- ostream &operator << (ostream &s1, Para<T> &o1) {
- s1 << o1.x << " " << o1.y;
- return s1;
- }
- istream &operator >> (istream &s1, Macierze &o1) {
- if (o1.wsk != NULL) //zajęte, więc trzeba zwolnić
- delete [] o1.wsk;
- s1 >> o1.wymiar; //stream size
- if (o1.wymiar < 1) {
- o1.wsk = NULL;
- o1.wymiar = 0;
- return s1;
- }
- o1.wsk = new double [o1.wymiar*o1.wymiar]; //create new array
- for (int x=0; x < o1.wymiar*o1.wymiar; x++) {
- s1 >> o1.wsk[x]; //stream variables
- }
- return s1;
- }
- ostream &operator << (ostream &s1, Macierze &o1) {
- if (o1.wsk != NULL) {
- for (int x=0; x < o1.wymiar*o1.wymiar; x++) {
- if (x%o1.wymiar == 0)
- s1 << endl; //end line
- s1 << o1.wsk[x]; //out
- }
- }
- return s1;
- }
- int main(){
- Para<int> a, b, c;
- cin >> a >> b;
- c=a+b;
- cout << c;
- Para<Macierze> d, e, f;
- cin >> d >> e;
- f=d+e;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement