5. Approach for writing a class #
Created Monday 13 January 2020
How to make a class:
- Decide data members. Check if there is a need for static and constants. If yes, include them.
- Constructors and Destructors.
- Provide a default constructor, set it as per contextual defaults.
- Check if a parameterized constructor is required.
- Check if any level 2 memory(i.e DS) requirement is present, if yes, provide a deepened copy constructor and deepened copy assignment operator.
- Destructors: check if a level 2 memory is present, if yes, free it.
- Grouping functions is important, write their names and objectives only: 1. Getters aka Accessors 2. Setters aka Mutators 3. Checkers aka Inspectors. is_fff() ignorable 4. Overloaded **Operators. **ignorable 5. Content aka **Facilitators. // logic part **
- Decide the operators to be overloaded, if required. Add them to the list of of functions.
- For each function, write skeleton, then:
- Return type and qualifier:
- Does it need to returns a copy or changes the same object. **Return by **reference/value?
- Nestability in operators. **Return by **reference/value?
- If it works with static data members only (i.e no this parts are accessed). Write the **static **qualifier.
- Parameters:
- For the parameters which don’t need a change, take them as const references.
- For the objects where we do need a change, take them as references.
- If any const data members are present, initialize them using the initialization list.
- For operators only, check if it is a post or pre. For unary post operators, write (int) in the param list.
- Write the code.
- Make sure objects to be returned are created inside the function. Return by value. If we need to return this, then return by reference. Buffer(rvalue optimization) will handle the rest.
- Use copy constructor as much as possible. If a deepened version is present.
- Check if returned value is at par with the return type.
- Finally, mark the function const, if it’s possible do so. (This is not allowed for constructor, destructors and static functions).
- Return type and qualifier:
- Leave notes for initializing any static data members.