954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

OOP any chance of assistance? Its about areas etc

[php]
#include
#include

using namespace std;

class point
{
public:
point(int xcoord , int ycoord); //constructor
point();

int getX();
int getY();

double getDistance( point otherPoint );

void setpnt(point p);

private:
int x, y;
};

class rectangle
{
public:
rectangle(point p1, point p2, point p3, point p4);

void perimeter();

int area(point, point);

private:
point pt1;
point pt2;
point pt3;
point pt4;
};


point::point()
{
x = 1;
y = 2;
}
point::point(int xcoord, int ycoord )
{
x = xcoord ;
y = ycoord ;
}

double point::getDistance( point otherPoint)
{
double distance;
int xdist = otherPoint.x - x;
int ydist = otherPoint.y - y;

distance = sqrt( ( xdist * xdist) + (ydist * ydist) );
return distance;

}


int point::getX()
{
return x;
}

int point::getY()
{
return y;
}

void point::setpnt(point p)
{
x = p.getX();
y = p.getY();
}

rectangle::rectangle(point p1, point p2, point p3, point p4)
{
pt1.setpnt(p1);
pt2.setpnt(p2);
pt3.setpnt(p3);
pt4.setpnt(p4);
}

void perimeter(point pt1, point pt2)
{
int perimeter = 0;
cout << "the distance is" << point::getDistance();
}

int area()
{
int area = 0;


}
int main()

{
point pt1(2,3) , pt2(7,3), pt3(7,7), pt4(2,7);

rectangle box( pt1, pt2, pt3, pt4);

//cout << "perimeter is : " << box.perimeter() << endl;

return 0;
}
[/php]

code bug is : D:\cpp2\question4_week5.cpp(90) : error C2660: 'getDistance' : function does not take 0 parameters

Also I'm confused as how do I call another class's functions and data values?? Snce I need to find the lengh and height of a rectangle then go on to calculate the perimeter and area...

Thanks for assisting :)

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Member functions can be simply called in one of two ways. If the function is declared as static then you call it using the scope resolution operator and the name of the class:

class C {
public:
  static void foo();
};

// ...
C::foo();

If the member function is not declared as static then you must call it using the member access operator and anobject of the class:

class C {
public:
  void foo();
};

// ...
C obj;
obj.foo();

In your case, the line in question should be changed to:

cout << "the distance is" << pt1.getDistance(pt2);

Alternatively you could say:

cout << "the distance is" << pt2.getDistance(pt1);

Because this shouldn't effect the distance between the two points. The key is understanding that at least one of the objects passed to perimeter should be used as the base object that you call getDistance on and the other object should be passed as the argument to getDistance.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hello thanks for the assitance... I still can;t get this line of code to work... //cout << "perimeter is : " << box.perimeter() << endl;

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
Hello thanks for the assitance... I still can;t get this line of code to work... //cout << "perimeter is : " << box.perimeter() << endl;


You haven't defined the perimeter member function of rectangle, so you should be getting a linker error. The only advice for that I can offer is to write it so that you're actually doing something instead of calling a nonexistent function. ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
You haven't defined the perimeter member function of rectangle, so you should be getting a linker error. The only advice for that I can offer is to write it so that you're actually doing something instead of calling a nonexistent function. ;)


I Now have declared perimeter function... Howver its funny when I try to compile it ::

[php]
#include
#include

using namespace std;

class point
{
public:
point(int xcoord , int ycoord); //constructor
point();

int getX();
int getY();

double getDistance( point otherPoint );

void setpnt(point p);

private:
int x, y;
};

class rectangle
{
public:
rectangle(point p1, point p2, point p3, point p4);

void perimeter();

private:
point pt1;
point pt2;
point pt3;
point pt4;
};


point::point()
{
x = 1;
y = 2;
}
point::point(int xcoord, int ycoord )
{
x = xcoord ;
y = ycoord ;
}

double point::getDistance( point otherPoint)
{
double distance;
int xdist = otherPoint.x - x;
int ydist = otherPoint.y - y;

distance = sqrt( ( xdist * xdist) + (ydist * ydist) );
return distance;

}


int point::getX()
{
return x;
}

int point::getY()
{
return y;
}

void point::setpnt(point p)
{
x = p.getX();
y = p.getY();
}

rectangle::rectangle(point p1, point p2, point p3, point p4)
{
pt1.setpnt(p1);
pt2.setpnt(p2);
pt3.setpnt(p3);
pt4.setpnt(p4);
}

void perimeter(point pt1, point pt2)
{
cout << 2 * pt1.getDistance(pt2);

}


int main()

{
point pt1(2,3) , pt2(7,3), pt3(7,7), pt4(2,7);

rectangle box( pt1, pt2, pt3, pt4);

cout << "the distance is" << box.perimeter()<< endl;

return 0;
}

[/php]

I get this error

"D:\cpp2\question4_week5.cpp(101) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Error executing cl.exe."

any ideas?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>any ideas?
>void perimeter();
You can't print a void type.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>any ideas? >void perimeter(); You can't print a void type.

if I include it to int perimeter I dont know what to tell it to return :confused:


i put in return pt1.getDistance(pt2);

........

edit.......

[php]
int rectangle::perimeter(point pt1, point pt2)
{

return pt1.getDistance(pt2);
}
[/php]

D:\cpp2\question4_week5.cpp(88) : error C2511: 'perimeter' : overloaded member function 'int (class point,class point)' not found in 'rectangle'

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

When you change the definition of a member function, you need to change the declaration to match it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

ok, it works now but I'm sure its not ment to. Here what I have:

[php]

#include
#include

using namespace std;

class point
{
public:
point(int xcoord , int ycoord); //constructor
point(); // constructor

int getX(); //returns x
int getY(); //returns y

double getDistance( point otherPoint ); // get the distance of 2 points

void setpnt(point p); // creates link to rectangle

private:
int x, y; // store value of x cord and y here
};

class rectangle
{
public:
rectangle(point p1, point p2, point p3, point p4);

int perimeter(point p1, point p2, point p3, point p4);

private:
point pt1;
point pt2;
point pt3;
point pt4;
};


point::point()
{
x = 1;
y = 2;
}
point::point(int xcoord, int ycoord )
{
x = xcoord ;
y = ycoord ;
}

double point::getDistance( point otherPoint)
{
double distance;
int xdist = otherPoint.x - x;
int ydist = otherPoint.y - y;

distance = sqrt( ( xdist * xdist) + (ydist * ydist) );
return distance;

}


int point::getX()
{
return x;
}

int point::getY()
{
return y;
}

void point::setpnt(point p)
{
x = p.getX();
y = p.getY();
}

rectangle::rectangle(point p1, point p2, point p3, point p4)
{
pt1.setpnt(p1);
pt2.setpnt(p2);
pt3.setpnt(p3);
pt4.setpnt(p4);
}

int rectangle::perimeter(point pt1, point pt2, point pt3, point pt4)
{

return pt1.getDistance(pt2) ;

}


int main()

{
point pt1(2,3) , pt2(7,3), pt3(7,7), pt4(2,7);

rectangle box( pt1, pt2, pt3, pt4);

cout << "the distance is" << box.perimeter( pt1, pt2, pt3, pt4) << endl;

return 0;
}


[/php]

Now in int main, you will see the cout line. According to my teacher I'm not ment to be sending any paramenters ... could this be achieved ?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>rectangle box( pt1, pt2, pt3, pt4);
>box.perimeter( pt1, pt2, pt3, pt4)
Tell me, is this redundant? You pass the same parameters to perimeter as you do to the constructor. Looking at nothing but these two lines, I would assume that p1, p2, p3, and p4 are all saved in box so that they can be accessed later by member functions of rectangle. So you shouldn't need to pass them again to perimeter because it's a member function. This will work just as well:

int rectangle::perimeter() 
{  
  return pt1.getDistance(pt2); 
}

Though you should get a warning because getDistance returns a double and perimeter returns an int. That's a narrowing conversion that could lose data.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You