c++learner
November 25th, 2008, 04:44 PM
Hi all!
I'm trying to create a Vector3D that represents a three dimensional vector in c++, and i'm totally confused with constructors/destructors/ and << >> overloadings. It compiles but doesnt' work. Could you point what important I'm missing (if you know)? Thank you very much!
Here is the code:
header:
#ifndef Vector3D_h
#define Vector3D_h
#include <iostream>
using namespace std;
class Vector3D {
friend ostream& operator<<(ostream& out, const Vector3D& V);//Ouputs Vector3D
friend istream& operator>>(istream& in, Vector3D& V);
public:
Vector3D(); //null constructor that initializes the vector to all 0’s
Vector3D(double x , double y, double z); //constructor that initializes the x, y, and z components
// Vector3D(Vector3D& V); //constructs copy of Vector3D type V
~Vector3D(); //deallocates Vector3d array memory
void Print() const;
private:
double vect[3]; //double array of size 3
};
#endif;
//Vector3D.cpp
#include <iostream>
using namespace std;
#include "Vector3D.h"
Vector3D::Vector3D() //null constructor that initializes the vector to all 0’s
{
for (int i = 0; i < 3; i++) vect[i] = 0;
}
Vector3D::Vector3D(double x , double y, double z) //constructor that initializes the x, y, and z components
{
vect[0] = x; vect[1] = y; vect[3] = z;
}
/*
Vector3D::Vector3D(Vector3D& V)
{
vect[0] = V[0]; vect[1] = V[1]; vect[3] = V[2];
}
*/
Vector3D::~Vector3D() //destructor
{
delete [] vect; //deallocates array (of any length)
}
/*
void Vector3D::Print() const
{
cout << "vector (x, y, z) - components are: (";
for (int i = 0; i < 3; i++)
cout << vect[i] << " " ;
cout << ")" <<endl;
}*/
istream& operator>>(istream& in, Vector3D& V)
{
double x , y, z;
in >> x >> y >> z;
V = Vector3D (x, y, z);
return in;
}
ostream& operator<<(ostream& out, const Vector3D& V)//Outputs Vector3D V
{
out << "vector (x, y, z) - components are: (";
for (int i = 0; i < 3; i++)
out << V.vect[i] << " " ;
out << ")" << endl;
return out;
}
//Vector3DTest.cpp
#include <iostream>
using namespace std;
#include "Vector3D.h"
int main(){
Vector3D V;
double x, y, z;
cout << "Please enter your 3D vector ";
cin >> V;
cout << endl << V << endl;
return 0;
}
I'm trying to create a Vector3D that represents a three dimensional vector in c++, and i'm totally confused with constructors/destructors/ and << >> overloadings. It compiles but doesnt' work. Could you point what important I'm missing (if you know)? Thank you very much!
Here is the code:
header:
#ifndef Vector3D_h
#define Vector3D_h
#include <iostream>
using namespace std;
class Vector3D {
friend ostream& operator<<(ostream& out, const Vector3D& V);//Ouputs Vector3D
friend istream& operator>>(istream& in, Vector3D& V);
public:
Vector3D(); //null constructor that initializes the vector to all 0’s
Vector3D(double x , double y, double z); //constructor that initializes the x, y, and z components
// Vector3D(Vector3D& V); //constructs copy of Vector3D type V
~Vector3D(); //deallocates Vector3d array memory
void Print() const;
private:
double vect[3]; //double array of size 3
};
#endif;
//Vector3D.cpp
#include <iostream>
using namespace std;
#include "Vector3D.h"
Vector3D::Vector3D() //null constructor that initializes the vector to all 0’s
{
for (int i = 0; i < 3; i++) vect[i] = 0;
}
Vector3D::Vector3D(double x , double y, double z) //constructor that initializes the x, y, and z components
{
vect[0] = x; vect[1] = y; vect[3] = z;
}
/*
Vector3D::Vector3D(Vector3D& V)
{
vect[0] = V[0]; vect[1] = V[1]; vect[3] = V[2];
}
*/
Vector3D::~Vector3D() //destructor
{
delete [] vect; //deallocates array (of any length)
}
/*
void Vector3D::Print() const
{
cout << "vector (x, y, z) - components are: (";
for (int i = 0; i < 3; i++)
cout << vect[i] << " " ;
cout << ")" <<endl;
}*/
istream& operator>>(istream& in, Vector3D& V)
{
double x , y, z;
in >> x >> y >> z;
V = Vector3D (x, y, z);
return in;
}
ostream& operator<<(ostream& out, const Vector3D& V)//Outputs Vector3D V
{
out << "vector (x, y, z) - components are: (";
for (int i = 0; i < 3; i++)
out << V.vect[i] << " " ;
out << ")" << endl;
return out;
}
//Vector3DTest.cpp
#include <iostream>
using namespace std;
#include "Vector3D.h"
int main(){
Vector3D V;
double x, y, z;
cout << "Please enter your 3D vector ";
cin >> V;
cout << endl << V << endl;
return 0;
}