Vectors - Past Exam Question

Hi, I'm currently revising for my C++ exam and I'm going through past papers and I'm stuck on the very first question, can someone explain this to me, I feel like a n00b! The question is:

1. a) Define a Vector2D class encapsulating two real-valued coordinates of a vector on a two-dimensional plane. Use the most compact real type for representation. Include a constructor and appropriate scalar selector methods.

P.S. I don't think I've gone through the Vector2D class yet, I've only studied vector class a little bit.
A two-dimensional vector is exactly like a vector. You just need to expand it into another dimension: vector<vector<T> >.
Try this on for size.

Vector2D.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef VECTOR2D
#define VECTOR2D

class Vector2D
{
    float _x;
    float _y;

    Vector2D();
public:
    Vector2D( float x, float y, float scaler = 1 );
    scale( float scaler );
};

#endif /* VECTOR2D */ 


Vector2D.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Vector2D.hpp"

Vector2D::Vector2D( float x, float y, float scaler ) :
    _x( x * scaler ),
    _y( y * scaler )
{
}

Vector2D::scale( float scaler )
{
    _x *= scaler;
    _y *= scaler;
}


test.cpp:
1
2
3
4
5
6
7
8
9
#include "Vector2D.hpp"

int main( int args, char * argv[] )
{
    // create a vector from the origin to the point (3,5)
    Vector2D vector( 3, 5 );

    return 0;
}
I think helios misunderstood - it is a vector of real-valued coordinates, not a vector-of-vectors. Much like a 2D point.

My bet is that you are asked to define a class that contains two real-valued coordinates, say x and y. In C++ you have a choice of two built-in types for real-number representation, and you must use the one that takes up the least memory of them.

You must do a constructor that initializes the two real-number values. The "appropriate scalar selector methods" i don't know about - get/set methods?

@ Seymour: Please do not give away answers like this. If a man is hungry, give him a net and teach him to fish, don't just give him a fish!
Last edited on
Thank you very much for your replies fellas. :)
Topic archived. No new replies allowed.