Guest User

Untitled

a guest
Apr 26th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. //kolokwium - _po.kolokwium.4.gr1.pdf
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template <class T, int size>
  6. class Test{
  7.     T a[size];
  8. public:
  9.     Test(float x=0){
  10.         for(int i=0; i<size; i++)
  11.             a[i]=(float)x;
  12.     }
  13.     void pisz(){
  14.         for(int i=0; i<size; i++)
  15.             cout << a[i] << "  ";
  16.         cout << endl;
  17.     }
  18.     Test operator++(int){
  19.         Test temp(*this);
  20.         for(int i=0; i<size; i++)
  21.             temp.a[i]++;
  22.         return temp;
  23.     }
  24.     Test operator+(Test & A){
  25.         Test temp(*this);
  26.         for(int i=0; i<size; i++)
  27.             temp.a[i]=a[i]+A.a[i];
  28.         return temp;
  29.     }
  30.     Test operator-=(Test & A){
  31.         for(int i=0; i<size; i++)
  32.             a[i]-=A.a[i];
  33.         return *this;
  34.     }
  35. }; 
  36.  
  37. class Punkt{
  38.     float x,y;
  39. public:
  40.     Punkt(int a=0, int b=0){
  41.         x=(float)a;
  42.         y=(float)b;
  43.     }
  44.     friend ostream & operator << (ostream & wyjscie, Punkt & A);
  45.     void pisz(){
  46.         cout << *this << endl;
  47.     }
  48.     Punkt operator++(int){
  49.         Punkt temp(*this);
  50.         temp.x++;
  51.         temp.y++;
  52.         return temp;
  53.     }
  54.     Punkt operator+(Punkt & A){
  55.         Punkt temp(*this);
  56.         temp.x=x+A.x;
  57.         temp.y=y+A.y;
  58.         return temp;
  59.     }
  60.    
  61. };
  62.  
  63. ostream & operator << (ostream & wyjscie, Punkt & A){
  64.     wyjscie << "x="<< A.x << " y=" << A.y << ",";
  65.     return wyjscie;
  66. }
  67. int main(){
  68.     Test<float,12> A1(10), B1(A1); // 10 - wartość wypełnienia tablicy
  69.     cout << "A1:  "; A1.pisz();
  70.     cout << "B1:  "; B1.pisz();
  71.     Test<Punkt,5> A2(12),B2(A2); // 12 - wartość wypełnienia tablicy
  72.     cout << "A2:  "; A2.pisz();
  73.     cout << "B2:  "; B2.pisz();
  74.     B2 = (A2++) + B2;   cout << "B2=(A2++)+B2: "; B2.pisz();
  75.     B1 -= A1 + B1;      cout << "B1-=A1+B1: "; B1.pisz();
  76.  
  77.     system("pause");
  78. }
Add Comment
Please, Sign In to add comment