Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- class K1 {
- friend ostream & operator<<(ostream & stream, const K1 & obj);
- string * p1;
- public:
- K1() : p1(new string("Brak")) {}
- K1(const K1 & obj): p1(new string(*obj.p1)) {}
- K1(const string & str) : p1(new string(str)) {}
- ~K1() { delete p1; }
- K1 & operator=(const K1 & obj);
- string *& getP1() { return p1; }
- const string & getP1() const { return *p1; }
- friend class K2;
- };
- K1 & K1::operator=(const K1 & obj) {
- if (this != &obj) {
- *p1 = *obj.p1;
- }
- return *this;
- }
- ostream & operator<<(ostream & stream, const K1 & obj) {
- return stream << *obj.p1;
- }
- class K2 {
- K1 txt[2];
- int w1;
- public:
- K2() {
- *txt[0].p1 = "Brak";
- *txt[1].p1 = "Brak";
- w1 = 0;
- }
- K2(const string & str1, const string & str2, const int & p) {
- *txt[0].p1 = str1;
- *txt[1].p1 = str2;
- w1 = p;
- }
- K2(const K2 & obj) {
- *txt[0].p1 = *obj.txt[0].p1;
- *txt[1].p1 = *obj.txt[1].p1;
- w1 = obj.w1;
- }
- K2 & operator=(const K2 & obj);
- K2 & operator+=(const int & val);
- friend ostream & operator<<(ostream & stream, const K2 & obj);
- friend istream & operator>>(istream & stream, K2 & obj);
- friend K2 operator+(const K2 & obj, const int & val);
- friend K2 operator+(const K2 & obj, const string & val);
- friend K2 operator+(const string & val, const K2 & obj);
- };
- K2 operator+(const K2 & obj, const int & val) {
- K2 newObj = K2(obj);
- newObj.w1 += val;
- return newObj;
- }
- K2 operator+(const K2 & obj, const string & val) {
- K2 newObj = K2(obj);
- *newObj.txt[1].getP1() += val;
- return newObj;
- }
- K2 operator+(const string & val, const K2 & obj) {
- K2 newObj = K2(obj);
- string temp = val;
- temp += *newObj.txt[1].getP1();
- *newObj.txt[1].getP1() = temp;
- return newObj;
- return newObj;
- }
- K2 operator+(const int & val, const K2 & obj) {
- return obj + val;
- }
- K2 & K2::operator=(const K2 & obj) {
- if (this != &obj) {
- *txt[0].p1 = *obj.txt[0].p1;
- *txt[1].p1 = *obj.txt[1].p1;
- w1 = obj.w1;
- }
- return *this;
- }
- K2 & K2::operator+=(const int & val) {
- w1 += val;
- return *this;
- }
- istream & operator >> (istream & stream, K2 & obj) {
- if (!stream) throw string("Strumien nie jest otwarty lub jest uszkodzony!");
- if (!(stream >> *obj.txt[0].getP1())) throw string("Blad podczas wyprowadzania z strumienia!");
- if (!(stream >> *obj.txt[1].getP1())) throw string("Blad podczas wyprowadzania z strumienia!");
- if (!(stream >> obj.w1)) throw string("Blad podczas wyprowadzania z strumienia!");
- if (!stream) throw string("Strumien jest uszkodzony!");
- return stream;
- }
- ostream & operator<<(ostream & stream, const K2 & obj) {
- return stream << obj.txt[0] << " " << obj.txt[1] << " " << obj.w1 << endl;
- }
- 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 << "*********** 3 *************\n" << endl;
- K2 tab[4];
- ifstream file("data.txt", ios::out);
- if (file) {
- try {
- for (int i = 0; i < 4; i++) {
- file >> tab[i];
- }
- } catch (string err) {
- cout << err << endl;
- return 1;
- }
- }
- else {
- cerr << "Blad podczas otwierania strumienia!" << endl;
- return 1;
- }
- for (int i = 0; i < 4; i++) {
- tab[i] += 1;
- cout << tab[i];
- }
- cout << "*********** 4 *************\n" << endl;
- tab[1] = tab[1] + " Kowalska";
- tab[3] = "Bocian " + tab[3];
- for (int i = 0; i < 4; i++) {
- cout << tab[i];
- }
- cout << "*********** 5 *************\n" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement