| | |
OOP any chance of assistance? Its about areas etc
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
[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
#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
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:
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:
In your case, the line in question should be changed to:
Alternatively you could say:
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.
C++ Syntax (Toggle Plain Text)
class C { public: static void foo(); }; // ... C::foo();
C++ Syntax (Toggle Plain Text)
class C { public: void foo(); }; // ... C obj; obj.foo();
C++ Syntax (Toggle Plain Text)
cout << "the distance is" << pt1.getDistance(pt2);
C++ Syntax (Toggle Plain Text)
cout << "the distance is" << pt2.getDistance(pt1);
New members chased away this month: 4
•
•
•
•
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;
New members chased away this month: 4
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
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?
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by Narue
>any ideas?
>void perimeter();
You can't print a void type.

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'
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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 ?
[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 ?
>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:
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.
>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)
int rectangle::perimeter() { return pt1.getDistance(pt2); }
New members chased away this month: 4
![]() |
Other Threads in the C++ Forum
- Previous Thread: confused about turbo c++ graphics.h
- Next Thread: Intracting ODBE with C++
Views: 1758 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






