Last active
January 30, 2017 23:58
-
-
Save Inityx/2efd4fa2d7bb6696ff91988a852af1f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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