944,001 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1926
  • C++ RSS
Mar 6th, 2005
1

OOP any chance of assistance? Its about areas etc

Expand Post »
[php]
#include <iostream>
#include <cmath>

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
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 6th, 2005
1

Re: OOP any chance of assistance? Its about areas etc

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:
C++ Syntax (Toggle Plain Text)
  1. class C {
  2. public:
  3. static void foo();
  4. };
  5.  
  6. // ...
  7. C::foo();
If the member function is not declared as static then you must call it using the member access operator and an object of the class:
C++ Syntax (Toggle Plain Text)
  1. class C {
  2. public:
  3. void foo();
  4. };
  5.  
  6. // ...
  7. C obj;
  8. obj.foo();
In your case, the line in question should be changed to:
C++ Syntax (Toggle Plain Text)
  1. cout << "the distance is" << pt1.getDistance(pt2);
Alternatively you could say:
C++ Syntax (Toggle Plain Text)
  1. 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 6th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

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

Re: OOP any chance of assistance? Its about areas etc

Quote originally posted by Acidburn ...
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

Quote originally posted by Narue ...
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 <iostream>
#include <cmath>

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?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 7th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

>any ideas?
>void perimeter();
You can't print a void type.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

Quote originally posted by Narue ...
>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


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'
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 7th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

When you change the definition of a member function, you need to change the declaration to match it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 8th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

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

[php]

#include <iostream>
#include <cmath>

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 ?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 8th, 2005
0

Re: OOP any chance of assistance? Its about areas etc

>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:
C++ Syntax (Toggle Plain Text)
  1. int rectangle::perimeter()
  2. {
  3. return pt1.getDistance(pt2);
  4. }
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: confused about turbo c++ graphics.h
Next Thread in C++ Forum Timeline: Intracting ODBE with C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC