jeudi 27 juillet 2017

C++ when is the right time to use a no default constructor vs. getters and setters vs just invoking the variables in the class directly

Ie. When I have a class say

class color()
{
     Private:
     std:: string _colors

  Public:
   color();
   color(std::string colors);
   std::string setColors(std::string colors);
  std::string colors;
  ~color();
}

And I wanted to call it in another class like main to assign to the variable colors.

#include "color.h"

using namespace std;
int main()
{
  color C;
  C.colors = "Blue"; //is this correct
  color("Blue"); //is this correct
 C.setColors("Blue");//or is this correct.
 return 0;
 }

When is the correct time to use either?

Aucun commentaire:

Enregistrer un commentaire