C++ OOP, Beginner problem/question

04/16/2013 15:50 Hikarim#1
HI, i just latelly started with Object Oriented programming in c++ and i have a simple question... well, i have a square for which i put in the x and y, but then i have a method that should change the x and y, but somehow that doesnt happen, and i have been breaking my head to figure it out why not...

here is my code:
Code:
#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;    
}
04/16/2013 16:18 Dr. Coxxy#2
x and y are integers.

x=x+((x/100)*proc);
y=y+((y/100)*proc);

integer division for all x or y below 100, ?/100 will equal 0.

so it will be x = x + 0 * proc;

Code:
x = x+((x/100.0f)*proc); // you may need to cast the result or you will get a precision loss warning.
y = y+((y/100.0f)*proc);
or changing your x and y to floats/doubles will solve your problem.

EDIT:
btw. throw away this stupid k&r/java style, use a new line for every '{' and for every '}'.
04/16/2013 16:27 Hikarim#3
Ok, thank you. Is there a way i can just change the type int to doulbe just inside of that change and sqxy methods so i can still use an integer as input and all
04/16/2013 16:29 .Scy#4
well a floar or a double can have the same numbers as an integer so it wont affect anything if u change it :)

€: ok im wrong it will change something, u can from then on use numbers like 9.4 etc. wich is not possible with integers :D
04/16/2013 16:33 Hikarim#5
well, ye, i know that, but a got a book from the library, and i has some exercieses but not the solutions... :/ and it says i have to keep x andy as integers, and add to their values for a certain %, so if the x is 100 and %=20, it should return 120... but it becomes tricky when the numbers are decimal... :)
04/16/2013 16:35 Dr. Coxxy#6
ofc. just change the type of x and y in your class.
however, if you want to use ints you may need to add some casts, to avoid compiler precision loss warnings.

EDIT:
im slow as always.
following will do:
Code:
x += (int) (x * proc / 100.0f); // Use c++ style casts for free internetz
y += (int) (y * proc / 100.0f);
EDIT2:
if you dont want to use any floats use following:
Code:
x += x * proc / 100;
y += y * proc / 100;
however, this may produce inaccurate results.
04/16/2013 22:26 Hikarim#7
yes, i tried this, but the results are not correct as it rounds up some decimals and gives me full numbers... :/ i would use double, but as said the exercise requires me to put in both sides of square (x & y) as int, and then change their length for a certain % which may result in decimal values (double) :/