### 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...

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include 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; }``````
 ``1234567891011121314`` ``````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:
 ``12`` ``````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.