overloading operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <sstream>

using namespace std;
//////////////////////////////////
class bMoney
{
      private:
         long double money;
         int n;
         char* end;
         int len;
         char* str;
         char* karakter;
      
      public:
             
       bMoney(): money(0.0),n(0),len(0)
       { }
       bMoney(long double par): money(par)
       { }
       ///////////////////////////
       void getMoney()
       {
       
      cout << "Enter money: "; 
      cin >> str;
      }
      //////////////////////////
      void putMoney()
      {cout <<"Your cash is:" << money << endl;}
      ////////////////////////////////////////////////////////////
      void mstold()
      {
          int n=0;
          
          char* end;
          int len = strlen(str);
                 for(int i=0; i<len+1; i++)    
                 {
                      if(str[i] >= '0' && str[i] <= '9')
                      {
                        karakter[n] = str[i];
                      n++;
                      }
                 }
     
                        
               money = strtold(karakter,NULL);
     
               
      }
        
///////////////////////////////////////////////////////
       void ldtombs()
        {
         std::ostringstream ss;
         ss << money;
        karakter = strdup( ss.str().c_str() );
         }
         
       void addmoney(bMoney a)
         {
            money +=a.money;
         }
//////////////////////////////////////////////////
       bMoney operator + (bMoney a2)
       {
              long double temp; 
              temp = money + a2.money;
              
              return bMoney(temp);
       
       
       }
/////////////////////////////////////////////////////       
       bMoney operator - (bMoney a2)
       {
              long double temp;
             temp = money - a2.money;
       
              return bMoney(temp);
       }
//////////////////////////////////////////////////////
       bMoney operator * (bMoney a2)
       {
              long double temp;
             temp = money * a2.money;
              
              return bMoney(temp);
       }
////////////////////////////////////////////////////////////7
      bMoney operator / (bMoney a2)
      {
             long double temp;
            temp = money / a2.money;
      
             return bMoney(temp);
      }
////////////////////////////////////////////////////////////////
      long double  operator / (long double a2)
      {
                    return (money / a2);
      }            
                   
              
};
            

   
int main(int argc, char *argv[])
{
    bMoney a1,a2,a3;
    a1.getMoney();  
    a1.mstold();
    a2.getMoney();
    a2.mstold();
    a3 = a1 + a2;
    a3.mstold();
    a3.putMoney();
   
   

    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

where is the problem in operator + ?
Last edited on
You never allocate memory for str, karakter and end varibales, they are pointing to some memory garbage. you can dynamically allocate memory with new, and then deallocate with delete, but I recomend to use std::vector, which will do the memory (de)allocation for you, if you use arrays then you have to define copy construtor, and assignment operator, which will copy the contents of the arrays :
1
2
3
4
5
6
7
bMoney(const bMoney& other) {
    //copy and assign stuff from other to *this
}
bMoney& operator=(const bMoney& other) {
    //copy stuff and assign from other to *this
    return *this;
}


Hope this helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//30.06.2009
//para karakter katarını long double'a çevirir.Long double'ı da para karakter katarına çevirir

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <sstream>

using namespace std;
//////////////////////////////////
class bMoney
{
      private: //declare variables
         long double money;
         int n;
         int len;
         char str[100];
         char karakter[100];
      
      public:
    
       bMoney(): money(0.0),n(0),len(0) // constructor
       { }
       bMoney(long double par): money(par) //what is this^_^
       { }
       
       bMoney(const bMoney& other) //copy constructor
       {
          money += other.money;
       }
       //////////////////////////////////
       bMoney& operator = (const bMoney& other) //assignment operator
       {
               money+=other.money;
               return *this;
       }
            ///////////////////////////
       void getMoney() //get money because i need money
       {
       
      cout << "Enter money: "; 
      cin >> str;
      }
      //////////////////////////
      void putMoney() // this is your money
      {cout <<"Your cash is:" << money << endl;}
      ////////////////////////////////////////////////////////////
      void mstold() // convert string to long double 
      {
          int n=0;
          int len = strlen(str);
                 for(int i=0; i<len+1; i++)    
                 {
                      if(str[i] >= '0' && str[i] <= '9')
                      {
                        karakter[n] = str[i];
                      n++;
                      }
                 }
     
                        
               money = strtold(karakter,NULL);
     
               
      }
        
///////////////////////////////////////////////////////

         
       void addmoney(bMoney a) // add money
         {
            money +=a.money;
         }
//////////////////////////////////////////////////
       bMoney operator + (bMoney a2) // + operator
       {
            this -> money += a2.money;
            return *this;
       
       }
/////////////////////////////////////////////////////       
       bMoney operator - (bMoney a2) // - operator
       {
               this -> money += a2.money;
            return *this;
       }
//////////////////////////////////////////////////////
       bMoney operator * (bMoney a2) //* operator
       {
               this -> money -= a2.money;
                return *this;
       }
////////////////////////////////////////////////////////////7
      bMoney operator / (bMoney a2)
      {
             this -> money /= a2.money;
                return *this;
      }

         
                                
};
////////////////////////////////////////////////////////////////
        
int main(int argc, char *argv[])
{
    bMoney a1,a2,a3;
    a1.getMoney();  
    a1.mstold();
    a2.getMoney();
    a2.mstold();
    a3 = a1 + a2;
    a3.mstold();
    a3.putMoney();
   

    system("PAUSE");
    return EXIT_SUCCESS;
}

i dont take error. but. out put is "Cash is 0 " i dont understand
Last edited on
Those pointers point to the stack...

What does the function void bMoney::getMoney() do? It doesn't deal with any of the member variables.

Many other issues like...
the copy constructor and the assignment operator are doing += on money. Money isn't necessorily 0.
for other operators, better also pass in a const reference object...and, review all their bodys...- is with +=...* is with -=...and
e.g. + should not change *this, if you want to change *this overload +=, make the interface easier to use...
silly me.i review this code again.this is a trash.
Topic archived. No new replies allowed.