I am having issues displaying coordinates that are read into a class. This is my first time using classes so please be understanding!
Here is what I have so far:
#include <iostream>
using namespace std;
class Vector{
private:
float x;
float y;
public:
Vector(float f1, float f2)
{
x=f1;
y=f2;
}
Vector(){}
float display()
{
Vector v;
cout << "(" << v.x << "," << v.y << ")" << endl;
return 0;
}
};
int main()
{
Vector v1(0.5, 0.5);
cout << "v1 ";
v1.display();
system("pause");
return 0;
}
It prints
v1 (-1.07374e+008,-1.07374e+008)
Your problem is that you are not printing out the coordinates of the Vector you created in main() but a default created one in your function. Instead of
float display()
{
Vector v;
cout << "(" << v.x << "," << v.y << ")" << endl;
return 0;
}
You need
float display()
{
//Vector v; remove this
cout << "(" << x << "," << y << ")" << endl;
// no v.x no v.y
return 0;
}
I suggest you change the default constructor to
Vector() : x(0), y(0) {}
So it would have printed
v1 (0,0)
You should also change
Vector(float f1, float f2)
{
x=f1;
y=f2;
}
To
Vector(float f1, float f2) : x(f1), y(f2) {}
As it is a good habit to get into. This can save resources and CPU cycles when dealing with non POD types. For more information see Why should I prefer to use member initialization list?
the Vector v; line is a mistake. You are basically creating a new uninitialized vector instead of crating your own instance.
one correction would be:
int display()
{
cout << "(" << x << "," << y << ")" << endl;
return 0;
}
since x and y are member of this class
Related
So basically I created two classes for two vectors and got there norms. How would I approach using both classes to find the distance between both vectors in my main function ? I know I can use the friend function but we have not been taught that in class so I have to use the scope :: operator and constructor and destructors. I been trying different things but nothing works any ideas ? I am fairly new to c++.
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
class Vector {
private:
float x;
float y;
float z;
public:
Vector(float xaxis, float yaxis, float zaxis)
{
x = xaxis;
y = yaxis;
z = zaxis;
}
float getx() { return x; }
float gety() { return y; }
float getz() { return z; }
float normVector()
{
float result = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
return result;
}
};
class Vectortwo {
private:
float x;
float y;
float z;
public:
Vectortwo(float xaxis, float yaxis, float zaxis)
{
x = xaxis;
y = yaxis;
z = zaxis;
}
float getx() { return x; }
float gety() { return y; }
float getz() { return z; }
float normVectortwo()
{
float result = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
return result;
}
};
int main()
{
Vector v(2, 2, 2);
v.getx();
v.gety();
v.getz();
v.normVector();
cout << " X axis" << v.getx() << endl;
cout << " Y axis" << v.gety() << endl;
cout << " Z axis" << v.getz() << endl;
cout << " The norm of our first vector " << v.normVector() << endl;
Vectortwo b(3, 3, 3);
b.getx();
b.gety();
b.getz();
b.normVectortwo();
cout << " X axis" << b.getx() << endl;
cout << " Y axis" << b.gety() << endl;
cout << " Z axis" << b.getz() << endl;
cout << " The norm of our first vector " << b.normVectortwo() << endl;
return 0;
}
I know I can use the friend function but we have not been taught that in class [...]
You can always use getx(), gety() and getz() directly to calculate the distance. Since this gets a bit tiresome to write more than once you can write a little function that wraps the calculation:
float distance(Vector v1, Vectortwo v2)
{
return sqrt(
pow(v1.getx() - v2.getx(), 2)
+ pow(v1.gety() - v2.gety(), 2)
+ pow(v1.getz() - v2.getz(), 2)
);
}
It has to be declared before main() (but after Vector and Vectortwo) and could be used like this:
cout << "The distance is " << distance(v, b) << " units." << endl;
Since you somehow declared to vector classes Vector and Vectortwo that are basically identical (apart from the name), you can also just remove Vectortwo and use Vector for both instances, v and b. That would simplify the code and both parameters of the distance() function could be of type Vector.
I have been trying to print the points like The position of the point is (1,2) from using class, but I can't figure out a way to do it. I simply can't find a way to return two numbers like that, but the problem requires solution that way.
#include <iostream>
using namespace std;
class MyPoint{
public:
int x,y,radius;
MyPoint()
{
x=0;
y=0;
}
MyPoint(int x1,int y1)
{
x=x1;
y=y1;
}
int point_display()
{
char st=(x,y);
return st;
}
int getAdd()
{
return x+y;
}
};
int main()
{
MyPoint mypoint;
cin>>mypoint.x>>mypoint.y;
cout<<"The position of the point is "<<mypoint.point_display()<<endl;
cout<<"The sum of the coordinates is "<<mypoint.getAdd()<<endl;
return 0;
}
The usual solution to this is to provide an overload of operator << for class MyPoint to print the point.
Something like this:
#include <iostream>
using namespace std;
class MyPoint{
public:
int x,y,radius;
MyPoint()
{
x=0;
y=0;
}
MyPoint(int x1,int y1)
{
x=x1;
y=y1;
}
int getAdd()
{
return x+y;
}
friend ostream& operator << (ostream& os, const MyPoint& p);
};
ostream& operator << (ostream& os, const MyPoint& p)
{
os << p.x << ", " << p.y;
return os;
}
int main()
{
MyPoint mypoint { 1,2 };
cout<<"The position of the point is "<<mypoint<<endl;
cout<<"The sum of the coordinates is "<<mypoint.getAdd()<<endl;
return 0;
}
Output:
The position of the point is 1, 2
The sum of the coordinates is 3
Live demo
Your point_display could return a string composed of the 2 values:
std::string point_display()
{
return std::string{"("} + std::to_string(x)
+ "," + std::to_string(x) + ")";
}
Alternatively, as your question asks about returning 2 values, the function could return a pair:
std::pair<int,int> point_display ()
{
return {x,y};
}
and in main, you could do:
auto [x, y] = mypoint.point_display();
cout << "The position of the point is ("
<< x << "," << y << ")" << endl;
However, since the data members are public, you could just destructure the object and print out the values in main:
auto [x, y, radius] = mypoint;
cout << "The position of the point is ("
<< x << "," << y << ")" << endl;
If all you want to do is print the coordinates, you could have the method do it:
void point_display()
{
cout << "(" << x << ", " << y << ")";
}
...
cout<<"The position of the point is ";
mypoint.point_display();
cout << endl;
If you really want to return the coordinates, you could have separate accessors ("getters"):
int getX()
{
return x;
}
int getY()
{
return y;
}
or use references:
void getCoordinates(int &rx, int &ry)
{
rx = x;
ry = y;
}
...
int a, b;
mypoint.getCoordinates(a, b);
cout << a << " " << b << endl;
Hello I have trouble access STL complex + binary operator, my fVector is derived from std::complex, I try to call the std::complex binary + operator function to do the calculation, but I say it does have member of operator +.
ok update more of my code as request, there is 3 file which is fVector2D_test.cpp fVector2D.cpp and fVector2D.hpp
#include <iostream>
#include <complex>
#include "fVector2D.hpp"
//using namespace std;
using std::cout;
using std::endl;
int main()
{
/*fVector2D u, v(2.4f, 7), w(v);
cout << v.X(); u = fVector2D(u.X(), v.Y());
cout << u <<endl;
// v.Y() = 3.4f; // compiler error
fVector2D a(1, 2), b(2, 3);
float dot = a*b;
cout << dot <<endl;
cout << fVector2D::EX << "+" << fVector2D::EY;*/
fVector2D v(3,4.1f), u(1.2f,8.5f);
fVector2D w = u + v;
cout << w << endl;
//w = exp(std::complex<float>(0,0.2f))*v;
//cout << w << endl;
// cout << (u*v) << endl;
//cout << fVector2D::EX << endl;
//cout << fVector2D::EY << endl;
//cout << abs(v) << endl;
return 0;
}
#include <complex>
class fVector2D : public std::complex<float>
{
public:
fVector2D(float x=0, float y =0);
fVector2D(const fVector2D& floatVector2D);
float X()const;
float Y()const;
//static const EX;
//static const EY;
private:
};
fVector2D operator+(const fVector2D& , const fVector2D&);
#include "fVector2D.hpp"
fVector2D::fVector2D(float x, float y)
: std::complex<float>(x, y)
{
}
fVector2D::fVector2D(const fVector2D& floatVector2D)
: std::complex<float>(floatVector2D)
{
}
float fVector2D::X()const
{
return real();
}
float fVector2D::Y()const
{
return imag();
}
fVector2D operator+(const fVector2D& lhs, const fVector2D& rhs)
{
return std::complex<float>::operator+(lhs,rhs);// compile error , no operator + in the member
}
+ for complex numbers is a free function, not a member function. That is why you can not access it using complex<float>:: syntax.
Beside this problem your code has other problems(I have trouble seeing any real scenario where one would want to inherit from std::complex), but that is outside the scope of the question.
I am trying, just for learning, to make a Vector class with a derived Vector3 class. The first Vector class, has a double* v; pointer for the array and some [] operators for easier data access, and Vector3 also has x, y, z pointers.
The important parts of the classes are like:
class Vector{
protected:
double* v;
int size_;
public:
[ ... a lot of stuff ... ]
double & operator [](int i);
}
class Vector3 : public Vector{
public:
double* x; //Access to v[0]
double* y; //Access to v[1]
double* z; //Access to v[2]
Vector3();
Vector3(double,double,double);
};
So my intention is to make a code like this work:
//You can create a Vector3 and access with x, y, z values:
Vector3 v3 = Vector3(10,9,8);
cout << "Testing v3.x -- v3.y -- v3.z" << endl;
cout << v3.x << " -- " << v3.y << " -- " << v3.z << endl;
//And also change its values
v3.x = 5;
v3.y = 1;
v3.z = 6;
//Now, the two following couts should print the same:
cout << "Testing v3.x -- v3.y -- v3.z and v3[0] -- v3[1] -- v3[2]" << endl;
cout << v3.x << " -- " << v3.y << " -- " << v3.z << endl;
cout << v3[0]<< " -- " << v3[1]<< " -- " << v3[2]<< endl;
And my question is:
Is it possible to do this without modifying that last code?
I know I can easily make this work changing the v3.x for v3.x[0] or something like that but I want it to be more intuitive.
If you do not need operator= for Vector3 class, you could change pointers to references.
class Vector3 : public Vector{
public:
double& x; //Access to v[0]
double& y; //Access to v[1]
double& z; //Access to v[2]
Vector3();
Vector3(double,double,double);
};
Pretty new to C++, I have been following the intermediate tutorials at 3DBuzz.com, and trying to experiment with their tasks.
Current tutorial is on classes: http://www.3dbuzz.com/vbforum/sv_showvideo.php?v=37
I am trying to overload the &operator << to output my 'Point' as a stream when I want. The relevant part of the video starts at 39:00.
As far as I can tell my code is syntactically identical (though I am new so I'm probably missing something) but I get the error:
1>c:\users\jack\documents\visual studio 2010\projects\myfirstgame\myfirstgame\main.cpp(88): error C2146: syntax error : missing ';' before identifier 'myPoint
I realise that I declare the instance Point &myPoint in the operator overload function.. but I don't know where else I could do it so the compiler knows what it is.. if that makes sense.
Any help is appreciated! Thanks
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(float f_x = 0.0, float f_y = 0.0, float f_z = 0.0);
~Point();
void SetXYZ(float X, float Y, float Z);
void SetX(float X);
void SetY(float Y);
void SetZ(float Z);
void GetXYZ(float &X, float &Y, float &Z);
float GetX();
float GetY();
float GetZ();
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "Constructor with ARGUMENTS!" << endl;
x = f_x;
y = f_y;
z = f_z;
}
void Point::GetXYZ(float &X, float &Y, float &Z)
{
X = GetX();
Y = GetY();
Z = GetZ();
}
float Point::GetX()
{
return x;
}
float Point::GetY()
{
return y;
}
float Point::GetZ()
{
return z;
}
void Point::SetXYZ(float X,float Y, float Z)
{
SetX(X);
SetY(Y);
SetZ(Z);
}
void Point::SetX(float X)
{
x = X;
}
void Point::SetY(float Y)
{
y = Y;
}
void Point::SetZ(float Z)
{
z = Z;
}
Point::~Point()
{
cout << "We're in the destructor" << endl;
}
ostream &operator <<(ostream &stream, Point &myPoint)
{
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
return stream;
}
void main()
{
float x, y, z; //Declaring floats for use in GetXYZ()
Point myLocation (1,2,-1); //Creating instance and using Point(...) function
cout << myLocation.GetX() << myLocation.GetY() << myLocation.GetZ() <<endl; // Getting xyz values and printing
myLocation.SetXYZ(2,3,-4); //Testing SetXYZ function
cout << myLocation.GetX() << myLocation.GetY() << myLocation.GetZ() <<endl; // Getting xyz values and printing
myLocation.GetXYZ(x, y, z);
cout << x << " " << y << " " << z << endl;
cout << myLocation;
system("PAUSE");
}
EDIT: Unbelievable response! Love this site already. Thanks everyone who spotted this ^^
You are missing << in :
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
^^
At line 88 you should add << before myPoint.GetZ();
Your code:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
Correction:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " << myPoint.GetZ();
See the difference in between these two?
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
stream << myPoint.GetX() << " " << myPoint.GetY() << " " << myPoint.GetZ();
Here is the problem: stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ(); You are missing << between the last " " and the last myPoint.
Here you go:
ostream &operator <<(ostream &stream, Point &myPoint) { stream << myPoint.GetX() << " " << myPoint.GetY() << " "<< myPoint.GetZ(); return stream; }
Note the extra "<<" before myPoint.GetZ();