Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- class K2;
- class K1{
- string *p1;
- public:
- K1(): p1(new string("---")){}
- K1(const string& a1): p1(new string(a1)){}
- //K1(const K1& r): p1(new string(*r.p1)){} //konstruktor kopiujący
- K1& operator =(const K1& r){
- if(this != &r){
- *p1 = *r.p1;
- }
- return *this;
- }
- string& pp(){return *p1;};
- const string& pp() const{return *p1;}
- friend ostream& operator <<(ostream&, const K1&);
- //friend K2;
- ~K1(){delete p1;}
- };
- class K2{
- K1 txt[2];
- int w1;
- public:
- K2(): w1(0){}
- K2(const string& a1, const string& a2, const int& a3): w1(a3){
- txt[0] = a1;
- txt[1] = a2;}
- K2(const K2& r): w1(r.w1){
- txt[0] = r.txt[0];
- txt[1] = r.txt[1];
- }
- /*K2& operator=(const K2& r){ //przeciazenie operatora do wskaznikow
- if(this != &r){
- txt[0] = r.txt[0];
- txt[1] = r.txt[1];
- w1 = r.w1;
- }
- return *this;
- }*/
- friend ostream& operator <<(ostream&, const K2&);
- friend K2 operator+(const K2&, const string&);
- friend K2 operator+(const string&, const K2&);
- friend istream& operator >>(istream&, K2&);
- /*const K1& operator[](size_t i)const { //indeksowanie
- return txt[i];
- }
- K1& operator[](size_t i){
- return txt[i];
- }*/
- K2 operator+(const int& l){
- return K2(txt[0].pp(), txt[1].pp(), (w1+l));
- }
- K2& operator+=(const int& l){
- w1 += l;
- return *this;
- }
- };
- ostream& operator <<(ostream& out, const K1& r){return out << *r.p1;}
- ostream& operator <<(ostream& out, const K2& r){
- return out << r.txt[0] << ' ' << r.txt[1] << ' ' << r.w1 << endl;
- }
- istream& operator >>(istream& in, K2& r){
- if(!in) throw string("Blad na strumieniu");
- if(!(in >> r.txt[0].pp())) throw string("blad wczytywania");
- if(!(in >> r.txt[1].pp())) throw string("blad wczytywania");
- if(!(in >> r.w1)) throw string("blad wczytywania");
- if(!in) throw string("Blad na strumieniu");
- return in;
- }
- K2 operator+(const K2& l, const string& p){
- return K2(l.txt[0].pp(), (l.txt[1].pp() + p), l.w1);
- }
- K2 operator+(const string& l, const K2& p){
- return K2(p.txt[0].pp(), (l + p.txt[1].pp()), p.w1);
- }
- int main(){
- K2 ob1, ob2;
- const K2* wsk1 = new K2("Ala", "Koala", 15);
- const K2 ob3(*wsk1);
- delete wsk1;
- wsk1 = 0;
- const K2 * wsk2 = new K2(ob3);
- ob2 = *wsk2;
- cout << ob1 << *wsk2;
- delete wsk2;
- wsk2 = 0;
- cout << ob2;
- cout << ob2+10;
- cout << "\n**** 3 ****\n"<< endl;
- K2 tab[4];
- ifstream plik("data.txt");
- try{
- for(int i = 0; i < 4; i++){
- plik >> tab[i];
- }
- }
- catch(string err){cerr << err;}
- catch(...){cout << "blad nieznany";}
- plik.close();
- for(int i = 0; i < 4; i++){
- tab[i] += 1;
- cout << tab[i];
- }
- cout << "\n**** 4 ****\n"<< endl;
- tab[1] = tab[1] + " Kowalska";
- tab[3] = "Bocian " + tab[3];
- for(int i = 0; i < 4; i++){
- cout << tab[i];
- }
- cout << "\n**** 5 ****\n"<< endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement