c++ how to conserve vector items and double-precision - c++

I'm trying to fill a vector of an object Point 3D. My app read a csv file to load the vector by the three cordinate x, y, z. I use the type float.
This is my code.
main.cpp
int main(int argc, char** argv) {
char *theFileName = "file.csv"; //[100];
vector<Point> v = getPointCloud(theFileName);
for (int i = 0; i < v.size(); ++i) {
v.at(i).print(cout);
}
}
getPointCloud
vector<Point> getPointCloud(char *fileName) {
string line;
string token;
vector<Point> v;
double tab[3];
ifstream file(fileName);
if (file.is_open()) {
while (getline(file, line)) {
int cpt = 0;
stringstream stream(line);
while (getline(stream, token, ',')) {
tab[cpt] = ::atof(token.c_str());
cpt++;
}
Point p(tab[0], tab[1], tab[2]);
p.print(cout); <-- the display works
p.setColor(255, 0, 0);
v.push_back(p);
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
return v;
}
I have two problems:
1 - when I try to display points in the main method, I found that the three coordinates are null ( == 0) but in the displaying in the getPointCloud method works very well.
2 - Can someone give a simple method to conserve my coordinates without loss precision after mathematical operations. I have searched in the net but I don't understand haw to solve it. I'm newbie with c++.
Point.h
#ifndef POINT_H
#define POINT_H
#include <math.h>
#include <iostream>
class Point {
protected:
float x;
float y;
float z;
// color RGB
float r;
float g;
float b;
public:
// Constructors
Point();
// Point(const Point& orig);
Point(std::ostream &strm);
Point(float x, float y, float z);
Point(const Point& orig);
virtual ~Point();
//getters
float getX() const {
return this->x;
}
float getY() const {
return this->y;
}
float getZ() const {
return this->z;
}
float getR() const {
return this->r;
}
float getG() const {
return this->g;
}
float getB() const {
return this->b;
}
//setters
void setX(float x) {
this->x = x;
}
void setY(float y) {
this->y = y;
}
void setZ(float z) {
this->z = z;
}
void setR(float r) {
this->r = r;
}
void setG(float g) {
this->g = g;
}
void setB(float b) {
this->b = b;
}
void setColor(float r, float g, float b) {
this->r = r;
this->g = g;
this->b = b;
}
/**
* Print the point
* #param strm
*/
void print(std::ostream &strm);
//Other methods
float dist2D(Point &other);
float dist3D(Point &other);
Point swap(Point p);
// Point operator-(const Point &other) const;
};
#endif /* POINT_H */
Point.cpp
#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;
#include "Point.h"
Point::Point(const Point& orig) {
}
Point::Point(ostream &strm) {
strm << "Type the abscissa: ", cin >> this->x;
strm << "Type the ordinate: ", cin >> this->y;
strm << "Type the applicate: ", cin >> this->z;
}
Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
// The default point color is blue
this->r = 0;
this->g = 0;
this->b = 255;
}
/**
* Destructor
*/
Point::~Point() {
}
//Other methods
float Point::dist2D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
return sqrt(xd * xd + yd * yd);
}
float Point::dist3D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
float zd = z - other.z;
return sqrt(xd * xd + yd * yd + zd * zd);
}
Point Point::swap(Point p) {
Point aux(x, y, z);
x = p.x;
y = p.y;
z = p.z;
return aux;
}
//Point Point::operator-(const Point &other) const {
// return Point(other.getX() - this->x, other.getY() - this->y, other.getZ() - this->z);
//}
void Point::print(ostream &strm) {
strm << "Point(" << this->x << "," << y << "," << z << ")" << endl;
}
Thanks in advance.

Point::Point(const Point& orig) {
}
is incorrect.
It does not copy data from orig to *this
Please copy each of the member in this constructor.
This would look like this:
Point::Point(const Point& orig) {
x = orig.x ;
y = orig.y ;
x = orig.z ;
r = orig.r ;
g = orig.g ;
b = orig.b ;
}

Related

How do I simulate C#'s {get; set;} in C++?

I am experimenting with lambda functions and managed to recreate a "get" functionality in C++. I can get the return value of a function without using parentheses. This is an example class, where I implement this:
using namespace std;
struct Vector2 {
float x;
float y;
float length = [&]()-> float {return sqrt(x * x + y * y); }();
float angle = [&]()-> float {return atan2(y, x); }();
Vector2() : x(0), y(0) {}
Vector2(float a, float b) : x(a), y(b) {}
~Vector2() {}
Vector2(Vector2& other) : x(other.x), y(other.y) {}
Vector2(Vector2&& other) = delete;
void operator =(Vector2&& other) noexcept{
x = other.x;
y = other.y;
}
};
int main()
{
Vector2 vec = Vector2(10, 17);
printf("%f\n%f\n%f\n%f\n", vec.x, vec.y, vec.length, vec.angle);
}
However, I am currently trying to also recreate the "set" functionality that C# has. But I'm failing. I tried to add this:
void angle = [&](float a)->void {
float l = length;
x = cos(a) * l;
y = sin(a) * l;
};
But am getting "Incomplete type is not allowed" error. I'm not sure if that's how it should look, even if I wasn't getting the error. Is it even possible to recreate the "set" functionality C# has in C++?
I know that I can just use a method SetAngle(float a){...}, but that's not really the point.
TL;DR: Don't
What you have isn't a getter, it's just a normal data member that's calculated once when the object is initialized.
In general, C++ doesn't support C#-style properties. The usual C++-style solution is to just use a pair of member functions (and maybe a data member, if you need to save the value separately), i.e.
struct Vector2 {
// ...
float length() const { return sqrt(x * x + y * y); }
void length(float l) {
float angle = angle();
float new_x = l * cos(angle);
float new_y = l * sin(angle);
x = new_x;
y = new_y;
}
// ...
};
You can get something close to a C#-style property, but you'll always run into edge-cases where they don't work perfectly. For example, here's something that will work in many cases:
template <typename T>
class Property
{
private:
std::function<T()> getter_;
std::function<void(const T&)> setter_;
public:
Property(std::function<T()> getter, std::function<void(const T&)> setter)
: getter_{getter},
setter_{setter}
{}
operator T()
{
return getter_();
}
const T& operator=(const T& val)
{
setter_(val);
return val;
}
};
struct Vector2
{
float x;
float y;
Property<float> length{
[this]() { return sqrt(x * x + y * y); },
[this](float l) {
float new_x = l * cos(angle);
float new_y = l * sin(angle);
x = new_x;
y = new_y;
}
}
Property<float> angle{
[this]() { return atan2(y, x); },
[this](float a) {
float l = length;
x = cos(a) * l;
y = sin(a) * l;
}
}
// ...
};
int main() {
Vector2 v;
v.x = 1;
v.y = 1;
v.angle = std::numbers::pi / 2;
std::cout << "(" << v.x << ", " << v.y << ")\n";
}
But this still falls apart in the edge cases, especially when you mix it with templates and/or auto type-deduction. For instance:
Vector2 v;
v.x = 1;
v.y = 1;
auto old_angle = v.angle;
v.angle = std::numbers::pi / 2;
// oops, this prints pi/2, not pi/4 like you probably expected
// because old_angle isn't a float, it's a Property<float> that
// references v
std::cout << old_angle << '\n';
Note also that there's a bug here. Consider this:
int main() {
Vector2 v1;
v1.x = 1;
v1.y = 1;
Vector2 v2 = v1;
v2.angle = std::numbers::pi / 2;
// Oops, assigning to v2.angle modified v1
std::cout << "(" << v1.x << ", " << v1.y << ")\n";
}
You could work around these issues by making Property non-copyable, but then you force any class that uses it to implement a custom copy-constructor. Also, while that would make the auto case "safe", it does so by turning it into a compile error. Still not ideal.
I agree with Miles. This is not the greatest idea, because it's unnatural for C++ developers, and you should write code that is first and foremost easy to read.
However, as an engineering challenge, here's a possible implementation:
#include <math.h>
#include <iostream>
template <typename T>
class Member
{
public:
operator T() const { return _value; }
void operator =(const T& value) const { _value = value; } void operator =(T&& value) { _value = std::move(value); }
private:
T _value;
};
class Angle
{
public:
Angle(const Member<float>& x, const Member<float>& y) :
_x(x), _y(y) {}
operator float() const { return atan2(_y, _x); }
private:
const Member<float>& _x, _y;
};
class Obj
{
public:
Member<float> x, y;
Angle angle;
Obj() : angle(this->x, this->y) {}
};
int main()
{
Obj o;
o.x = 3;
o.y = 5;
std::cout << o.x << ", " << o.y << " -> " << o.angle << std::endl;
}
While other solutions also seem to be possible, this one seems to be the most elegant :P
using namespace std;
struct Vector2 {
float x;
float y;
float init_length = [&]()-> float {return sqrt(x * x + y * y); }();
float init_angle = [&]()-> float {return atan2(y, x); }();
__declspec(property(get = GetAngle, put = SetAngle)) float angle;
__declspec(property(get = GetLength, put = SetLength)) float length;
Vector2() : x(0), y(0) {}
Vector2(float a, float b) : x(a), y(b) {}
~Vector2() {}
Vector2(Vector2& other) : x(other.x), y(other.y) {}
Vector2(Vector2&& other) = delete;
void operator =(Vector2&& other) = delete;
void Display() {
printf("%f\n%f\n%f\n%f\n\n", x, y, length, angle);
}
float GetLength() {
return sqrt(x * x + y * y);
}
float GetAngle() {
return atan2(y, x);
}
void SetLength(float l) {
float a = GetAngle();
x = cos(a) * l;
y = sin(a) * l;
}
void SetAngle(float a) {
float l = GetLength();
x = cos(a) * l;
y = sin(a) * l;
}
};
int main()
{
Vector2 vec = Vector2(10, 17);
vec.Display();
vec.length = 5;
vec.Display();
}

Count how many times class member was printed

I need to count how many times class members were printed using function Print which is inspector. Constructor should set private elements of class.
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d();
Vector3d(double x, double y, double z);
void Print() const;
int GetCount() const;
};
Vector3d::Vector3d() {
count = 0;
}
Vector3d::Vector3d(double x, double y, double z) {
count = 0;
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
I used mutable int to enable changing element of const function. For v1.GetCount() I get output 3, which is correct. However, for v2.GetCount() I get output 5, which is wrong (correct is 2).
Could you help me to fix this? Where am I making mistake?
You need to overload copy constructor and copy assign operator for Vector3d class.
Now you are copying state of count field into v2 object, therefore it starts from 3 not from 0.
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d(double x, double y, double z);
Vector3d(const Vector3d&);
Vector3d& operator=(const Vector3d&);
Vector3d(Vector3d&&) = delete;
Vector3d& operator=(Vector3d&&) = delete;
void Print() const;
int GetCount() const;
};
Vector3d::Vector3d(double x, double y, double z) {
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d& Vector3d::operator=(const Vector3d& that)
{
x = that.x;
y = that.y;
z = that.z;
return *this;
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
UPDATE:
Someone mentioned that explicitly deleted move ctor and operator is not ok, I understand that, but for me it is not clear should we move counter to other instance or not. Therefore here possible implementation:
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d(double x, double y, double z);
Vector3d(const Vector3d&);
Vector3d(Vector3d&&);
Vector3d& operator=(Vector3d);
void Print() const;
int GetCount() const;
private:
void swap(Vector3d&);
};
Vector3d::Vector3d(double x, double y, double z) {
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d::Vector3d(Vector3d&& that)
: Vector3d(that.x, that.y, that.z)
{
count = that.count;
}
Vector3d& Vector3d::operator=(Vector3d that)
{
swap(that);
return *this;
}
void Vector3d::swap(Vector3d& that)
{
std::swap(x, that.x);
std::swap(y, that.y);
std::swap(z, that.z);
std::swap(count, that.count);
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2 = std::move(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
But these more for commenters than for question author.

Class square finding of axis

I am trying to figure out how to find the axis of class Square's axis as shown below? But I've been trying for hours and still did not managed to solve it. Can someone with their high level expertise show me the ropes to do it? Because both center function call and axis function call in main() does call the same x() and y(), hence brought me to a state of confusion. I know the inheritance of square from circle is weird. But it is what my school wants. Note: Main() CANNOT be modified! Thanks!
Output:
Square::axis test failed
8.87627 0.284967
3.82567 0.958537
Tests passed: 50%
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
class Object
{
public:
private:
float d;
public:
Object(float n) : d(n){}
Object(){}
float depth() const
{
return d;
}
struct PointType
{
float x2;
float y2;
PointType( float x1, float y1) :x2(x1),y2(y1){}
PointType(){}
float x()
{
return x2;
}
float y()
{
return y2;
}
PointType center()
{
return *this;
}
};
struct VectorType
{
float tx;
float ty;
VectorType( float tx1, float ty1) :tx(tx1),ty(ty1){}
VectorType( ){}
};
virtual ~Object()
{}
};
class Point :public Object
{
private:
PointType mpoint;
public:
Point(const PointType& pt, float& y1) :Object(y1), mpoint(pt) {}
Point(const PointType& pt):mpoint(pt){}
Point(){}
Point center() const
{
return *this;
}
float x()
{
return mpoint.x2;
}
float y()
{
return mpoint.y2;
}
virtual ~Point(){}
};
class Circle : public Point
{
private:
Object::PointType m_pt;
float r;
public:
Circle(const PointType pts, float rad, float dep)
: Point(m_pt,dep),m_pt(pts),r(rad) {}
Circle(const PointType pts, float rad):m_pt(pts),r(rad){}
Circle(){}
float radius() const
{
return r;
}
Circle center() const
{
return *this;
};
float x()
{
return m_pt.x2;
}
float y()
{
return m_pt.y2;
}
};
class Square: public Circle
{
private:
Object::PointType s_pt;
Object::VectorType v_pt;
float a=getRadius();
public:
Square(const PointType spts,const VectorType vpts,float depth) :
Circle(spts,a,depth),s_pt(spts),v_pt(vpts){}
Square(const Object::PointType& spts, const VectorType vpts):s_pt(spts),v_pt(vpts){}
Square axis() const
{
return Square(s_pt,v_pt,getRadius());
}
Square center() const
{
return *this;
}
float radius() const
{
return a;
}
float getRadius() const
{
float rad= sqrt(v_pt.tx * v_pt.tx + v_pt.ty * v_pt.ty);
return rad;
}
float x() const
{
return s_pt.x2;
// v_pt.tx/radius();
}
float y() const
{
return s_pt.y2;
// v_pt.ty/radius();
}
};
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 sx = frand();
float sy = frand();
float depth = frand();
Square square(Square::PointType(x, y), Square::VectorType(sx, sy), depth);
if (is_near(square.center().x(), x) &&
is_near(square.center().y(), y))
{
++count;
}
else
{
std::cout << " - Square::center test failed" << std::endl;
}
++max_count;
float radius = std::sqrt(sx * sx + sy * sy);
if (is_near(square.axis().x(), sx / radius) &&
is_near(square.axis().y(), sy / radius))
{
++count;
}
else
{
std::cout << " - Square::axis test failed" << std::endl;
}
++max_count;
std::cout << square.axis().x()<< " " << sx / radius<<std::endl;
std::cout << square.axis().y()<< " " << sy / radius<<std::endl;
int result = static_cast<int>(
100.0f * static_cast<float>(count) / static_cast<float>(max_count) + 0.5f
);
std::cout << "Tests passed: " << result << "%" << std::endl;
return result;
}

Function type error c++

I am creating a class to print a point, compare two points to see if they are equal, and to find the distance between two points using separate methods for each. The method for finding the distance between two points is giving me a type error and I don't know why.
#include <iostream>
using namespace std;
class Point
{//in C++ stuff is private by default
public:
Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }
void print() { cout << "(" << x << "," << y << ")\n" << endl; }
double getX() { return x; };
double getY() { return y; };
bool compare(Point other) { return (x == other.x && y == other.y); }
void setX(double a)
{if (a >= 0)
{x = a;}};
void setY(double b)
{if (b >= 0)
{y = b;}};
double distance(Point point1, Point point2)
{
return(sqrt (pow (point1.getX-point2.getX,2) + pow(point1.getY-point2.getY,2)));
};
private:
double x, y;
};
bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }
int main()
{
Point p1(5,1);
Point p2;
p2.setX(2);
p2.setY(5);
p1.print();
p2.print();
p1.getX();
p1.getY();
p2.getX();
p2.getY();
p1.setX(3.5);
p1.setY(9);
p1.print();
p1.compare(p2);
//or p2.equals(p1);
distance(p1, p2);
cout << "This distance b/w p1 & p2 is:" << distance (p2, p1) << endl;
}
You have to call the methods getX and getY by adding () after each name:
return(sqrt(pow(point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2)));
Otherwise you will be subtracting pointers to functions, which isn't allowed.
#include <iostream>
#include <math.h>
using namespace std;
class Point
{//in C++ stuff is private by default
public:
Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }
void print() { cout << "(" << x << "," << y << ")\n" << endl; }
double getX() { return x; };
double getY() { return y; };
bool compare(Point other) { return (x == other.x && y == other.y); }
void setX(double a)
{if (a >= 0)
{x = a;}};
void setY(double b)
{if (b >= 0)
{y = b;}};
static double distance1(Point point1, Point point2)
{
return(sqrt (pow (point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2)));
};
private:
double x, y;
};
bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }
int main()
{
Point p1(5,1);
Point p2;
p2.setX(2);
p2.setY(5);
p1.print();
p2.print();
p1.getX();
p1.getY();
p2.getX();
p2.getY();
p1.setX(3.5);
p1.setY(9);
p1.print();
p1.compare(p2);
//or p2.equals(p1);
//distance(p1, p2);
cout << "This distance b/w p1 & p2 is:" << Point::distance1 (p2, p1) << endl;
}
Replace distance function from class. In your case std::distance is called. And try do not use using namespace std; in your code at all.

How to make a good set/get method

For example, we have this class:
class Coord
{
double x;
double y;
double z;
public:
Coord() { x = y = z = 0; }
void set(double xx, double yy, double zz)
{
x = xx;
y = yy;
z = zz;
}
void set_x(double xx) { x = xx; }
void set_y(double yy) { y = yy; }
void set_z(double zz) { z = zz; }
double get_x() { return x; }
double get_y() { return y; }
double get_z() { return z; }
};
On these 7 methods we can set and get x,y and z of a coordinate. I am interested in create less methods set() and get() where I can call something like that:
int main()
{
Coord c;
c.set_x(5); /* only set x */
c.set_y(6); /* or y */
c.set_z(7); /* or z */
c.set(1,2,5); /* or setting x, y and z */
c.get_x(); /* only get x */
c.get_y(); /* or y */
c.get_z(); /* or z */
}
If the Coord class is that simple, it could also be a struct.
Anyway you can write something like:
class Coord
{
public:
enum xyz {x = 0, y, z};
Coord() : vec{x, y, z} {}
template<xyz C> void set(double v) { vec[C] = v; }
template<xyz C> double get() const { return vec[C]; }
void set(double xx, double yy, double zz)
{
set<Coord::x>(xx);
set<Coord::y>(yy);
set<Coord::z>(zz);
}
private:
double vec[z + 1];
};
and use the class this way:
Coord c;
c.set<Coord::x>(5); /* only set x */
c.set<Coord::y>(6); /* or y */
c.set<Coord::z>(7); /* or z */
c.set(1,2,5); /* or setting x, y and z */
c.get<Coord::x>(); /* only get x */
c.get<Coord::y>(); /* or y */
c.get<Coord::z>(); /* or z */
getters and setters are meant to protect your data and provide encapsulation.
For example they allow you to add side effects to getting and setting operations (such as writing to a log), or allow you to catch invalid values early before they cause horrible problems later (For example preventing values greater than n being set).
Here's a brief and contrived setter example:
void set_x(int x)
{
// prevent an invalid value for x
if( x > 11 ) x = 11;
// set x
this.x = x;
// log the operation
log("user set x to {0}", x);
}
Assuming your c.set.x(5) example is not using some whacky preprocessor macros, it would require that the Coord class have a member variable called set with methods
x()
y()
z()
This would require just as much code as writing a set_x(), set_y() and set_z() method in your Coord class, but the methods would not belong to the class Coord, instead belonging to another class that is itself used as a member variable of Coord. Doing so would not really make any logical sense... the x, y, and z values belong to Coord and operations on them are operations on the Coord.
Furthermore the methods x() y() and z() would no longer obey the general principle of making methods verbs. Anyone reading the class with those methods would have no idea what function z() is supposed to do!
It would also create a refactoring nightmare: If for example in the future a business requirement appeared that meant no Coords could ever have values of x greater than 21 somone maintaining your code would have to change a class that is a member of Coord rather than the Coord class itself.
Encapsulation with getter and setter methods is often a really good idea and in C++ with the benefit of inlining it can even add no runtime overhead. But keep to the principle "Make everything as simple as possible, but not simpler." In other words get_x() and set_x() are widely understood, useful, easily refactored, convenient, self-documenting and performant... other approaches are likely to be less so.
First: Your usage of c.set.x would not work because you would call a public element set on your object c and where set has a public element x.
I find both classes lack standards of clean code and usual style of getter and setter - even without specifying any language.
A usual way would be to create the following:
class Coord
{
double x;
double y;
double z;
public:
Coord() {
x = 0;
y = 0;
z = 0;
}
Coord(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
void setX(double x) { this.x = x; }
void setY(double y) { this.y = y; }
void setZ(double z) { this.z = z; }
double getX() { return x; }
double getY() { return y; }
double getZ() { return z; }
};
Though some prefer to use m_x as setter variable parameter or any other convention.
Anyhow everyone would directly understand your code. Is able to set and get coordinate values for x, y, z and it would look pretty standard-default-behaviour if someone does following:
Coord P(10, 15, 20);
std::cout << P.getX() << " " << P.getY() << std::endl;
P.setX(-10);
P.setZ(40);
You've already gotten some good answers, but you could also use an enum if you really want a syntax with fewer setters and getters. The client syntax can get a little clunky and awkward, but it of course depends on what you're looking for!
#include <iostream>
class Coord {
public:
enum Axis {
X = 0,
Y,
Z,
NUM_AXES
};
// Using extended initializer lists (C++11)
Coord() : axes_{0, 0, 0} {}
Coord(double x, double y, double z) : axes_{x, y, z} {}
void set(Axis a, double d) {
axes_[a] = d;
}
double get(Axis a) const {
return axes_[a];
}
private:
double axes_[NUM_AXES];
// Copy constructor and assgn. operator included for good measure
Coord(const Coord &);
void operator=(const Coord &);
};
int main()
{
Coord c(1, 2, 3);
std::cout << "X: " << c.get(Coord::X) << std::endl;
std::cout << "Y: " << c.get(Coord::Y) << std::endl;
std::cout << "Z: " << c.get(Coord::Z) << std::endl;
c.set(Coord::Y, 4);
std::cout << "Y: " << c.get(Coord::Y) << std::endl;
return 0;
}
This strange code does exactly what you ask - just C++ fun. Don't do this!
#include <iostream>
using namespace std;
class Coord {
double x;
double y;
double z;
public:
class Setter {
public:
Setter(Coord& coord) : c(coord) {}
void x(double value) { c.x = value; }
void y(double value) { c.y = value; }
void z(double value) { c.z = value; }
void operator()(double x, double y, double z) { c.x = x; c.y = y; c.z = z; }
private:
Coord& c;
};
class Getter {
public:
Getter(Coord& coord) : c(coord) {}
double x() { return c.x; }
double y() { return c.y; }
double z() { return c.z; }
private:
Coord& c;
};
Setter set;
Getter get;
Coord() : set(*this), get(*this) { x = y = z = 0; }
friend class Setter;
};
int main()
{
Coord c;
cout << c.get.x() << " " << c.get.y() << " " << c.get.z() << endl;
c.set.x(1);
c.set.y(2);
c.set.z(3);
cout << c.get.x() << " " << c.get.y() << " " << c.get.z() << endl;
c.set(5, 6, 7);
cout << c.get.x() << " " << c.get.y() << " " << c.get.z() << endl;
return 0;
}
Output:
0 0 0
1 2 3
5 6 7
The more or less standard way to expose this, if validation is not a concern, is to return mutable references from the non-const accessor, and values from the const one. This allows you to separate the interface from storage, while not making the syntax too heavy.
private:
double m_x, m_y, m_z;
public:
double & x() { return m_x; }
double & y() { return m_y; }
double & z() { return m_z; }
double x() const { return m_x; }
double y() const { return m_y; }
double z() const { return m_z; }
This will allow c.x() to obtain the value of the x coordinate whether the Coord object is const or not, and allows setting the value using the syntax c.x() = value.
In the interest of completeness, you can get exactly the syntax you want using the following code, but I would strongly recommend against it. It is a lot of extra code, provides no real benefit, and creates a syntax that is uncommon and most programmers will not find it intuitive.
The technique creates two nested classes getters and setters and exposes instances of them as public members of Coord.
This is provided as an example of how to achieve the result you asked for, but I do not recommend this approach.
class Coord
{
private:
double x, y, z;
public:
Coord();
Coord(double, double, double);
class setters {
friend class Coord;
private:
explicit setters(Coord &);
public:
setters(setters const &) = delete;
setters & operator=(setters const &) = delete;
void x(double) const;
void y(double) const;
void z(double) const;
private:
Coord & coord;
};
friend class setters;
class getters {
friend class Coord;
private:
explicit getters(Coord const &);
public:
getters(getters const &) = delete;
getters & operator=(getters const &) = delete;
double x() const;
double y() const;
double z() const;
private:
Coord const & coord;
};
friend class getters;
setters const set;
getters const get;
};
Coord::Coord() : x(0), y(0), z(0), set(*this), get(*this) { }
Coord::Coord(double px, double py, double pz) : x(px), y(py), z(pz), set(*this), get(*this) { }
Coord::setters::setters(Coord & c) : coord(c) { }
void Coord::setters::x(double px) const {
coord.x = px;
}
void Coord::setters::y(double py) const {
coord.y = py;
}
void Coord::setters::z(double pz) const {
coord.z = pz;
}
Coord::getters::getters(Coord const & c) : coord(c) { }
double Coord::getters::x() const {
return coord.x;
}
double Coord::getters::y() const {
return coord.y;
}
double Coord::getters::z() const {
return coord.z;
}
(Demo)
Actually the function can be reduce based on your requirement as follows,
class Coord
{
double x;
double y;
double z;
public:
Coord() {
x = 0;
y = 0;
z = 0;
}
void GetValues(double* x=NULL, double* y=NULL, double* z=NULL);
void SetValues(double x=0, double y=0, double z=0)
/* You can use constructors like below to set value at the creation of object*/
Coord(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
/*You can set the values of x, y & z in a single function as follows. It can be used at any time without restriction */
void SetValues(double x, double y, double z)
{
if(x > 0) //It is optional to use condition so that you can update any one variable aloen by sending other two as ZERO
{
this.x = x;
}
if(y > 0)
{
this.y = y;
}
if(z > 0)
{
this.z = z;
}
}
/*You can Get the values of x, y & z in a single function as follows. Pass By Reference id the concept you need */
void GetValues(double* x, double* y, double* z)
{
if(x != NULL) //It x is not null.
{
x = this.x;
}
if(y != NULL)
{
y = this.y;
}
if(z != NULL)
{
z= this.z;
}
}
};
while calling you can call like the following,
SetValues(10, 20, 0); //To set x and y values alone.
double x1 = 0;double y1 = 0;double z1 = 0;
GetValues(&x1, &y1, &z1)//It will return the values x1 y1 and z1 as 10, 20 & 0
You cannot do exactly what you want.
In
c.set.x(5); /* only set x */
the c.set subexpression is retrieving a field set from c (unless set is a #define-d macro, but that would be silly).