Advertisement
Guest User

Oba kolosy na 4 zrobione

a guest
Nov 12th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. /*class K1{
  7. string* p1;
  8. public:
  9.     K1():p1(new string [2]){
  10.     p1[0]="Nie podano";
  11.     p1[1]="nie Podano";
  12.     }
  13.     K1(const string& a,const string& b):p1(new string [2]){
  14.     p1[0]=a;
  15.     p1[1]=b;
  16.     }
  17.     K1(const K1& obj):p1(new string [2]){
  18.     p1[0]=obj.p1[0];
  19.     p1[1]=obj.p1[1];
  20.     }
  21.     ~K1() {delete [] p1;}
  22.     friend ostream& operator <<(ostream& out, const K1& obj);
  23.  
  24.     string& operator [](int id){
  25.     return p1[id];
  26.     }
  27.  
  28.     K1 operator =(const K1& obj){// DO ZAPAMIĘTANIA
  29.     p1=new string [2];
  30.     p1[0]=obj.p1[0];
  31.     p1[1]=obj.p1[1];
  32.     return *this;
  33.     };
  34. };
  35. ostream& operator <<(ostream& out, const K1& obj){
  36. return out<<obj.p1[0]<<"| |"<<obj.p1[1]<<"| |";
  37. }
  38.  
  39. class K2{
  40.   K1 p1;
  41.   double p2;
  42.   public:
  43.     K2():p1(),p2(0){};
  44.     K2(const string& a,const string& b,double c):p1(a,b),p2(c){};
  45.     K2(const K2& obj):p1(obj.p1),p2(obj.p2){};
  46.     friend ostream& operator <<(ostream& out, const K2& obj);
  47.     K2 operator =(const K2& obj){
  48.     p1=obj.p1;
  49.     p2=obj.p2;
  50.     return *this;
  51.     }
  52.     friend K2 operator -(const K2& obj,double val);
  53.     friend istream& operator>>(istream& in,K2& obj);
  54.  
  55.     void operator +=(const int & val){
  56.       this->p2+=val;
  57.     }
  58. };
  59.  
  60. ostream& operator <<(ostream& out, const K2& obj){
  61. return out<<obj.p1<<"| |"<<obj.p2<<"| |";
  62. }
  63.  
  64. istream& operator>>(istream& in,K2& obj){
  65. in>>obj.p1[0];
  66. in>>obj.p1[1];
  67. in>>obj.p2;
  68. return in;
  69. }
  70.  
  71. K2 operator -(const K2& obj,const double val){// DO ZAPAMIĘTANIA
  72. K2 newObj=K2(obj);
  73.  
  74. newObj.p2-=val;
  75. return newObj;
  76. }
  77.  
  78.  
  79.  
  80. int main()
  81. {
  82.     K2 ob1,ob2;
  83.     const K2 * wsk1= new K2("kawa","z mlekiem",4.50);
  84.     const K2 ob3(*wsk1);
  85.     delete wsk1;
  86.     wsk1=0;
  87.  
  88.     const K2 * wsk2 = new K2(ob3);
  89.     ob2=*wsk2;
  90.     cout<< ob1<<endl<<*wsk2;
  91.     delete wsk2;
  92.     wsk2=0;
  93.     cout<<ob2;
  94.     cout<<endl<<ob2-1.25;
  95.  
  96.     K2 txt[4];
  97.  
  98.     ifstream plik("dane.txt");
  99.     for(int i=0;i<4;i++){
  100.         plik>>txt[i];
  101.     }
  102.     plik.close();
  103.  
  104.     for(int i=0;i<4;i++){
  105.             txt[i]+=1;
  106.         cout<<txt[i];
  107.     }
  108.     return 0;
  109. }*/
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. class K1{
  117. string* p1;
  118. public:
  119.     K1():p1(new string){
  120.     *p1="Nie podano";
  121.     };
  122.     K1(const string& a):p1(new string){
  123.     *p1=a;}
  124.     K1(const K1& obj){
  125.     p1=new string;
  126.     *p1=obj.p1[0];
  127.     }
  128.     K1 operator=(const K1& obj){
  129.     p1=new string;
  130.     *p1=obj.p1[0];
  131.     return *this;
  132.     }
  133. friend ostream& operator<<(ostream& out,const K1& obj);
  134. ~K1(){delete p1;}
  135.     };
  136. ostream& operator<<(ostream& out,const K1& obj){
  137. return out<<obj.p1[0];
  138. }
  139.  
  140. class K2{
  141. K1 txt[2];
  142. int w1;
  143.  
  144. public:
  145.     K2(){
  146.     txt[0];
  147.     txt[1];
  148.     w1=0;
  149.     }
  150.     K2(const string& a,const string& b,int c):w1(c){
  151.     txt[0]=a;
  152.     txt[1]=b;
  153.     };
  154.     K2(const K2& obj){
  155.     txt[0]=obj.txt[0];
  156.     txt[1]=obj.txt[1];
  157.     w1=obj.w1;
  158.     }
  159.     friend istream& operator>>(istream& in,K2& obj);
  160.     friend ostream& operator<<(ostream& out,const K2& obj);
  161.     friend K2 operator +(const K2& obj,const int& val);
  162.     K2 operator =(const K2& obj){
  163.     txt[0]=obj.txt[0];
  164.     txt[1]=obj.txt[1];
  165.     w1=obj.w1;
  166.     }
  167.  
  168.     void operator+=(const int& val){
  169.     this->w1+=val;
  170.     }
  171. };
  172.  
  173. K2 operator +(const K2& obj,const int& val){
  174. K2 newObj=obj;
  175.  
  176. newObj.w1+=val;
  177. return newObj;
  178. }
  179.  
  180. ostream& operator<<(ostream& out,const K2& obj){
  181. return out<<obj.txt[0]<<" "<<obj.txt[1]<<" "<<obj.w1;
  182. }
  183. istream& operator>>(istream& in,K2& obj){
  184. string a,b;
  185. in>>a;
  186. obj.txt[0]=a;
  187. in>>b;
  188. obj.txt[1]=b;
  189. in>>obj.w1;
  190. return in;
  191. }
  192.  
  193.     int main(){
  194.    K2 ob1,ob2;
  195.    const K2 * wsk1 = new K2("Ala","Koala",15);
  196.    const K2 ob3(*wsk1);
  197.    delete wsk1;
  198.    wsk1=0;
  199.  
  200.    const K2 *wsk2 = new K2(ob3);
  201.    ob2=*wsk2;
  202.    delete wsk2;
  203.    wsk2=0;
  204.  
  205.    cout<<ob2;
  206.    cout<<ob2+10;
  207. K2 tab[4];
  208. ifstream plik("data.txt");
  209. for(int i=0;i<4;i++){
  210.     plik>>tab[i];
  211. }
  212. plik.close();
  213.  
  214. for(int i=0;i<4;i++){
  215.         tab[i]+=1;
  216.     cout<<tab[i]<<endl;
  217. }
  218.     return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement