State Pattern Design using OOP [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
A simple example of how you would structure this would be particularly useful.

This is how I would do it:
MyMotor is an instance of the class Motor. This class has four functions idle(), accelerate(), flat(), decelerate(). (I assume you know how to build a basic class with private members and its constructors)
Then in main(), I create MyMotor and control it based on states. States can be controlled/monitored using Boolean Values. Whatever state I am in and whenever, certain function will be called.
Next time give it a try before you ask here, in order to get better responses.

Related

How to find the total numbers of object created of my class? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For example, I have my class employee. I want to keep record how many employees till date have worked for me. I can make static count variable and add 1 in my constructor. But whenever my temporary object will be created when we pass object in parameters or return object of our class it will add for them too.
Static class member is the right way to go. A few things to be careful about:
Make sure you overload all constructors. The ones you don't want to support you should explicitly delete.
Don't forget to decrement in destructor.
If this program of yours is multithreaded then use atomic_uint or provide locking mechanism of your own.

Can anyone tell me a practical situation from real-life applications where friend functions can be useful? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am a newbie in OOP (C++) and just learned about friend function but is there a real life example that defines the usage of friend function ?
I am developing a large class library where some of them provide services used by others, such as graphics.
I am not exposing the inner mechanisms to the normal users, but my classes and functions are allowed to. Hence, friends.

Scopes in blank function c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hy, my question is simple..
I have this function.
CPythonMessenger::CPythonMessenger(): m_poMessengerHandler(NULL)
{
}
What scope have and why is there since constructor is empty and also is not used m_poMessengerHandler(NULL) i want to say the function is not used anywhere is constructor.
I guess you are examining (or maybe it is your code) THIS.
CPythonMessenger is the default constructor for the class CPythonMessenger as you can see in the relative header file HERE. After the : you can invoke a method (or another constructor) that must be run when creating an object of CPythonMessenger type. In particular, it creates an instance of m_poMessengerHandler that is then used in several place in the other class methods.

Should a class be thread-safe? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Should a thread-safe mechanism be added when a class is developed and it is known that this class will be used in multi-threaded environment (not always however) or leave it to the user?
As a general rule, it's more flexible to leave it to the user. For example, consider a map-type container. Suppose the application needs to atomically move something from one map to another map. In this case, the user needs to lock both maps before the insert-erase sequence.
Having such a scenario be automatically taken care of somehow by your class would probably be inelegant, because it's naturally something that happens across objects, and because there may be many such scenarios, each slightly different.

Should I aim to write as little as possible in my main() function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm making a small game and I don't know whether I should have the majority of the statements in the main function or instead just put them as void functions in my player object (I'm not really returning anything other than boolean values throughout each iteration).
In general, you should aim for your main() to be a bridge between the execution environment (the OS) and the system that you implement. This means that main should "crack" the command-line parameters, and then promptly pass control to the method that instantiates top-level objects and runs your system.