increment function
Sep 26, 2009 at 5:10pm UTC
Id like state that i am aware that ive posted this code before in another thread but im having a different issue. Im trying to increment certain points by a given amount but when i do it doesnt work.
heres my code:
header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef location_H
#define location_H
#include <iostream>
class location
{
friend std::istream& operator >>(std::istream & , location &);
friend std::ostream& operator <<(std::ostream & , location &);
private :
int row;
int colm;
public :
location (int =0,int =0);
void assign(int , int );
location operator =(location);
bool operator ==(location);
int inc(int , int );
int Row(){return row;};
int Colm(){return colm;};
// bool WithinRectangle(Location, Location);
};
#endif
implementation
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 47 48 49 50 51 52 53 54
#include "location.h"
location::location(int r,int c)
{
row=r;
colm=c;
}
void location::assign(int r,int c)
{
row=r;
colm=c;
}
location location::operator = (location y)
{
row = y.row;
colm = y.colm;
return y;
}
bool location::operator == (location z)
{
return ((row==z.row)&&(colm==z.colm));
}
//bool location::WithinRectangle(location x, location y)
//{
// location x= (1,1);
// location y= (10,10);
//
//)
int location::inc(int a, int b)
{
row=+a;
colm=+b;
return ((row)&&(colm));
}
std::istream& operator >>(std::istream &input , location &x)
{
char a;
input>>a
>>x.row
>>a
>>x.colm
>>a;
return input;
}
std::ostream& operator <<(std::ostream &output , location &x)
{
output<<'('
<<x.row
<<','
<<x.colm
<<')' ;
return output;
}
main program:
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
#include "location.h"
int main()
{
location x,y(1),z(1,2);
std::cout<<"testing constructor" <<std::endl;
std::cout<<"x=" <<x<<std::endl;
std::cout<<"y=" <<y<<std::endl;
std::cout<<"z=" <<z<<std::endl;
std::cout<<"testing assign function" <<std::endl;
x.assign(3,7);
y.assign(4,3);
std::cout<<"Enter a location for z (r,c)" ;
std::cin>>z;
std::cout<<"x=" <<x<<std::endl;
std::cout<<"y=" <<y<<std::endl;
std::cout<<"z=" <<z<<std::endl;
std::cout<<"testing == function" <<std::endl;
if (x==y)
std::cout<<"these points are equal" <<std::endl;
else
std::cout<<"these points are not equal" <<std::endl;
z=y;
std::cout<<"testing = function" <<std::endl;
std::cout<<"z=" <<z<<std::endl;
x=x.Row();
y=y.Row();
z=z.Row();
std::cout<<"testing row function" <<std::endl;
std::cout<<"x row=" <<x<<std::endl;
std::cout<<"y row=" <<y<<std::endl;
std::cout<<"z row=" <<z<<std::endl;
x.assign(3,7);
y.assign(4,3);
z.assign(4,3);
x=x.Colm();
y=y.Colm();
z=z.Colm();
std::cout<<"testing colm function" <<std::endl;
std::cout<<"x column=" <<x<<std::endl;
std::cout<<"y column=" <<y<<std::endl;
std::cout<<"z column=" <<z<<std::endl;
//std::cout<<"testing Withinrectangle function"<<std::endl;
x=x.inc(1,2);
y=y.inc(1,2);
z=z.inc(1,2);
std::cout<<"testing inc function" <<std::endl;
std::cout<<"x=" <<x<<std::endl;
std::cout<<"y=" <<y<<std::endl;
std::cout<<"z=" <<z<<std::endl;
system("pause" );
return 0;
}
note:ignore the commented section.its a function i havent worked on yet.
I know my problem is my return value for the inc function itself,but im not sure what to do on that front.If someone can help me i would appreciate it.
Sep 26, 2009 at 7:41pm UTC
I think you probably want += instead of =+.
Topic archived. No new replies allowed.