C++ OOP begiiiner question/problem

Hi. I just started with Object Oriented programming in C++ and i got a book from the library which has some exercises but sadly no solutions for them,... and i got this problem... Well, i have to put in the length of sides of a square (they are required to be entered as integers). But then i have to write a method to extend their length for a certain %, but here the tricky part that for that to show up correctly i would have to use double or somehow transform from int to double... And i would like to ask how can i change the type? -as i have been breaking my head for this latelly...

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
#include <iostream>


using namespace std;

class aabbcc{
      private:
              int x,y;
      public:
             void setxy(int a, int b){
                    x=a;
                    y=b;     
             }      
      
             int sqxy(){
                 return x*y;    
             }
             
             void change(int proc){
                  
                  x=x+((x/100)*proc);
                  y=y+((y/100)*proc);

                  
             }
             void print(){
                  cout << "X and Y" <<"(" << x << ", " << y << ")" << endl;     
             }
};


int main(){
    
    aabbcc green;
    
    green.setxy(10,5);
    cout << "x*y: " << green.sqxy() << endl;
    green. print();
    
    green.change(20);
    cout << "Changed: " << green.sqxy() << endl;
    green.print();
    
    system("PAUSE");
    return 0;    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private:
    double x, y;
public:
    void setxy(int a, int b) //Still uses int
    {
        x=a; //Will work fine (implicit type conversion here)
        y=b;     
     }

    void change(int proc)
    {
        x += x * (proc / 100.0); //Readability and correct division
        y += y * (proc / 100.0); //(floating point instead of integer one)
     }
Last edited on
Thanks, but the exercise requires it so that x and y are declared as int.. (dont knwo why, but thats how it is :S),
so:
1
2
private:
    int x, y;
And still you don't need to change your change() method. It will still work.
Oh, ye, i got it now... Thank you.
Topic archived. No new replies allowed.