C++ Programming Tutorials brought to you by TONY TUTORIALS.
Пікірлер: 6
@luthandonomzaza1607 Жыл бұрын
Your accent says you are in South Africa. . . Super proud of you man
@aleynaaslan55164 жыл бұрын
Hi, nice content. I need to write a program where it identifies if a number is odd or even. The software should take user input in the main function and the function that decides if the number is odd or even should be done inside the class function of your program. Mine does not work about determining whether it is odd or even. I appreciate that if you can help me.
@AntonieSmithVideos4 жыл бұрын
Thanx =)
@aleynaaslan55164 жыл бұрын
Mine is like that and i think it is very wrong #include using namespace std; class Numbers { int n; public: void displayMessage() { if (n%2==0) { cout
@AntonieSmithVideos4 жыл бұрын
Hi, 1. We need to understand that the variable, "int n" declared in the class and the variable, "int n" declared in the main function is not the same variable. Actually, the variable declared in the class does not exist until an instance of the "class numbers" is created. 2. The variable, "int n" in the class must be defined as either "public" or "private". This will determine how you will interact with the variable. We need to interact with the variable, in order to initialize the variable by either a function (if the variable is private), or directly (if the variable is public). 3. If we understand how to interact with the variable, we can send the user input to the class via the correct channel. 4. Your coding to determine if the number is odd or even is perfect. It is just the way you use the class that is the problem.
@AntonieSmithVideos4 жыл бұрын
This is my suggestion: #include using namespace std; class Numbers { public: void setn( int a ) //Set function to pass user input to the class variable { n = a; } void displayMessage() { if (n%2==0) { cout