[[Programming#Section 2 Intermediate|Learn Programming Section 2]] previous: [[Section 1 Exercises]] --- ## General Structs and Classes are a way to group up or encapsulate other data into a single container. You can put variables(including structs/classes) and functions into either of them. They are almost identical in function with the main difference being: - <u>Struct</u> members are public by default - <u>Class</u> members are private by default ```cpp struct position { float x; float y; }; class position2 { float x; float y; } int main() { position pos1; //struct pos1.x = 0; position2 pos2; //class pos2.x = 0 //Error x is private } ``` Public variables are ones that can be accessed from outside the class/struct while private can only be accessed from inside or from designated 'friends' ```cpp //You can change what is accessible by using certain keywords like 'public:' and 'private:'. //Everything below they keyworld until the next keyword is affected. struct position{ float x; //public as normal void foo(); //public private: float y; //private float y; //still private void bar(); //private public: float w; //back to public; }; class position{ float x; //private as normal float y; //private as normal public: float z; //public void bar(); //public private: void foo(); //private }; ``` While structs and classes can largely be used interchangeably there are some conventions most follow. Generally recommendations for use are - use Structs for "Plain Old Data" (just variables, no functions, constructors, etc..) - Classes for anything more complex ## Constructors Constructors are called when an instance of your class is created. They are used to initialize their members and call other functions. Constructors have the same name as their class. ### Default All classes have a default constructor unless specifically deleted. If you do not create one the compiler will generate one for you. > [!Warning] Generated constructors do not initialize values on their own. > ```cpp class Health { public: Health() // Our default constructor { value_ = 100; } private: float value_; }; int main(){ Health hp; //an instance of our Health class is created here so it's default constructor is called. } ``` Other ways of organizing our constructor ```cpp //initializer list class Health { public: Health(int maxDefault = 100, valueDefault = 100) : max_(maxDefault), value_(valueDefault) { /*Nothing here for now*/ } private: float max_; float value_; }; int main(){ Health hp; //uses set default values of 100 for both } ``` ```cpp class Health { public: Health() { /*Nothing here for now*/ } private: float max_ {100}; float value_ {100}; }; int main(){ Health hp; //uses set default values of 100 for both } ``` You can also have more than one constructor. ```cpp class Health { public: Health() { } Health(int max) { value_ = max; } Health(int maxDefault = 200, valueDefault = 100) : max_(maxDefault), value_(valueDefault) { } private: float max_ {100}; float value_ ; }; int main(){ Health hp; //uses set default values of 100 for both } ``` ```cpp //For initializer lists default values are filled from right to left //this works Health(int maxDefault , valueDefault = 100) : max_(maxDefault), value_(valueDefault) { } //this doesn't Health(int maxDefault = 100 , valueDefault) : max_(maxDefault), value_(valueDefault) { } //Note, for initlizer lists it is only a default constructor if all paramaters are asigned a default value ``` ### Copy Copy constructors use an already existing instance of the class to copy it's values during it's own construction. ```cpp class Health { Health(const Health& other); //Good practice to use const to prevent modification of other Health(const Health& other, int value = 100); //You can also add other parameters after } ``` ### Move ```cpp class Health { Health(Health&& other) : { } } ``` ### Deleting Delete constructors if you want to prevent those operations. ```cpp class Health { public: // Delete the default constructor Health() = delete; // Delete the copy constructor Health(const Health&) = delete; // Delete copy assignment operator for complete prevention of copying Health& operator=(const Health&) = delete; // Delete move constructor Health(Health&&) = delete; }; ``` ## Destructors Destructors are functions that are called when the object either goes out of scope or we delete it. This is most important for cleaning up any memory that the class might manage. ```cpp class Health { public: Health() {} //constructor ~Health() {}; //destructor } ``` ## Getting and Setting We need some way to interface with our private data. These are generally referred to as Getters/Setters or Accessors/Mutators. ```cpp class Health { public: Health(int maxDefault = 100, valueDefault = 100) : max_(maxDefault), value_(valueDefault) { } int Get_HP() { return value_; } void Set_HP(int hp) { value_ = hp; } void Damage(int damage) { value_ -= damage; if(value_<= 0) DeathEvent();// } void Heal(int heal) { value_ = std::min(value_+heal, max_); } private: int value_; int max_; } ``` # --- next: [[pointers and references]]