5. Approach for writing a class #

Created Monday 13 January 2020

How to make a class:

  1. Decide data members. Check if there is a need for static and constants. If yes, include them.
  2. Constructors and Destructors.
    1. Provide a default constructor, set it as per contextual defaults.
    2. Check if a parameterized constructor is required.
    3. Check if any level 2 memory(i.e DS) requirement is present, if yes, provide a deepened copy constructor and deepened copy assignment operator.
    4. Destructors: check if a level 2 memory is present, if yes, free it.
  3. 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 **
  4. Decide the operators to be overloaded, if required. Add them to the list of of functions.
  5. For each function, write skeleton, then:
    1. Return type and qualifier:
      1. Does it need to returns a copy or changes the same object. **Return by **reference/value?
      2. Nestability in operators. **Return by **reference/value?
      3. If it works with static data members only (i.e no this parts are accessed). Write the **static **qualifier.
    2. Parameters:
      1. For the parameters which don’t need a change, take them as const references.
      2. For the objects where we do need a change, take them as references.
    3. If any const data members are present, initialize them using the initialization list.
    4. For operators only, check if it is a post or pre. For unary post operators, write (int) in the param list.
    5. Write the code.
      1. 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.
      2. Use copy constructor as much as possible. If a deepened version is present.
    6. Check if returned value is at par with the return type.
    7. Finally, mark the function const, if it’s possible do so. (This is not allowed for constructor, destructors and static functions).
  6. Leave notes for initializing any static data members.