C++ passing an array of classes - c++

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.

Related

Passing constructor with parameters to another constructor as argument

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

How to define constructor in class with struct as data member in c++?

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.

Constructor can't access inherit class private int

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)
{}

C++ class method call in .cpp without ::

I'm new to progamming in C++. I have a good background with Java but C++ is different on many things and I have a question about one of the regarding .h and .cpp files.
I have the following files for a point object with x and y position:
Point.h
#ifndef POINT_H_
#define POINT_H_
class Point{
Point();
Point(int newX, int newY);
public:
int getX();
int getY();
void setX(int newX);
void setY(int newY);
void moveBy(int moveX, int moveY);
Point reverse();
private:
int x;
int y;
};
#endif
Point.cpp
#include "Point.h"
using namespace Point;
Point::Point(int newX, int newY){
x = newX;
y = newY;
}
int Point::getX(){
return x;
}
int Point::getY(){
return y;
}
void Point::setX(int newX){
x = newX;
}
void Point::setY(int newY){
y = newY;
}
void Point::moveBy(int moveX, int moveY){
x += moveX;
y += moveY;
}
Point Point::reverse(){
return Point(y,x);
}
I was wondering if there was a way of avoinding the Point::Point part like with std::cout by using namespace.
Thank you
You aren't required to separate your declaration and definition, and these functions are incredibly trivial. So including them in the class definition may actually allow the compiler to perform numerous additional optimizations.
So you could discard the .cpp entirely and the header becomes:
#ifndef POINT_H_
#define POINT_H_
class Point
{
int x_ { 0 };
int y_ { 0 };
public:
Point() = default;
Point(int x, int y) : x_(x), y_(y) {}
int getX() const { return x_; }
int getY() const { return y_; }
void setX(int x) { x_ = x; }
void setY(int y) { y_ = y; }
void moveBy(int x, int y) { x_ += x, y_ += y; }
Point reverse() const { return Point(y_, x_); }
};
#endif
But you can't avoid the "Point::" part when defining the members outside of the class declaration.
If you want to avoid typing the "Point::" in front of the getX, getY etc., then the answer is "no", unfortunately. In C++ and the name of any class (like "Point") is not a namespace, it's a scope.
What you can only do is inlining the method, defining into the class declaration.
class Point {
public:
void a_method() {
// all the code here!
}
};
You can't avoid "the Point::Point part," unless you declare the construction inline in the class declaration. The first "Point" defines the scope of the function, and the second "Point" is the name of the constructor function.
However, you could define the constructor(s) inline, like so:
class Point
{
Point()
{
x = 0;
y = 0;
}
Point(int newX, int newY);
{
x = newX;
y = newY;
}
// ...
};
Or:
class Point
{
Point() : x(0), y(0) {}
Point(int newX, int newY) : x(newX), y(newY) {}
// ...
};
Or:
class Point
{
Point(int newX = 0, int newY = 0) : x(newX), y(newY) {}
// ...
};

Named constructor idiom and new operator

I use the named constructor idiom to create objects, because I have lots of calls with identical parameters but the object shall be created differently.
The C++ FAQ tell us how to do this. It also tells us how to force objects being heap allocated. Yet it really fails to tell us how to use the named constructor idiom with the new operator.
Because new requires a constructor to be called we cannot directly call named constructors. So I found two workarounds to this problem:
I create an additional copy constructor and hope that optimizing compilers won't create a temporary object.
class point_t {
int X,Y;
point_t(int x, int y) : X(x), Y(y) { }
public:
point_t(const point_t &x) : X(x.X), Y(x.Y) { }
static point_t carthesian(int x, int y) { return point_t(x,y); }
static point_t polar(float radius, float angle) {
return point_t(radius*std::cos(angle), radius*std::sin(angle));
}
void add(int x, int y) { X += x; Y += y; }
};
int main(int argc, char **argv) {
/* XXX: hope that compiler doesn't create a temporary */
point_t *x = new point_t(point_t::carthesian(1,2));
x->add(1,2);
}
The other version is to create separate named constructors. Because function overloading doesn't work on return type I use two different names, which is ugly.
class point_t {
int X,Y;
point_t(int x, int y) : X(x), Y(y) { }
public:
/* XXX: function overloading doesn't work on return types */
static point_t carthesian(int x, int y) { return point_t(x,y); }
static point_t *carthesian_heap(int x, int y) { return new point_t(x,y); }
void add(int x, int y) { X += x; Y += y; }
};
int main(int argc, char **argv) {
point_t *x = point_t::carthesian_heap(1,2);
x->add(1,2);
}
Is there a prettier version that is equal to the example code?
You can avoid named constructor idiom for this completely, and do it using an additonal dummy enum parameter to select the constructor.
enum Carthesian {carthesian};
enum Polar {polar};
class point_t {
int X,Y;
public:
point_t(int x, int y) : X(x), Y(y) { } // may keep as a default
point_t(Carthesian, int x, int y) :X(x),Y(y){}
point_t(Polar, float radius, float angle)
: X (radius*std::cos(angle)), Y(radius*std::sin(angle)) {}
void add(int x, int y) { X += x; Y += y; }
};
int main(int argc, char **argv) {
point_t *x = new point_t(carthesian,1,2);
point_t *y = new point_t(polar,0,3);
x->add(1,2);
}
It is simple, portable, and the only overhead you will see is for the passing of the dummy enum values. In the rare case this overhead is too high for you it can be eliminated by wrapping a function call even when the construction itself is not inlined, as follows:
enum Carthesian {carthesian};
enum Polar {polar};
class point_t {
int X,Y;
void initCarthesian(int x, int y); // may be long, not inlined
void initPolar(float radius, float angle);
public:
point_t(int x, int y) : X(x), Y(y) { } // may keep as a default
point_t(Carthesian, int x, int y)
{initCarthesian(x,y);} // this is short and inlined
point_t(Polar, float radius, float angle) {initPolar(radius, angle);}
void add(int x, int y) { X += x; Y += y; }
};
Another approach is to use a derived class for construction. When using inner classes, it leads into quite a nice syntax I think:
class point_t {
int X,Y;
public:
struct carthesian;
struct polar;
point_t(int x, int y) : X(x), Y(y) { } // may keep as a default
void add(int x, int y) { X += x; Y += y; }
};
struct point_t::carthesian: public point_t
{
carthesian(int x, int y):point_t(x,y){}
};
struct point_t::polar: public point_t
{
polar(float radius, float angle):point_t(radius*std::cos(angle),radius*std::sin(angle)){}
};
int main(int argc, char **argv) {
point_t *x = new point_t::carthesian(1,2);
point_t *y = new point_t::polar(0,3);
x->add(1,2);
return 0;
}
You could write :
point_t *x = new point_t(point_t::carthesian(1,2));
It first calls carthesian() and then the copy-constructor.
Or, is there any problem in it? Perhaps, a bit slow?
By the way, there is one clear advantage in this code: the programmer can clearly see the new operator in his code (where he is using point_t written by someone else), so you can assume that its his responsibility to call delete once he is done with x.
Is this really a problem? In my experience classes tend to be either dynamically allocated most of the time or seldom, if at all. Classes that represent values, such as your point_t class here, belong to the second category, while classes that represent entities (i.e. something with identity) belong to the first one.
So my suggestion is to chose what you think is the best approach for each class and only provide that. Note that you could always return a small directly allocated object which has a private pointer to a larger one, as in the Handle-Body idiom.
On the other hand, other answers show how you may disambiguate among constructors that take arguments of the same number and types. In this line of thought, one alternative approach is to introduce specific types for the arguments as follows:
class radius_t {
float R;
public:
explicit radius_t(float r) : R(r) {}
operator float() const { return R; }
};
class angle_t {
float A;
public:
explicit angle_t(float a) : A(a) {}
operator float() const { return A; }
};
class point_t {
float X,Y;
public:
point_t(float x, float y) : X(x), Y(y) { }
point_t(radius_t radius, angle_t angle) :
X(radius*std::cos(angle)), Y((radius*std::sin(angle)) {
}
void add(int x, int y) { X += x; Y += y; }
};
int main(int argc, char **argv) {
point_t *x = new point_t(radius_t(1),angle_t(2));
x->add(1,2);
}
One approach that I haven't seen is overloading the constructor making the heap allocation use the last argument as an out one (Granted that the second function is not technically a constructor, it doesn't return an instance). The result would be something like (taken as base your second code fragment):
class point_t {
int X,Y;
point_t(int x, int y) : X(x), Y(y) { }
public:
/* XXX: function overloading doesn't work on return types */
static point_t carthesian(const int x, const int y) { return point_t(x,y); }
static void carthesian(const int x, const int y, point_t * & point) { point = new point_t(x,y); }
void add(int x, int y) { X += x; Y += y; }
void add(const point_t & point) { this->X += point.x; this->Y += point.y; }
};
int main(int argc, char **argv) {
point_t p1 = point_t::carthesion(1, 2);
point_t * p2;
point_t::carthesian(1, 2, p2);
p2->add(p1);
}
Can think of template allocator:
template<typename T>
struct Allocator : T
{
template<typename A1, typename A2>
Allocator(A1 a1, A2 a2) : T(a1, a2) {}
};
class point_t {
//...
template<typename T> friend struct Allocator;
};
int main(int argc, char **argv) {
point_t *x = new Allocator<point_t>(1,2);
x->add(1,2);
}
Now Allocator is friend of point_t. So it can access its private constructor. Also, you can add few more constructors like <A1, A2> inside Allocator to make it more generalized. Advantages are:
It doesn't look verbose.
You don't have to worry about compiler optimizations
The friendship is not exploited as, Allocator is a template
and we use it solely for heap allocation
Demo.