OOP any chance of assistance? Its about areas etc

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

OOP any chance of assistance? Its about areas etc

 
1
  #1
Mar 6th, 2005
[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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
1
  #2
Mar 6th, 2005
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:
  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:
  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:
  1. cout << "the distance is" << pt1.getDistance(pt2);
Alternatively you could say:
  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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

 
0
  #3
Mar 6th, 2005
Hello thanks for the assitance... I still can;t get this line of code to work... //cout << "perimeter is : " << box.perimeter() << endl;
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #4
Mar 6th, 2005
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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

 
0
  #5
Mar 7th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #6
Mar 7th, 2005
>any ideas?
>void perimeter();
You can't print a void type.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

 
0
  #7
Mar 7th, 2005
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'
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #8
Mar 7th, 2005
When you change the definition of a member function, you need to change the declaration to match it.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

 
0
  #9
Mar 8th, 2005
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 ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #10
Mar 8th, 2005
>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:
  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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1758 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC