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();
Vector2d(double xValue, double yValue);
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();
};
#endif
Member Access
You can access a member of a class with the dot operator (instance.member
).
Vector2d v;
v.setX(1);
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;
}
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.
Vector2d::Vector2d() {
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();
Vector2d(double xValue, double yValue);
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;
}
double Vector2d::dist() {
return sqrt(pow(x, 2) + pow(y, 2));
}
double Vector2d::angle() {
return atan2(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
<< v.dist() << endl
<< v.angle() << endl;
}
More Information