This is what I need to do: Add a new data member, string color to your Point2D class and a new getter and setter function for color. Create a Point2D object and set its color. Then create a Point3D color and try to set its color. Is this setColor behavior available for a Point3D class? Why or why not?
This is my code:
#include<iostream>
#include<vector>
using namespace std;
class Point2D
{
friend class SPALops;
protected:
float x;
float y;
protected:
float getX(){
return x;
};
float getY(){
return y;
};
void setX(float xc){
x = xc;
};
void setY(float yc){
y = yc;
};
Point2D(int xcoord, int ycoord){
x = xcoord;
y = ycoord;
};
};
class Point2D : Point2D
{
friend class SPALops;
public:
Point2D(int x, int y) : Point2D(x,y){}
Point2D() : Point2D(0,0){}
float getX(){
return this->Point2D::getX();
};
float getY(){
return this->Point2D::getY();
};
void setX(float x){
this->Point2D::setX(x);
};
void setY(float y){
this->Point2D::setY(y);
};
};
class RectangleImplementation{
friend class SPALops;
protected:
Point2D ll;
Point2D ur;
RectangleImplementation(float llx, float lly, float urx, float ury){
ll.setX(llx); ll.setY(lly);
ur.setX(urx); ur.setY(ury);
}
void setLLx(float x){
ll.setX(x);
};
void setLLy(float y){
ll.setY(y);
};
void setURx(float x){
ur.setX(x);
};
void setURy(float y){
ur.setY(y);
};
float getLLx(){
return ll.getX();
};
float getLLy(){
return ll.getY();
};
float getURx(){
return ur.getX();
};
float getURy(){
return ur.getY();
};
vector<vector<float> > getPointList(){
vector<vector<float> > v(4);
vector<float> llv(2); llv[0] = ll.getX(); llv[1] = ll.getY();
v[0] = llv;
vector<float> luv(2); luv[0] = ll.getX(); luv[1] = ur.getY();
v[1] = luv;
vector<float> ruv(2); ruv[0] = ur.getX(); ruv[1] = ur.getY();
v[2] = ruv;
vector<float> rlv(2); rlv[0] = ur.getX(); rlv[1] = ll.getY();
v[3] = rlv;
return v;
};
void printPointList(){
vector<vector<float>> v = this->getPointList();
cout << "ll = " << v[0][0] << " , " << v[0][1] << endl;
cout << "lu = " << v[1][0] << " , " << v[1][1] << endl;
cout << "ru = " << v[2][0] << " , " << v[2][1] << endl;
cout << "rl = " << v[3][0] << " , " << v[3][1] << endl;
};
};
class Rectangle : RectangleImplementation{
friend class SPALops;
public:
Rectangle(Point2D &p, Point2D &q) : RectangleImplementation(p.getX(), p.getY(), q.getX(), q.getY()){};
Rectangle(float llx, float lly, float urx, float ury): RectangleImplementation(llx,lly,urx,ury){};
float getLLx(){
return this->RectangleImplementation::getLLx();
};
float getLLy(){
return this->RectangleImplementation::getLLy();
};
float getURx(){
return this->RectangleImplementation::getURx();
};
float getURy(){
return this->RectangleImplementation::getURy();
};
void setLLx(float x){
this->RectangleImplementation::setLLx(x);
};
void setLLy(float y){
this->RectangleImplementation::setLLy(y);
};
void setURx(float x){
this->RectangleImplementation::setURx(x);
};
void setURy(float y){
this->RectangleImplementation::setURx(y);
};
void printPointList(){
cout << "In rectangle: " << endl;
cout << "ll = " << ll.getX() << " , " << this->RectangleImplementation::getLLy() << endl;
cout << "ru = " << this->getURx() << " , " << this->RectangleImplementation::getURy() << endl;
};
};
class SPALops{
public:
static bool touches(Rectangle &r, Point2D &p){
vector<vector<float> > v = r.RectangleImplementation::getPointList();
if((v[0][0] == p.getX() and v[0][1] == p.getY()) or
(v[1][0] == p.getX() and v[1][1] == p.getY()) or
(v[2][0] == p.getX() and v[2][1] == p.getY()) or
(v[3][0] == p.getX() and v[3][1] == p.getY()))
{
return true;
}
else
{
return false;
}
};
};
int main(){
Point2D p(10,10);
Point2D q(15,15);
Point2D s(20,20);
Point2D t(10,12);
Rectangle r(p,q);
r.printPointList();
cout << "Do rectangle 'r' and point 'p' touch = " << SPALops::touches(r,p) << endl;
cout << "Do rectangle 'r' and point 's' touch = " << SPALops::touches(r,s) << endl;
cout << "Do rectangle 'r' and point 't' touch = " << SPALops::touches(r,t) << endl;
return 0;
}
It seems I have many errors that stop me from running succesfully. I would appreciate if I get any feedback.
Here are some issues I found in your post:
1) Duplicate classes.
You have a Point2D class first, then a 2nd Point2D class.
Maybe you wanted the 2nd class to be Point3D.
2) Public Inheritance.
Your class Point2D: Point2D is private inheritance.
You may want to use public inheritance:
class Point3D : public Point2D
3) The this-> notation not needed.
When accessing members or functions of the class, access them directly:
int getX() const
{ return x; }
4) Don't duplicate parent functions.
No need to have parent functions repeated in the child class. For example, the child class doesn't need a getX method. This is what inheritance is for -- so you don't need to duplicate methods.
5) Child constructor calls parent constructor.
Your constructor should look something like this:
Point3D(int new_x, int new_y, int new_z)
: Point2D(new_x, new_y),
z(new_z)
{ ; }
Too many other errors to discuss in one post. The above should get you started and give you patterns to apply to other classes.
Related
I have a class
class player{
private:
int x;//coordinates
int y;
public:
player() {}
player(int px, int py) {
x = px;
y = py;
}
int get_x() {
return x;
}
int get_y() {
return y;
}
and a class avatar that inherit from class player:
class avatar :public player {
public:
avatar() {}
avatar (int px, int py) :player (px, py) {
cout << "Make a avatar in this coordinates" << " " << px << " " << py << endl;
}
I do
void start(avatar &av){
.
.
.
.
.
(in the end of the fuction)
cout << endl << avatar .get_x() << ", " << avatar.get_y();
}
int main()
avatar pl;
start(pl);
cout << endl << player.get_x() << ", " << player.get_y();
and the results i am getting are
6, 8(right answer)
-858993460, -858993460(after the function,wrong answer)
i dont understand why that happen. I use by reference function call(&)
There are many errors in the code you have shown.
But, the most important mistake that can cause the behavior you are experiencing is that you are default constructing an avatar object in main(), but neither of your default constructors in player or avatar are initializing the x and y data members, so they will have indeterminate values. That is why you see garbage in your output.
You need to initialize x and y, eg:
class player{
private:
int x;
int y;
public:
player() : x(0), y(0) {} // <-- HERE
...
};
Alternatively:
class player{
private:
int x;
int y;
public:
player(int px = 0, int py = 0) { // <-- HERE
x = px;
y = py;
}
...
};
Otherwise, you need to construct the avatar object in main() with initial values, eg:
int main()
avatar pl(6, 8);
...
}
I have a vector of class Point3D objects inside a class Figure3D. Function which changes coordinates of the Point3D object inside a vector, doesn't change coordinates of the Point3D object which is outside the vector.
Using function Figure3D::Pos() I see that the coordinates changed inside the vector after using function Figure3D::Move(), but using Point3D::full_pos() I see that the Point3D object still has its initial coordinates.
#include <vector>
#include <iostream>
#include <math.h>
#define PI acos(-1)
class Point3D {
public:
Point3D()
{
X = 0;
Y = 0;
Z = 0;
}
Point3D(double a, double b, double c) {
X = a;
Y = b;
Z = c;
};
void full_pos() {
std::cout << "Coordinates of the point are: X = " << X << " Y = " << Y << " Z = " << Z << std::endl;
}
void Move(double dx, double dy, double dz) {
X += dx;
Y += dy;
Z += dz;
}
private:
double X, Y, Z;
};
class Figure3D :public Point3D {
public:
Figure3D() {
f.reserve(10);
}
void AddPoint(Point3D *p) {
f.push_back(*p);
}
void Move(double x, double y, double z) {
for (auto it = f.begin(); it != f.end(); it++) {
it->Move(x, y, z);
}
}
void Pos() {
int i = 0;
for (auto it = f.begin(); it != f.end(); it++) {
cout << "Position of point " << i << " X: " << it->posX() << " Y: " << it->posY() << " Z: " << it->posZ() << std::endl;
i++;
}
}
private:
std::vector<Point3D> f;
};
int main() {
Point3D p1(1, 2, 3), p2(2, 2, 2), p3(5, 4, 7), p4(4, 9, 0);
Figure3D f1;
f1.AddPoint(&p1);
f1.AddPoint(&p2);
f1.AddPoint(&p3);
f1.AddPoint(&p4);
f1.Pos();
p1.full_pos();
f1.Move(10, 10, 10);
f1.Pos();
p1.full_pos();
}
Assuming you expect that the Point3D objects p1 to p4 in the main function to be modified as you modify the elements in the vector in the f1 object, then they wont.
The reson is in the AddPoint function where you do
f.push_back(*p);
The vector stores distinct objects, not pointers or references. This together with your use of the derefernece operator makes you store a copy of the object inside the vector. Modifying a copy will not modify the original.
Your AddPoint function is wrong. You are passing a pointer as an argument but then you are dereferencing a pointer and storing copies of Point3D object into your std::vector. Therefore, it should be:
void AddPoint(Point3D *p) {
f.push_back(p);
}
instead of
void AddPoint(Point3D *p) {
f.push_back(*p);
}
and
std::vector<Point3D*> f;
instead of
std::vector<Point3D> f;
In the c++ class below, I've provided default parameters to the constructor in case the user does not provide one. However, when I for example Point2d1 first(1, 0); in main() I get an error of no matching function call. I expected the behavior to default the 3rd parameter to 0?
.h
#ifndef POINT2D1_H_
#define POINT2D1_H_
class Point2d1 {
private:
int m_Object_ID;
double m_x;
double m_y;
public:
//Point2d1(int nID);
Point2d1(int nID, double x, double y);
virtual ~Point2d1();
void print() const;
friend double distanceFrom(const Point2d1& D1, const Point2d1& D2);
};
#endif /* POINT2D1_H_ */
.cpp
Point2d1::Point2d1(int nID = 0, double x = 0.0, double y = 0.0) : m_Object_ID(nID), m_x(x), m_y(y)
{
std::cout << "Constructing Point2d object " << nID << '\n';
}
Point2d1::~Point2d1() {
std::cout << "Destructing Object" << '\n';
}
void Point2d1::print() const
{
std::cout << "Point2d(" << m_x << ", " << m_y << ")\n";
}
double distanceFrom(const Point2d1& D1, const Point2d1& D2)
{
double distance = sqrt((D1.m_x - D2.m_x)*(D1.m_x - D2.m_x) + (D1.m_y - D2.m_y)*(D1.m_y - D2.m_y));
return distance;
}
Declare the default arguments in the member function declartion inside the class definition in the header file. Otherwise other compilation units will not know about the default arguments.
For example
.h
#ifndef POINT2D1_H_
#define POINT2D1_H_
class Point2d1 {
//...
public:
//Point2d1(int nID);
Point2d1(int nID = 0, double x = 0.0, double y = 0.0);
//...
};
#endif /* POINT2D1_H_ */
and
.cpp
Point2d1::Point2d1(int nID, double x, double y) : m_Object_ID(nID), m_x(x), m_y(y)
{
std::cout << "Constructing Point2d object " << nID << '\n';
}
The task is in the above code to write member function that calculate new point, which is amount of two other points. And i dont know how to return object or what should i do. Here is the code, and the function is marked with three !!!. The function must return something, i cant make it void because reference to void is unallowed.
class point {
private:
float x;
float y;
public:
point();
point(float xcoord, float ycoord);
void print();
float dist(point p1, point p2);
!!! float &add(point p1, point p2);
float &X();
float &Y();
~point();
};
float & point::X() { return x; }
float & point::Y() { return y; }
point::point() {
cout << "Creating POINT (0,0)" << endl;
x = y = 0.0;
}
point::point(float xcoord, float ycoord) {
cout << "Creating POINt (" << xcoord << "," << ycoord << ")" << endl;
x = xcoord;
y = ycoord;
}
void point::print() {
cout << "POINT (" << x << "," << y << ")";
}
float point::dist(point p1, point p2) {
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
!!!// float & point::add(point p1, point p2) {
point z;
z.X() = p1.X() + p2.X();
z.Y() = p1.Y() + p2.Y();
z.print();
}
point::~point() {
cout << "Deleting ";
print();
cout << endl;
}
int main() {
point a(3, 4), b(10, 4);
cout << "Distance between"; a.print();
cout << " and "; b.print();
cout << " is " << a.dist(a, b) << endl;
}
i make it ! here is what must be add function
//prototype
point &add(point& p1, point& p2);
//function itself
point & point::add(point& p1, point& p2) {
point z;
z.x = p1.X() + p2.X();
z.y = p1.Y() + p2.Y();
z.print();
return z;
}
Many thanks to ForceBru!! and all of you
What to do
You can return a point as well:
point point::add(const point& p1, const point& p2) {
point result;
result.x = p1.x + p2.x;
result.y = p1.y + p2.y;
return result;
}
Note that there's no need to use X() and Y() functions here since this method already has access to the private members.
It's also possible to do an operator overload
/*friend*/ point operator+ (const point& one, const point& two) {
// the code is the same
}
How to use it
int main() {
point one(2,5), two(3,6), three;
three.add(one, two);
std::cout << "Added " << one.print() << " and " << two.print();
std::cout << " and got " << three.print() << std::endl;
return 0;
}
Edit: as it was said in the comments, you shouldn't return a reference to an object created inside your add function since in such a situation you're allowed to return references to class members and to static variables only.
You can use Operator overloading here:
point point::operator+(const point & obj) {
point obj3;
obj3.x = this->x + obj.x;
return obj3;
}
returning object with Addition of two points.
for simple example :
class Addition {
int a;
public:
void SetValue(int x);
int GetValue();
Addition operator+(const Addition & obj1);
};
void Addition::SetValue(int x)
{
a = x;
}
int Addition::GetValue()
{
return a;
}
Addition Addition::operator+(const Addition &obj1)
{
Addition obj3;
obj3.a = this->a + obj1.a;
return obj3;
}
int _tmain(int argc, _TCHAR* argv[])
{
Addition obj1;
int Temp;
std::cout<<"Enter Value for First Object : "<<std::endl;
std::cin>>Temp;
obj1.SetValue(Temp);
Addition obj2;
std::cout<<"Enter Value for Second Object : "<<std::endl;
std::cin>>Temp;
obj2.SetValue(Temp);
Addition obj3;
obj3 = obj1 + obj2;
std::cout<<"Addition of point is "<<obj3.GetValue()<<std::endl;
return 0;
}
I want to use polymorphism in my program but don't know why when I create
virtual void setVertices()=0;
in class CFigure i get an error
C2259: 'CRectangle': cannot instantiate abstract class (line 63 and 74)
IntelliSense: object of abstract class type "CRectangle" is not allowed:
pure virtual function "CFigure:setVertices" has no overrider (line 63 and 74)
I want also to declare:
virtual void setVertices(CFigure& fig) = 0;
I don't know at all that if I can write CFigure& fig cuz CRectangle i have:
void setVertices(CRectangle& fig)
and those two methods have different parameters.
Can someone can tell me how to help me to explain those errors and tell me how to fix my program? Code:
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
class Point2D{
int x, y;
public:
void setX(int X){ x = X; }
void setY(int Y){ y = Y; }
int getX(){ return x; }
int getY(){ return y; }
};
class CFigure :public Point2D
{
protected:
Point2D Vert[4];
public:
CFigure(){}
//virtual void setVertices(CFigure& fig) = 0;
virtual void setVertices()=0;// if I comment this line all works good
};
class CRectangle : public CFigure
{
public:
CRectangle(){}
void setVertices(CRectangle& fig)
{
//CRectangle fig;
int x1, y1, a;
cout << "Give x1, y1" << endl;
cin >> x1 >> y1;
cout << "Give a" << endl;
cin >> a;
fig.Vert[0].setX(x1);
fig.Vert[0].setY(y1);
fig.Vert[1].setX(x1 + a);
fig.Vert[1].setY(y1);
fig.Vert[2].setX(x1);
fig.Vert[2].setY(y1 + a);
fig.Vert[3].setX(x1 + a);
fig.Vert[3].setY(y1 + a);
}
void showPoints()
{
CRectangle f;
setVertices(f);
for (int i = 0; i < 4; i++)
{
cout << "P" << i << "( " << f.Vert[i].getX() << " " << f.Vert[i].getY() << " ) " << endl;
}
}
};
int main()
{
CRectangle ag;
ag.showPoints();
return 0;
}
CFigure declares setVertices() as:
virtual void setVertices()=0;
But CRectangle declares setVertices() as:
void setVertices(CRectangle& fig)
The additional parameter makes it so CRectangle::setVertices() is not overriding CFigure::setVertices(). It is overloading it instead. That is why the compiler is complaining that CRectangle is an abstract class - it really is. When you override a virtual method, the signature of the overriding method must exactly match the signature of the method that is being overridden, eg:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Point2D
{
int x, y;
public:
void setX(int X){ x = X; }
void setY(int Y){ y = Y; }
int getX(){ return x; }
int getY(){ return y; }
};
class CFigure : public Point2D
{
protected:
Point2D Vert[4];
public:
CFigure() {}
virtual void setVertices()=0;
};
class CRectangle : public CFigure
{
public:
CRectangle() {}
void setVertices()
{
int x1, y1, a;
cout << "Give x1, y1" << endl;
cin >> x1 >> y1;
cout << "Give a" << endl;
cin >> a;
Vert[0].setX(x1);
Vert[0].setY(y1);
Vert[1].setX(x1 + a);
Vert[1].setY(y1);
Vert[2].setX(x1);
Vert[2].setY(y1 + a);
Vert[3].setX(x1 + a);
Vert[3].setY(y1 + a);
}
void showPoints()
{
setVertices();
for (int i = 0; i < 4; i++)
{
cout << "P" << i << "( " << Vert[i].getX() << " " << Vert[i].getY() << " ) " << endl;
}
}
};
int main()
{
CRectangle ag;
ag.showPoints();
return 0;
}