Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  4-előadás
  4. //
  5. //  Created by Majer Imre on 2015. 03. 02..
  6. //  Copyright (c) 2015. Majer Imre. All rights reserved.
  7. //
  8.  
  9. /*
  10. szemely *sz;
  11. new-delet operátorok
  12. int *i = new int;
  13. szemely *sz = new szemely("Pisti");
  14. delete i;
  15. delete sz;
  16. */
  17.  
  18. /*
  19. int * i = new int[100];
  20. i[5]=8;
  21. delete[] i;
  22. */
  23.  
  24. /*
  25. #include<string.h>
  26.  
  27.  
  28. class szemely {
  29.     char *cim;
  30. public:
  31.     szemely(char *cimpar) {
  32.         cim=new char[strlen(cimpar)+1];
  33.         strcpy(cim, cimpar);
  34.     }
  35.     ~szemely() {
  36.         delete[] cim;
  37.     }
  38.     szemely(const szemely& szp) {
  39.         cim= new char[strlen(szp.cim)+1];
  40.         strcpy(cim, szp.cim);
  41.     }
  42. };
  43.  
  44. int main() {
  45.     szemely sz1("Magyar T 2");
  46.     szemely sz2(sz1);
  47. }
  48. */
  49.  
  50. /*
  51.  A a();
  52.  A b(a);
  53.  A a2= A();
  54.  A a3;
  55.  a3=a;
  56.  */
  57.  
  58. //FIFO - First in First out
  59.  
  60. #include<iostream>
  61.  
  62. class intfifo {
  63.     int elementnum;
  64.     int * pdata;
  65. public:
  66.     intfifo() {
  67.         elementnum=0; pdata= NULL;
  68.     }
  69.     intfifo(const intfifo& theother) {
  70.         if (theother.elementnum!=0){
  71.             pdata=new int[theother.elementnum];}
  72.         else
  73.         {
  74.             pdata=NULL;
  75.             elementnum =theother.elementnum;}
  76.         for(int i=0; i<elementnum; i++) {
  77.             pdata[i]=theother.pdata[i];
  78.         }
  79.     };
  80.     ~intfifo() {
  81.         if(pdata!=0)
  82.             delete[] pdata;
  83.     }
  84.     bool isempty() { return !elementnum;}
  85.     bool get( int& element);
  86.     bool put( int element);
  87.     const intfifo& operator=(const intfifo& theother);
  88.     intfifo(const intfifo& the other){
  89.         {
  90.             pdata=0;
  91.             *this = theother;
  92.         }
  93.     };
  94. };
  95.  
  96.  
  97. bool intfifo::get(int& element) {
  98.     if(isempty()) {
  99.         return false; }
  100.        element=pdata[0];
  101.        if(elementnum==1){
  102.            delete[] pdata; pdata=NULL;}
  103.        else{
  104.            int *tmp =new int[elementnum-1];
  105.            for (int i=0;i<elementnum-1; i++)
  106.            {
  107.                tmp[i]=pdata[i+1];
  108.            }
  109.            delete[] pdata;
  110.            pdata =tmp;
  111.        }
  112.        elementnum--;
  113.        return true;
  114. }
  115.  
  116. bool intfifo::put(int element) {
  117.     if(elementnum==INT_MAX) {return false;}
  118.     if(elementnum==0) {
  119.        pdata =new int[1];
  120.         pdata[0]= element;
  121.         elementnum--;}
  122.     else {
  123.         int *tmp=new int[elementnum+1];
  124.         for(int i=0; i<elementnum; i++) {
  125.             tmp[i]=pdata[i];
  126.         }
  127.         tmp[elementnum]= element;
  128.         delete[] pdata;
  129.         pdata=tmp;
  130.     }
  131.     elementnum++;
  132.     return true;
  133. }
  134.  
  135. const intfifo& intfifo::operator=(const intfifo& theother) {
  136.     if(this==&theother) {
  137.         return *this;
  138.     }
  139.     delete[] pdata;
  140.     if(theother.elementnum==0){
  141.         pdata=0;
  142.         elementnum=0;
  143.     }
  144.     else {
  145.         elementnum=theother.elementnum;
  146.         pdata= new int[elementnum];
  147.         for(int i=0;i<elementnum;i++)
  148.         {
  149.             pdata[i]=theother.pdata[i];
  150.         }
  151.     }
  152.     return *this;
  153. }
  154.  
  155. int main() {
  156.     intfifo f1;
  157.     f1.put(5);
  158.     f1.put(6);
  159.     int i;
  160.     f1.get(i);
  161.     intfifo f2=f1;
  162.     intfifo f3;
  163.     f3=f1;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement