Facebook
From Sweltering Lemur, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 325
  1. #include <iostream>
  2. #include<string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. class CNum{
  7. public:
  8.     CNum(){
  9.         pi_num = new int();
  10.         *pi_num=11;
  11.     }
  12.     CNum(CNum &other){
  13.         pi_num = new int(*other.pi_num);
  14.     }
  15.     ~CNum(){
  16.         if(pi_num!=NULL) delete pi_num;
  17.     }
  18.     int iGetVal(){
  19.         return *pi_num;
  20.     }
  21.     CNum operator + (CNum &other){
  22.         CNum result;
  23.         *result.pi_num=*pi_num+*other.pi_num;
  24.         return result;
  25.     }
  26.     void operator = (int val){
  27.         *pi_num=val;
  28.     }
  29.  
  30. private:
  31.     int *pi_num;
  32. };
  33.  
  34. int main() {
  35.     CNum c_t0, c_t1, c_t2;
  36.     cout<<c_t2.iGetVal();
  37.     c_t0=2;
  38.     c_t1=3;
  39.     CNum csum;
  40.     c_t0+c_t1;
  41.     csum = c_t0 + c_t1;
  42.     cout<<c_t0.iGetVal();
  43.     cout<<csum.iGetVal();
  44. }
  45.  
  46.  
  47. #include <iostream>
  48. #include<string>
  49. using namespace std;
  50. class CTab{
  51. public:
  52.     CTab(){
  53.         pi_tab=new int();
  54.         size=0;
  55.     }
  56.  
  57.     ~CTab(){if(pi_tab!=NULL) delete pi_tab;}
  58.  
  59.     void vSetSize(int iSize){
  60.         delete pi_tab;
  61.         size=iSize;
  62.         pi_tab=new int[iSize];
  63.  
  64.     }
  65.     CTab(const CTab &COther){
  66.         size=COther.size;
  67.         pi_tab=new int[size];
  68.         for (int i=0; i<size; i++){
  69.             pi_tab[i]=COther.pi_tab[i];
  70.         }
  71.     }
  72.     CTab operator+(CTab &other) {
  73.         int newSize = size + other.size;
  74.         CTab CRes;
  75.         CRes.vSetSize(newSize);
  76.         int i = 0;
  77.         for (i; i < size; i++) {
  78.             CRes.pi_tab[i] = pi_tab[i];
  79.         }
  80.         for (int j = 0; j < other.size; j++) {
  81.             CRes.pi_tab[i] = other.pi_tab[j];
  82.             i++;
  83.         }
  84.  
  85.  
  86.         return (CRes);
  87.     }
  88.     void operator=(const CTab &other){
  89.         if (pi_tab!=NULL) delete pi_tab;
  90.         size=other.size;
  91.         pi_tab=new int[other.size];
  92.         for (int i=0; i<other.size; i++){
  93.             pi_tab[i]=other.pi_tab[i];
  94.         }
  95.     }
  96.  
  97.     int *pi_tab;
  98.     int size;
  99. };
  100. int main() {
  101.     CTab ct0, ct1;
  102.     ct0.vSetSize(2);
  103.     ct1.vSetSize(1);
  104.     ct0.pi_tab[0]=0;
  105.     ct0.pi_tab[1]=1;
  106.     ct1.pi_tab[0]=4;
  107.     CTab csum;
  108.     csum=ct0+ct1;
  109.     for (int i=0; i<3; i++){
  110.         cout<<csum.pi_tab[i]<<" ";
  111.     }
  112. }
  113.  
  114. #include <iostream>
  115. #include<string>
  116. #include <vector>
  117.  
  118. using namespace std;
  119. class CNode{
  120. public:
  121.     CNode(int val){
  122.         pi_val=new int(val);
  123.     }
  124.     CNode(CNode &other){
  125.         pi_val=new int(*other.pi_val);
  126.         for(int i=0; i<other.v_kids.size(); i++){
  127.             v_kids.push_back(other.v_kids[i]);
  128.         }
  129.     }
  130.     ~CNode(){
  131.         delete pi_val;
  132.         for(int i=0; i<v_kids.size(); i++){
  133.             if(v_kids[i]!=NULL) delete v_kids[i];
  134.         }
  135.     }
  136.     void vSetVal(int iVal){
  137.         *pi_val=iVal;
  138.     }
  139.     void vAdd(CNode *pcKid){
  140.         v_kids.push_back(pcKid);
  141.     }
  142.     void vPrint(){
  143.         cout<<*pi_val<<" ";
  144.         for(int i=0; i<v_kids.size(); i++){
  145.             v_kids[i]->vPrint();
  146.         }
  147.     }
  148.  
  149. private:
  150.     int *pi_val;
  151.     vector<CNode*> v_kids;
  152. };
  153.  
  154. class CTree{
  155. public:
  156.     CTree(){
  157.         pc_root=new CNode(0);
  158.     }
  159.     CTree(CTree &other){
  160.         pc_root=new CNode(*other.pc_root);
  161.     }
  162.     ~CTree(){
  163.         delete pc_root;
  164.     }
  165.     CNode *pcGetRt(){
  166.         return pc_root;
  167.     }
  168.     void vPrint(){
  169.         pc_root->vPrint();
  170.     }
  171. private:
  172.     CNode *pc_root;
  173. };
  174.  
  175. int main() {
  176.     CTree c_t0;
  177.     c_t0.pcGetRt()->vSetVal(1);
  178.     c_t0.pcGetRt()->vAdd(new CNode(2));
  179.  
  180.     CNode *pc_buf;
  181.     pc_buf = new CNode(3);
  182.     pc_buf->vAdd(new CNode(4));
  183.     c_t0.pcGetRt()->vAdd(pc_buf);
  184.     c_t0.vPrint();
  185.  
  186.     CTree *pc_copy = new CTree(c_t0);
  187.  
  188.     pc_copy->vPrint();
  189.     delete pc_copy;
  190. }*/