object division

As simple as it is seems confusing to me at the moment,
How to divide 2 objects correctly.

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
#include <iostream>
#include <string>
using namespace std;
/*
        1.    try    ---
        2.    catch  --- 
        3.    throw  ---
  */     
  
  class var
  {
  	int x;
  
  	
  	public:
  		
  	var(int x=NULL){this->x=x; }
  	
  	var(var&Obj){this->x=Obj.x;}
  	
  	~var(){}
  	
  	void setX(int x){cout<<"Enter value= "; cin>>this->x;}

  	
  	const int getX() const{return this->x;}
  
  	friend ostream& operator<<(ostream&OUT, var&X);
   
   
 void operator=(int x)
 {
 this->x=x;
 }
 
void operator=(var&Obj)
 {
 this->x=Obj.x;
 }
 
 
   double operator /(var&Obj)
 {
 	return this->x/Obj.x;
 } 
  	
};
  
ostream& operator<<(ostream&OUT, var&X)
{
 OUT<<"Value="<<X.x;

 return OUT;
}
  
  
  
int main()
{ 

var A(3);
var B(4);
var R;

cout<<A<<endl;
cout<<B<<endl;

R=A/B;cout<<R;

R= static_cast<var>(A)/B;



/*
int x;

while(1)
{
    system("pause");
    system("cls");
    
    
   try
   {
     A.setX();
     B.setX();
     
   if( A == 0 || B==0)  throw 7;
      cout<<" OK "<<x<<endl;
  }
  
  
   catch(int 7)
   {
  cout<<" ERROR , division by 0 is restricted, provide another values"<<ex<<endl;
   }
}   
*/

 return 0;
}


1. expect to receive R=0.75;
2. all that's in comment section should work as an infinite loop until one of the values is not equal to 0.
expect to receive R=0.75;
The member variable x of the class var is of the type int. Which means you have an integer division without remainder. So either change to double x; or cast one of the values to double:
1
2
3
4
5
6
7
   double operator /(var&Obj)
 {
  if(0 == Obj.x)
    throw 0; // Normally it should inherite from std::exception

 	return static_cast<double>(this->x)/Obj.x;
 }
@coder777. thanks mate
Topic archived. No new replies allowed.