#include <iostream>
using namespace std;
#include <string>
#include <ctime>
void zwloka(int sekund);
class ekran_alfanum
{
string tresc;
char tlo_ekranu;
enum{ wys_ekranu = 24, szer_ekranu = 63};
public:
ekran_alfanum() : tlo_ekranu(' ')
{
tresc.resize(wys_ekranu * szer_ekranu, tlo_ekranu);
}
void zamaluj_obszar(int lewy, int gora, int prawy, int dol, char tlo_okna);
void wyswietl()
{
cout << tresc << flush;
}
void wymaz_wszystko()
{
zamaluj_obszar(0,0, szer_ekranu, wys_ekranu, tlo_ekranu);
}
void napisz(int kolumna, int rzad, string tekst)
{
tresc.replace(szer_ekranu * rzad + kolumna, tekst.length(), tekst);
}
};
void ekran_alfanum::zamaluj_obszar( int lewy, int gora, int prawy, int dol, char tlo_okna)
{
int dlugosc = prawy - lewy;
for(int y = gora; y < dol; y++)
{
tresc.replace(y * szer_ekranu + lewy, dlugosc, dlugosc, tlo_okna);
}
for(int i = 0; i < wys_ekranu; i++)
{
tresc[(i +1) * szer_ekranu - 1] = '\n';
}
}
ekran_alfanum monitor;
class okno;
class wizerunek_ekranu
{
okno * tab_okn[20];
int ile_okien;
public:
//konstruktor
wizerunek_ekranu() : ile_okien(0){}
void odswiez();
void mazanie();
void operator += (okno & ref_ok);
wizerunek_ekranu & operator+(okno & ref_ok);
void operator -= (okno & ref_ok);
void operator != (okno & ref_ok);
};
class okno
{
int x, y, wys, szer;
char kolor;
string tytul;
public:
okno(int xx, int yy, int ss, int ww, char kk, string tt)
: x(xx), y(yy), wys(ww), szer(ss), kolor(kk), tytul(tt) {}
wizerunek_ekranu operator + (okno & m2);
void narysuj_sie();
};
void okno::narysuj_sie()
{
monitor.zamaluj_obszar(x, y, x + szer, y + wys, kolor);
string linijka( szer, '-');
monitor.napisz(x, y, linijka);
monitor.napisz(x, y + wys, linijka);
for(int i = 0; i < wys+1; i++ )
{
monitor.napisz(x, y + i, "|");
monitor.napisz(x + szer, y+i, "|");
}
monitor.napisz(x +(szer - (tytul.size())) / 2, y, tytul);
monitor.wyswietl();
}
wizerunek_ekranu okno::operator+( okno & m2)
{
wizerunek_ekranu roboczy;
roboczy += *this;
roboczy += m2;
return roboczy;
}
void wizerunek_ekranu::operator += (okno & ref_ok)
{
tab_okn[ ile_okien++] = &ref_ok;
odswiez();
}
void wizerunek_ekranu::operator-=(okno & ref_okna)
{
int i = 0;
for( ; i < ile_okien; i++)
{
if(tab_okn[i] == &ref_okna) break;
}
if( i == ile_okien)
{
}else
{
for(int k = i; k < ile_okien ; k++)
{
tab_okn[k] = tab_okn[k +1];
}
ile_okien--;
}
mazanie();
odswiez();
}
void wizerunek_ekranu::operator!=(okno & ref_okna)
{
*this -= ref_okna;
*this += ref_okna;
odswiez();
}
void wizerunek_ekranu::odswiez()
{
for(int i = 0; i < ile_okien; i++)
{
tab_okn[i]-> narysuj_sie();
}
}
void wizerunek_ekranu::mazanie()
{
monitor.wymaz_wszystko();
}
wizerunek_ekranu & wizerunek_ekranu::operator + (okno & ref_okna)
{
*this += ref_okna;
return *this;
}
int main()
{
wizerunek_ekranu pulpit;
okno gra(15, 7, 21, 6, '$', "Gra w Chinczyka");
okno kalkulator(2, 3, 14, 6, '@', "Kalkulator");
okno edytor(7, 5, 18, 6, ':', "Edytor");
okno pol(3, 8, 22, 4, '%', "Pokax");
pulpit = kalkulator + gra + edytor ;//+ edytor;
zwloka(1);
okno zegar(4, 9, 18, 6, '%', "Zegar");
pulpit += zegar;
/*
zwloka(1);
pulpit != kalkulator;
zwloka(1);
pulpit != gra;
zwloka(1);
pulpit -= kalkulator;
zwloka(1);*/
return 0;
}
void zwloka(int sekund)
{
static time_t poprzedni_czas = time(NULL);
while(time(NULL) - poprzedni_czas < sekund);
poprzedni_czas = time(NULL);
}
Tutaj mam cały przykład z symfonii c++, nie potrafię zrozumieć jednej kwestii:
wizerunek_ekranu & operator+(okno & ref_ok);
Po co jest tutaj ta referencja do operatora. Dziękuję za wyjaśnienie.