Header file optimization

Heya, I'm just creating some random stuff to kill time and I was just wondering if there was anyway to further optimize or in anyway improve the following code: (don't be discouraged by it's length, it's really easy to understand)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* 
 * File:   CVector.h
 * Author: T. Westendorp
 *
 * Description:
 * Defines functionality of mathemathical vector calculations.
 *
 * Important note:
 * Setting the length of a null vector can lead to unexpected behavior.
 * (doubles holding values of: -nan)
 *
 * Automatically includes: <cmath> (if not earlier included)
 */

#ifndef cmath
#include <cmath>
#endif

#ifndef _CVECTOR_H

#define PI 3.141592653
class CVector
{
private:
    double * x; // Dynamically allocated double as x coordinate
    double * y; // Dynamically allocated double as y coordinate
public:
    CVector(); // Standard constructor
    CVector(const double&,const double&); // Overloaded constructor
    CVector(const CVector&); // Copy constructor
    ~CVector(); // Standard destructor
    CVector operator + (const CVector&) const; // Add vectors
    CVector operator - (const CVector&) const; // Substract vectors
    CVector operator / (const double&) const; // Divide by a scalar
    CVector operator * (const double&) const; // Multiply by a scalar
    CVector& operator = (const CVector&); // Vector equation
    CVector& operator += (const CVector&); // Vector addition
    CVector& operator -= (const CVector&); // Vector subtraction
    CVector& operator /= (const double&); // Scalar division
    CVector& operator *= (const double&); // Scalar multiplication
    operator double() const; // Convert to double
    operator int() const; // Convert to integer
    void SetX(const double&); // Set x endcoordinate
    void SetY(const double&); // Set y endcoordinate
    void SetLen(const double&); // Set length (using the current angle)
    double GetX(void) const; // Get x endcoordinate
    double GetY(void) const; // Get y endcoordinate
    double GetDir(void) const; // Get the offset 'theta' (aka angle)
    double GetLen(void) const; // Get the length of the vector
};

CVector::CVector()
{
    x = new double(0.0);
    y = new double(0.0);
}

CVector::CVector(const double& xx, const double& yy = 0.0)
{
    x = new double(xx);
    y = new double(yy);
}

CVector::CVector(const CVector& v)
{
    x = new double(*(v.x));
    y = new double(*(v.y));
}

CVector::~CVector()
{
    delete x;
    delete y;
}

CVector CVector::operator + (const CVector& v) const
{
    return CVector ( (*x) + v.GetX() , (*y) + v.GetY() );
}

CVector CVector::operator - (const CVector& v) const
{
    return CVector ( (*x) - v.GetX() , (*y) - v.GetY() );
}

CVector CVector::operator / (const double& d) const
{
    return CVector ( (*x) / d , (*y) / d );
}

CVector CVector::operator * (const double& d) const
{
    return CVector ( (*x) * d , (*y) * d );
}

CVector& CVector::operator = (const CVector& v)
{
    if (this != &v)
    {
        *x = *(v.x);
        *y = *(v.y);
    }
    return *this;
}

CVector& CVector::operator += (const CVector& v)
{
    *x += *(v.x);
    *y += *(v.y);
    return *this;
}

CVector& CVector::operator -= (const CVector& v)
{
    *x -= *(v.x);
    *y -= *(v.y);
    return *this;
}

CVector& CVector::operator /= (const double& d)
{
    *x /= d;
    *y /= d;
    return *this;
}

CVector& CVector::operator *= (const double& d)
{
    *x *= d;
    *y *= d;
    return *this;
}

CVector::operator double() const
{
    return GetLen();
}

CVector::operator int() const
{
    return GetLen();
}

void CVector::SetX(const double& xx)
{
    *x = xx;
}

void CVector::SetY(const double& yy)
{
    *y = yy;
}

void CVector::SetLen(const double& d)
{
    (*this)/=GetLen();
    (*this)*=d;
}

double CVector::GetX(void) const
{
    return (*x);
}

double CVector::GetY(void) const
{
    return (*y);
}]

double CVector::GetDir(void) const
{
    return (atan((*y)/(*x)) / PI * 180);
}

double CVector::GetLen(void) const
{
    return (sqrt((*x)*(*x)+(*y)*(*y)));
}
#endif 


I'm also open to suggestion on what to add or even, on what subject to focus my next math-oriented header file.
As far as I remember, this is not necessary:
1
2
3
#ifndef cmath
#include <cmath>
#endif 

I believe standard headers have their own include guards.

The other include guard is missing the #define _CVECTOR_H line. As it is, you never define _CVECTOR_H. Also, identifiers beginning with the '_' character are reserved by the standard.

Also notice that you're using a macro hack to define pi, when a const double would be a much better (and type safe) option.
A major problem is that you're dynamically allocating x and y, even though there is no apparent reason to do so.
You might want to see about using an initialization list in your constructor(s).

Also, some operators are better left outside of the class as friends. If I am remembering correctly, operator+, operator-, operator*, and operator/ are some of them. It has to do with associativity...
The top of your file is not very efficient. This should be better:

1
2
3
4
5
6
7
8
9
#ifndef _CVECTOR_H
#define _CVECTOR_H // remember to make this define

// place your includes inside your include guards
#include <cmath> // do not conditionally include them

class CVector
{
Why the dynamic allocation?

1
2
3
4
5
class CVector
{
private:
    double * x; // Dynamically allocated double as x coordinate
    double * y; // Dynamically allocated double as y coordinate 


This should be safer and quicker

1
2
3
4
5
class CVector
{
private:
    double x; // no dynamic allocation
    double y; 
unary minus?

use boost::operators where possible

provide friends so you can do things like 2 * v (I believe you support only v * 2).

providing both operator int() and operator double() is evil. (provide neither;
prefer an explicit accessor).
Topic archived. No new replies allowed.