Doubt about pointers (C++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Doubt about pointers (C++)

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.

19th Jul 2018, 8:48 PM
Enderson Perez
Enderson Perez - avatar
9 Answers
+ 5
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.
20th Jul 2018, 3:05 AM
John Wells
John Wells - avatar
+ 3
This would be with getters: https://code.sololearn.com/cW9UwAWc8rVr
20th Jul 2018, 7:28 PM
John Wells
John Wells - avatar
+ 2
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.
20th Jul 2018, 5:48 PM
John Wells
John Wells - avatar
+ 1
IMHO, one should ask here only... others can get benefit also from single post and different people can share their views...
20th Jul 2018, 7:26 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
use member functions getter and setter to access member variable....
19th Jul 2018, 9:20 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Hi, thank you for answering, I still have doubts regarding this issue, will anyone have an email to which I can communicate?
20th Jul 2018, 6:24 AM
Enderson Perez
Enderson Perez - avatar
0
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 :(
20th Jul 2018, 5:42 PM
Enderson Perez
Enderson Perez - avatar
0
it would be this way: #include <iostream> using namespace std; class Frecuency { public: //Function Member int birthDate() { cout << "place your date of birth: (DD/MM/AA)"; cin >> d >> s >> m; } private: int d,s,m; }; int main() { Frecuency b1; 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).
20th Jul 2018, 7:07 PM
Enderson Perez
Enderson Perez - avatar
0
Thank you very much !!, look how easy it was! I appreciate it a lot. (Another question these commands can be valid for builders and calls from source codes attached to classes?).
20th Jul 2018, 7:56 PM
Enderson Perez
Enderson Perez - avatar