Skip to content

Instantly share code, notes, and snippets.

@Inityx
Last active January 30, 2017 23:58
Show Gist options
  • Save Inityx/2efd4fa2d7bb6696ff91988a852af1f5 to your computer and use it in GitHub Desktop.
Save Inityx/2efd4fa2d7bb6696ff91988a852af1f5 to your computer and use it in GitHub Desktop.
template<typename Data> struct Point {
// Structs have all public by default, where classes are all private by default
Data x;
Data y;
Point(): // default constructor
x(0), y(0) { // member initialization, an alternative to [= in the constructor]
// do things
}
Point(Data ix, Data iy): // constructor
x(ix), y(iy) { // member initialization
// do things
}
Point(const Point& ip) { // copy constructor
x = ip.x;
y = ip.y;
}
Point& operator=(const Point& righthand) { // assignment operator
x = righthand.x;
y = righthand.y;
}
~Point() { // destructor
// do things
}
};
int main() {
Point<double> foo(5, 6); // on the stack
Point<int>* bar = new Point<int>(5, 6); // on the heap
Point<double> baz(foo); // copy construct
Point<double> quux = foo; // assignmnet
delete bar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment