Created
September 16, 2014 17:38
-
-
Save eraserhd/851d50590399aa14566c 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
| #include <algorithm> | |
| #include <iostream> | |
| using namespace std; | |
| class Slime { | |
| public: | |
| int n; | |
| // Constructor is a method with the same name as the class and | |
| // no return type (it doesn't return anything). | |
| Slime(int n_) | |
| { | |
| n = n_; | |
| } | |
| // You can have multiple constructors, as long as they take | |
| // different kinds or number of parameters. | |
| Slime(int a, int b, int c) | |
| { | |
| n = c; | |
| } | |
| // With no parameters, it is the "default constructor" | |
| Slime() | |
| { | |
| n = 79; | |
| } | |
| // Other noteworthy constructors include the copy constructor | |
| // (You often don't need to type it in as the compiler will | |
| // make it for you in most cases. It will fail to do that | |
| // when your class has some member variable it doesn't know | |
| // how to copy. The compiler can copy ints just fine.) | |
| Slime(Slime const& rhs) | |
| { | |
| n = rhs.n; | |
| } | |
| }; | |
| int main() { | |
| Slime s = 42; | |
| cout << "got: " << s.n << endl; | |
| Slime z(1,2,3); // When number of parameters != 1, can't use the '=' trick. | |
| cout << "got: " << z.n << endl; | |
| Slime y; // Default constructor is used here | |
| cout << "got: " << y.n << endl; | |
| Slime w = y; | |
| cout << "got: " << w.n << endl; | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment