Hello very good, this is my first post in this forum. A week ago I finished the c ++ course on the page, and I am creating a practical application. As I create it, an existential doubt arises; Is there a possibility that the variables of member functions can be called in the main function, either by being in an access specifier as private, protected or declared in the member function itself.
Let's put it this way: I have a member function which asks the user to enter their date of birth day / month / year (here I declare 3 variables), then depending on what the user places, I will show it in a message but in the main function.
Within the same member function if this action can be done and call it in main, but I can not do it in the way that I am commenting
Would you put some example ?, or advise me.
Thank you very much.
You could declare day public and access it everywhere, however, that violates OOP design. Hiding your data makes it so you can change it without effecting the code outside of the class. Now, you want day, month, and year. However, depending on the future coding, it might make more sense to keep track of the day within the year or some other way of storing the information. Using methods to get and set the data means those functions are the only thing that must change, when you change the data.
Code something to give a better idea of what you are looking for. Use comments for places you want something, but have no idea how to code, describing what you want done. Link it here so we can expand it into working code.
If I agree friend, but still I can not solve my problem
look for setter and getter these define them and give parameters in the main (which before this were placed within the classes) I can not get an example where user enter data (this instruction I put in a member function). Help :(
it would be this way:
using namespace std;
cout << "place your date of birth: (DD/MM/AA)";
cin >> d >> s >> m;
b1.birthDate; // Here I call the date of birth and place values
cout << "his birth date is"; // Here based on the numbers that are entered, I want to show them in cout
I know you will tell me, hey, but why do not you declare them in the member function ?, actually that would be a more comfortable way to solve my problem but I do not look for that solution since I have another function that also enters data and shows them, they do not fit as such if they are not: (first sampling date, and then the other) I have already tried to use line breaks or tabs to sort them but I can not do it.
What I want is that there is the possibility of removing the variables of the private function and placing them in main without losing their entered value. (I have already tried to match, use the betters and getters but it does not work out for me).