Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //szablony klas - _po.kolokwium.4.gr1.pdf
- #include <iostream>
- using namespace std;
- template <class T, int size>
- class Test{
- T a[size];
- public:
- Test(float x=0){
- for(int i=0; i<size; i++)
- a[i]=(float)x;
- }
- void pisz(){
- for(int i=0; i<size; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- Test operator++(int){
- Test temp(*this);
- for(int i=0; i<size; i++)
- temp.a[i]++;
- return temp;
- }
- Test operator+(Test & A){
- Test temp(*this);
- for(int i=0; i<size; i++)
- temp.a[i]=a[i]+A.a[i];
- return temp;
- }
- Test operator-=(Test & A){
- for(int i=0; i<size; i++)
- a[i]-=A.a[i];
- return *this;
- }
- };
- class Punkt{
- float x,y;
- public:
- Punkt(int a=0, int b=0){
- x=(float)a;
- y=(float)b;
- }
- friend ostream & operator << (ostream & wyjscie, Punkt & A);
- void pisz(){
- cout << *this << endl;
- }
- Punkt operator++(int){
- Punkt temp(*this);
- temp.x++;
- temp.y++;
- return temp;
- }
- Punkt operator+(Punkt & A){
- Punkt temp(*this);
- temp.x=x+A.x;
- temp.y=y+A.y;
- return temp;
- }
- };
- ostream & operator << (ostream & wyjscie, Punkt & A){
- wyjscie << "x="<< A.x << " y=" << A.y << ",";
- return wyjscie;
- }
- int main(){
- Test<float,12> A1(10), B1(A1); // 10 - wartość wypełnienia tablicy
- cout << "A1: "; A1.pisz();
- cout << "B1: "; B1.pisz();
- Test<Punkt,5> A2(12),B2(A2); // 12 - wartość wypełnienia tablicy
- cout << "A2: "; A2.pisz();
- cout << "B2: "; B2.pisz();
- B2 = (A2++) + B2; cout << "B2=(A2++)+B2: "; B2.pisz();
- B1 -= A1 + B1; cout << "B1-=A1+B1: "; B1.pisz();
- system("pause");
- }
Add Comment
Please, Sign In to add comment