Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 16:28

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C++ OOP, Beginner problem/question

Discussion on C++ OOP, Beginner problem/question within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2012
Posts: 370
Received Thanks: 20
C++ OOP, Beginner problem/question

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;    
}
Hikarim is offline  
Old 04/16/2013, 16:18   #2
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
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 '}'.
Dr. Coxxy is offline  
Old 04/16/2013, 16:27   #3
 
elite*gold: 0
Join Date: Apr 2012
Posts: 370
Received Thanks: 20
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
Hikarim is offline  
Old 04/16/2013, 16:29   #4
 
.Scy's Avatar
 
elite*gold: 15
Join Date: Jul 2010
Posts: 3,926
Received Thanks: 1,158
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
.Scy is offline  
Old 04/16/2013, 16:33   #5
 
elite*gold: 0
Join Date: Apr 2012
Posts: 370
Received Thanks: 20
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...
Hikarim is offline  
Old 04/16/2013, 16:35   #6
 
Dr. Coxxy's Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
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.
Dr. Coxxy is offline  
Old 04/16/2013, 22:26   #7
 
elite*gold: 0
Join Date: Apr 2012
Posts: 370
Received Thanks: 20
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) :/
Hikarim is offline  
Reply


Similar Threads Similar Threads
Beginner's question
12/04/2012 - SRO Private Server - 2 Replies
Hey guys, I have a little question for you. I finally decided to start learning about pk2 editing and all that. I downloaded pushedx pk2 exporter and everything. Now, I used Cherno's tool for finding a blowfish key and it gave me 169841. Now, I tried everything, converted it to hex (tried like 2-3 different methods), just typing 169841, and countless other things, and nothing works. It was still a wrong password. So my question is: What's the real password? I just want to mess around with...
Beginner Gold premium question
03/31/2010 - Silkroad Online - 12 Replies
Well im about to buy it, so, is the premium 150% xp/sp 28 days? and preferred game login? what else you know about it? ty in advance, diogo
Beginner BOT question
07/06/2009 - World of Warcraft - 0 Replies
Hey everyone, I have been playing WoW off and on for a couple of years now and was thinking of getting back into it again. I do remember the constant grinding to gain levels so this time around i was thinking of perhaps using a bot to do the dirty work for me. My question is mainly how safe they are and in which way they can be used to minimize the chance of being detected? I was actually planning on letting the bot do its thing while i basically sit by the computer doing other things...
Beginner CE question..
04/17/2008 - Cabal Online - 9 Replies
Oi! Eversince i started playing cabal i've been playing with ProjectX, Sora Engine and Cheat engine 5.4... However, i can't get much to work.. Only a few hacks. I run Cabal Europe, with GG emuserver coded by Pyte, OS: Windows Vista. I once got the walkspeed hack to work, but after that, nothing. After a while not even the walkspeed hack anymore. When i run a cheat engine (Mostly Sora Engine), and i click the process list, i can't see CABAL. Only in the "window list". Is this normal? I think...



All times are GMT +2. The time now is 16:28.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.