Team 2550
Technical Documentation
 All Files Variables Pages
Classes

Classes are an extension of structs. Here a few useful features...

  • In addition to storing data, classes can also hold functions (called methods) that operate on the data
  • You can restrict access to variables (this does not sound good, but you will see the benefit shortly)
  • You can assign default values to variables within a class
  • You can use one class to extend another (inheritance)
  • Operators can be overloaded like functions

Classes are used in object-oriented programming. An instance of a class is called an object, often corresponds to real-life objects. On a robot, there may be an arm class, a drive class, and a launcher class.

Declaration

Classes are declared in a very similar way to structs...

#ifndef VECTOR_HH
#define VECTOR_HH
class Vector2d {
private:
double x, y;
public:
Vector2d(); //This is the default constructor; I will explain this later
Vector2d(double xValue, double yValue); //Another constructor
void setX(double value);
void setY(double value);
void chgX(double add);
void chgY(double add);
double getX();xc
double getY();
double dist();
double angle();
void print();
}; //Classes need a semicolon here, like structs
#endif

Member Access

You can access a member of a class with the dot operator (instance.member).

//In some function
Vector2d v;
v.setX(1); //calling a method
v.setY(3);

In order to declare a method (a function within the class), you use the scope resolution operator (scope::item).

void Vector2d::setX(double value) {
x = value; //x refers to x within the class, be careful not to give arguments the same names as members
}

Constructors

Every class must have a default constructor, even if it does nothing. A constructor is a function without a return type that is run when a class is instantiated. All constructors must have the same name as the class.

Note
You instantiate a class when you use it as the datatype for a variable. An instance of the class is created when you write, for example Vector v;.

For the vector class declared above, here is a possible implementation for the default constructor.

//By convention, this code would go near the top of the implementation file (ClassName.cc).
Vector2d::Vector2d() { //The default constructor always has no arguments.
x = 0;
y = 0;
}
Note
When you create a class, it is always a good idea to initialize the member variables to a good default value. The behavior may be unpredictable otherwise.

With Arguments

After the default constructor, sometimes it is convenient to provide alternate constructors that accept arguments. The constructor Vector2d(double xValue, double yValue); will allow the user to set the values for both x and y when the class is instantiated.

Vector2d::Vector2d(double xValue, double yValue) {
x = xValue;
y = yValue;
}

Example: Vector2d

Here is a complete declaration and implementation of a simple class, called Vector2d.

Vector2d.hh, #include this file wherever you need to use this class.

#ifndef VECTOR_HH
#define VECTOR_HH
class Vector2d {
private:
double x, y;
public:
Vector2d(); //This is the default constructor I will explain this later
Vector2d(double xValue, double yValue); //Another constructor
void setX(double value);
void setY(double value);
void chgX(double add);
void chgY(double add);
double getX();
double getY();
double dist();
double angle();
void print();
};
#endif

Vector2d.cc, the implementation of the class declared in Vector2d.hh

#include <cmath>
#include <iostream>
#include "Vector2d.hh"
using namespace std;
Vector2d::Vector2d() {
x = 0;
y = 0;
}
Vector2d::Vector2d(double xValue, double yValue) {
x = xValue;
y = yValue;
}
void Vector2d::setX(double value) {
x = value;
}
void Vector2d::setY(double value) {
y = value;
}
void Vector2d::chgX(double add) {
x += add;
}
void Vector2d::chgY(double add) {
y += add;
}
double Vector2d::getX() {
return x;
}
double Vector2d::getY() {
return y;
}
//Calculate the distance between (0, 0) and (x, y)
double Vector2d::dist() {
//pythagorean theorem
return sqrt(pow(x, 2) + pow(y, 2));
}
//Calculate the angle of the vector, returns the answer in radians
double Vector2d::angle() {
return atan2(y, x); //atan2 computes the arctan of y / x
}
void Vector2d::print() {
cout << '<' << x << ", " << y << '>';
}

Here is an example usage of the class, this will work in any function assuming that the header was included at the top of the file.

int main() {
Vector2d v(2, 2);
v.print();
cout << endl //This is actually a single line to the compiler, remember that a line ending is denoted with a semicolon
<< v.dist() << endl
<< v.angle() << endl;
}

More Information