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 {
- string* p1;
- public:
- K1() : p1(new string[2]) {p1[0] = "Nothing"; p1[1] = "Nothing";};
- K1(const string& one, const string& two) { p1 = new string[2]; p1[0] = one; p1[1] = two; };
- ~K1() { delete p1; };
- friend ostream & operator <<(ostream &, const K1&);
- string & operator[](size_t i) const { // indexing operator
- return p1[i];
- }
- };
- ostream & operator <<(ostream & out, const K1 & object) {
- return out << object.p1[0] << " " << object.p1[1] << endl;
- }
- class K2 {
- K1 p1;
- double p2;
- public:
- K2(): p2(0){};
- K2(const string & one, const string & two, const double & price): p1(one, two), p2(price){};
- K2(const K2 & object){ // konstruktor kopiacy
- p1 = object.p1;
- p2 = object.p2;
- }
- const K2 & operator=(const K2 & object){ // operator przypisania
- if(this != &object){
- p1 = object.p1;
- p2 = object.p2;
- }
- return *this;
- }
- const K2 operator-(const double & value) const {
- return K2(p1[0], p1[1], p2 - value);
- }
- void czytajLinie(ifstream & input){
- if(!input) throw string("Problem ze strumieniem wczytywania");
- input >> p1[0];
- input >> p1[1];
- input >> p2;
- }
- const K2 operator+(const double & value) const { // przeciazanie operatora sumy
- return K2(p1[0], p1[1], p2 + value);
- }
- /*const K2 operator+=(const double & value) const{ // przeciazanie operatora sumowania iteracyjnego
- return K2(p1[0], p1[1], p2 + value);
- }*/
- friend ostream & operator <<(ostream &, const K2&);
- friend K2 operator +=(const K2 &, const double &);
- };
- K2 operator += (const K2 & object, const double & value){
- return K2(object.p1[0], object.p1[1], value + object.p2);
- }
- ostream & operator <<(ostream & out, const K2& object){
- return out << object.p1[0] << " " << object.p1[1] << " " << object.p2 << endl;
- }
- int main() {
- K2 ob1, ob2;
- const K2* wsk1 = new K2("kawa", "z mlekiem", 4.50);
- 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 - 1.25;
- cout << "******** 3 **********" << endl;
- K2 tab[4];
- ifstream plik("data.txt");
- try{
- if(!plik) throw string("Cannot open the file");
- for(size_t i = 0; i < 4; ++i)
- tab[i].czytajLinie(plik);
- }
- catch(const string & err){cout << err << endl;}
- catch(...){cout << "Contact with developers" << endl;}
- /*for(size_t i = 0; i < 4; i++){
- tab[i] += 1; //???
- cout << tab[i];
- }*/
- tab[1] += 1;
- cout << tab[1];
- cout << "******** 4 **********" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement