Thursday 15 May 2014

Is it a waste to create a class with only 1 object in c++ -



Is it a waste to create a class with only 1 object in c++ -

if wanted store info 1 thing (such user), waste create class?

this class ever hold 1 object, user. me seems clean , easy understand if using class don't know if it's wasteful or not.

class c_user{ public: int getage(); string getname(); void setage(); void setname(); private: int m_userage; string m_username; }

these can neatly accessed in main. 1 time they're set. can have it's own functions , has lot of control.

int main(){ c_user user; //set //get user.getage(); user.getname(); }

below way accomplish same thing without using class. 1 time they're set.

int main(){ int userage; string username; //set //get userage; username; }

they aren't beingness used above. thought of accessing them, , using them in way.

i'm not sure if method without class confusing 1 time holds more information. i've done has been small-scale i'm trying move onto larger projects more information.

is waste create class has 1 object?

it allows treat both values single entity, - conceptually, single entity have. since class simple container 2 values both getters , setters, doesn't accomplish encapsulation , more written as:

struct user { int age; string name; };

and syntactic cost of defining seems more in line actual usefulness.

c++ class logic

No comments:

Post a Comment