I have class Point, and i'm coding Circle. Then how to pass class as variable
int main() {
Point p1(0, 0), p2(5, 8);
Circle c1(p1, 4), c2(p2, 3);
int sgd = c1.getIntersection(c2);
cout<<"intersection: "<<sgd<<endl;
}
this is class Ponit
class Point {
private:
int _x;
int _y;
public:
Point() {
_x = 0;
_y = 0;
}
Point(int x, int y) {
_x = x;
_y = y;
return Point(_x, _y);
}
};
at this point the problem is located in the class circle,
you need to modify it so it can take a point as parameter:
class Circle
...public:
Circle(const Point& P, int s);
after that you can pass objects/instances of the class point
like
Circle c1(p1, 4), c2(p2, 3);
this approach will require that the Circle class "knows" about a Point class, this is called dependencies since the Circle depends on the Point...
you can as work-around, define a Circle constructor with the 2 params too
Circle(int x, int y, int s);
I found solution for it:
class Point {
public:
int _x;
int _y;
}
and in class Circle
Circle(Point x, int s);
Related
I have a struct Point, which has a constructor with parameters and a class called Circle.
struct Point{
int x, y;
Point(){}
Point(int ox, int oy) : x(ox),y(oy){}
};
class Circle{
public:
Point obj;
int radius;
Circle(Point pt(int ox, int oy), int raza) : obj.x(ox), obj.y(oy), radius(raza) {}
};
int main()
{
Circle(Point p(2,3),3);
return 0;
}
The problem is that I don't know how to pass a struct constructor with parameters as parameter to my Circle class constructor.
You can do that like this :
class Point
{
public:
Point(int x, int y) :
m_x(x),
m_y(y)
{
}
private:
int m_x{ 0 };
int m_y{ 0 };
};
class Circle
{
public:
Circle(const Point& pt, int raza) :
m_point{ pt },
m_radius{ raza }
{
}
private:
Point m_point;
int m_radius;
};
int main()
{
Circle c1(Point(2,3), 3);
// or use this shorter variant.
// first parameter is a Point, {2,3} looks for a constructor
// of Point with two ints of point and finds it.
Circle c2({ 2,3 }, 3);
return 0;
}
Circle(Point pt(int ox, int oy), int raza) : obj.x(ox), obj.y(oy), radius(raza) {}
is wrong syntax and should simply be:
Circle(Point pt, int raza) : obj(pt), radius(raza) {}
And then
Circle circle(Point(2, 3), 3);
What you intended could be done in the following ways
Circle(int ox, int oy, int raza) :
obj(ox, oy)
radius(raza)
{}
Or
Circle(Point const& pt, int raza) :
obj(pt), //note, here is the implicitly defined copy-constructor of obj called, not the one you defined
radius(raza)
{}
So If you define either of the constructors above the following will be valid
Circle crl(1,2,3); //valid for the first constructor
Circle crl(Point(1,2),3); //valid for the second constructor
Circle crl({1, 2}, 3); //valid for the second constructor
I made a struct as a data member in class. Now, I want to make a constructor to which I will pass my data members. How can I pass struct in constructor.
I wrote this code but it's not working.
#include <iostream>
#include <cmath>
using namespace std;
class circle
{
private:
struct center
{
int xcord;
int ycord;
};
struct center cordinate;
float radius;
public:
circle()
{
cordinate.xcord=0;
cordinate.ycord=0;
radius=0;
};
circle(int x, int y, float r) : cordinate.xcord(x), cordinate.ycord(y), radius(r)
{};
void showdata()
{
cout<<"Center(x,y) = ("<<cordinate.xcord<<","<<cordinate.ycord<<")"<<endl;
cout<<"Radius = "<<radius<<endl;
}
};
int main()
{
circle c1;
c1.showdata();
}
Or if I wrote it like this, it still don't work.
circle(int x, int y, float r) : center.xcord(x), center.ycord(y), radius(r)
{};
or
circle(int x, int y, float r) : center.cordinate.xcord(x), center.cordinate.ycord(y), radius(r)
{};
How can I pass struct to constructor?
You can initialize the cordinate member like this:
circle(int x, int y, float r) : cordinate{x, y}, radius{r} {}
Also, your default constructor can be simplified to:
circle() : circle(0, 0, 0) {}
which delegates to the 3-argument constructor. Even better, just use in class member initializers for all the fields:
struct center
{
int xcord = 0;
int ycord = 0;
} cordinate;
float radius = 0;
and then you can default the default constructor:
circle() = default;
Here's a demo.
I am getting a compilation error saying " ‘PointType’ is not a member of ‘Point’ " I do not know what I need to implement in my code. Been thinking for a while now. I've tried adjusting the codes here and there a few times. But could not think of a solution for this. I'm a little confused by what "Point point(Point::PointType(x, y), depth);" in the main() wants. What exactly is PointType(x,y)? Can anyone enlighten me with what I should do? Would appreciate anyone's help on this. Side Note: Main() can't be touched. Thanks!
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
struct PointType
{
float x;
float y;
PointType(const float x1, const float y1) :x(x1),y(y1){}
};
class Object
{
private:
float d;
PointType * pt;
public:
Object(float n) : d(n){}
float depth() const
{
return d;
}
};
class Point :public Object
{
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
virtual ~Point();
};
Main file:
const float EPSILON = 1e-5f;
bool is_near(float x, float y)
{
return std::abs(x - y) < EPSILON;
}
float frand()
{
return 10.0f * float(rand()) / float(RAND_MAX);
}
int main()
{
srand(unsigned(time(0)));
int count = 0;
int max_count = 0;
float x = frand();
float y = frand();
float depth = frand();
Point point(Point::PointType(x, y), depth);
if (is_near(point.depth(), depth))
{
++count;
}
else
{
std::cout << " - Point::depth test failed" << std::endl;
}
++max_count;
}
You've got a set circular declarations that can't be resolved if you can't change main(). You currently have PointType as a struct outside of the Point class. That's fine, but then you need to change the line in main() from:
Point point(Point::PointType(x, y), depth);
to:
Point point(PointType(x, y), depth);
But if you can't do that, then you can't ever compile this because Point is a subclass of Object, but Object requires a Point::PointType which can't have been defined yet. In order to have PointType be owned by Point, you need to declare it like this:
class Point :public Object
{
struct PointType
{
float x;
float y;
PointType(const float x1, const float y1) :x(x1),y(y1){}
};
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
virtual ~Point();
};
But once you put PointType into Point, you can't declare any member of Object to have a type of Point::PointType because Point inherits from Object.
You could declare Object::pt to be a void* but that throws away type information which is dangerous.
Class Point is working correctly, It is creating x, y point. Code:
point.h file
#ifndef POINT_H
#define POINT_H
namespace pt
{
class Point
{
int x, y;
public:
Point();
Point(int x, int y);
int getX();
int getY();
};
}
#endif // POINT_H
point.cpp file
#include "point.h"
pt::Point::Point()
{
this->x = this->y = 0;
}
pt::Point::Point(int x, int y)
{
this->x=x;
this->y=y;
}
int pt::Point::getX()
{
return this->x;
}
int pt::Point::getY()
{
return this->y;
}
Meanwhile when I try to create new Point3D class in main that will inherit from Point x, y coordinates and add z to create third dimension, new constructor cant get access to x, y of Point class. Errors are:
1. 'int pt::Point::x' is private at first and second this-> in Point3D constr.
2. Both are 'out of context'
main.cpp
#include <iostream>
#include "point.h"
int main()
{
class Point3D : public pt::Point
{
int z;
public:
getZ()
{
return this->z;
}
Point3D(int x ,int y, int z)
{
this->x=x;
this->y=y;
this->z=z;
}
};
return 0;
}
Thanks for help.
To make x and y accessible to derived classes, you should make them protected:
class Point
{
protected:
int x, y;
public:
Point();
Point(int x, int y);
int getX();
int getY();
};
By default, the visibility of a class member is private (note that this is different from the struct default where a struct member is public by default). On that topic, see this answer.
And as a side note, the idiomatic way to initialize x and y would be to write:
Point3D(int x ,int y, int z) : pt::Point(x,y)
{
this->z=z;
}
Then, you don't need to make x and y protected, they can remain private.
You can even write it like that:
Point3D(int x ,int y, int z) : pt::Point(x,y), z(z)
{}
Can anybody help me with the syntax of passing an array of classes
to another class. The syntax of passing an array of classes to another
class has got me beaten. class line tries to be initialised by an
array of points, but the prototype does not match.
#include <iostream>
using namespace std;
class point {
public:
point() {}
point(int x, int y) : X(x), Y(y) {}
void setXY(int x, int y) { X = x; Y = y; }
int getX() { return X; }
int getY() { return Y; }
private:
int X, Y;
};
class line {
public:
line(point *points, int); // Problem line.
private:
point *coords;
int numpoints;
};
int main() {
point points[3];
points[0].setXY(3, 5);
points[1].setXY(7, 9);
points[2].setXY(1, 6);
line l(points, 3); // Problem line.
return 0;
}
Error message:
cygdrive/c/Tmp/cc4mAXRG.o:a.cpp:(.text+0xa7): undefined reference to `line::line(point*, int)'
You need to define a constructor for your line class - you've only provided a declaration.
#include <iostream>
using namespace std;
class point {
public:
point() {}
point(int x, int y) : X(x), Y(y) {}
void setXY(int x, int y) { X = x; Y = y; }
int getX() { return X; }
int getY() { return Y; }
private:
int X, Y;
};
class line {
public:
line(point *points, int count)
: coords(points), numpoints(count) {}
private:
point *coords;
int numpoints;
};
int main() {
point points[3];
points[0].setXY(3, 5);
points[1].setXY(7, 9);
points[2].setXY(1, 6);
line l(points, 3);
return 0;
}
I'd recommend taking a look at the difference between definitions and declarations. Additionally, you should consider maintaining a std::vector<point> in your line class to manage the points. Your line class might then behave as:
#include <vector>
class line {
public:
line(std::vector<point> points)
: coords(points), numpoints(coords.size()) {}
private:
std::vector<point> coords;
int numpoints;
};
You didn't provide a definition for the constructor.
Try:
line(point *points, int np) : coords(points), numpoints(np) {}
Missing body of constructor "line".
You define prototype only.