Hello Members!
I am not completely new to C++. I have been studying it since past few months. But I have got a problem! Hope someone will find me the solution!
PROB: Suppose I have a class called 'location' and the class has three public member functions called getx(int), gety(int) and display(). Now I have made an object of location(inside main) called loc.
How to call them from main function like below:
loc.getx(10).gety(20).display();
You help will be appreciated!
4 replies to this topic
#1
Posted 16 January 2011 - 11:58 PM
|
|
|
#2
Posted 17 January 2011 - 12:24 AM
Ok.. I have got the solution. Look at the sample code below:
#include<iostream>
using namespace std;
class Location
{
int x,y;
public:
Location &getx(int a)
{
x=a;
return *this;
}
Location &gety(int b)
{
y=b;
return *this;
}
void display()
{
cout<<"x= "<<x<<", y= "<<y<<endl;
}
};
int main()
{
Location loc;
loc.getx(10).gety(20).display();
return 0;
}
#3
Posted 17 January 2011 - 01:49 AM
You arn't getting x and y, you are setting x and y. and when you set x it doesn't make sense to return the object, it would make sense to return the number, just incase the user want's to use it in a loop and check x again.
like
without getx returning x you will have no ability to use that kind of loop.
always return x on getting and setting a value for this reason!
then you can call display() every time in the loop.
If you want to explicitly change display coordinates every time you call display, think about creating an overloaded display function that accepts an x and y.
the display function would then take in two parameters, change the Location's coordinate, then draw the coordinate all within the same function.
it could look something like this:
Hope this helped!
~Skippy
like
while( int x = getx(); x<SCREEN_RIGHT_BOARDER; x = getx(1) )
{
display();
}
without getx returning x you will have no ability to use that kind of loop.
always return x on getting and setting a value for this reason!
then you can call display() every time in the loop.
If you want to explicitly change display coordinates every time you call display, think about creating an overloaded display function that accepts an x and y.
the display function would then take in two parameters, change the Location's coordinate, then draw the coordinate all within the same function.
it could look something like this:
void display(int x, int y )
{
setx(x);
sety(y);
display(); // don't create duplicate code.
}
// and main could call it like this:
// ( x and y are already variables with the initial Location coordinates. )
while ( ( y < boarder.y ) && ( x < boarder.x ) )
{
display( x+1, y+1 );
}
Hope this helped!
~Skippy
Edited by dargueta, 17 January 2011 - 08:06 PM.
Added code tags, fixed syntax error
#4
Posted 17 January 2011 - 01:55 AM
Skippy said:
You arn't getting x and y, you are setting x and y. and when you set x it doesn't make sense to return the object, it would make sense to return the number, just incase the user want's to use it in a loop and check x again.
like
while( int x = getx(); x<SCREEN_RIGHT_BOARDER; x = getx(1) )
{
display()
}
without getx returning x you will have no ability to use that kind of loop.
always return x on getting and setting a value for this reason!
then you can call display() every time in the loop.
If you want to explicitly change display coordinates every time you call display, think about creating an overloaded display function that accepts an x and y.
the display function would then take in two parameters, change the Location's coordinate, then draw the coordinate all within the same function.
it could look something like this:
void display(int x, int y )
{
setx(x);
sety(y);
display() // don't create duplicate code.
}
Hope this helped!
~Skippy
like
while( int x = getx(); x<SCREEN_RIGHT_BOARDER; x = getx(1) )
{
display()
}
without getx returning x you will have no ability to use that kind of loop.
always return x on getting and setting a value for this reason!
then you can call display() every time in the loop.
If you want to explicitly change display coordinates every time you call display, think about creating an overloaded display function that accepts an x and y.
the display function would then take in two parameters, change the Location's coordinate, then draw the coordinate all within the same function.
it could look something like this:
void display(int x, int y )
{
setx(x);
sety(y);
display() // don't create duplicate code.
}
Hope this helped!
~Skippy
Oh thanks for that post! I just wanted to know if in case we were supposed to do in this manner then how can we do. BTW i have got the solution.
I really appreciate your help!
Regards!
#5
Posted 17 January 2011 - 06:03 PM
Sure thing buddy :-) if you need any other help feel free to ask me or anyone else
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









