Facebook
From Lousy Parrot, 3 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 143
  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(11);
  10.     }
  11.     CNum(CNum &other){
  12.         pi_num = new int(*other.pi_num);
  13.     }
  14.     ~CNum(){
  15.         if(pi_num!=NULL) delete pi_num;
  16.     }
  17.     int iGetVal(){
  18.         return *pi_num;
  19.     }
  20.     CNum operator + (CNum &other){
  21.         CNum result;
  22.         *result.pi_num=*pi_num+*other.pi_num;
  23.         return result;
  24.     }
  25.     void operator = (int val){
  26.         *pi_num=val;
  27.     }
  28.  
  29. private:
  30.     int *pi_num;
  31. };
  32.  
  33. int main() {
  34.     CNum c_t0, c_t1, c_t2;
  35.     cout<<c_t2.iGetVal();
  36.     c_t0=2;
  37.     c_t1=3;
  38.     CNum csum;
  39.     c_t0+c_t1;
  40.     csum = c_t0 + c_t1;
  41.     cout<<c_t0.iGetVal();
  42.     cout<<csum.iGetVal();
  43. }
  44.