Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Objects across methods
    text
    copied!<blockquote> <p>Edit again: I think I get it now. All I need to do then is use the current class colon the class I want to be able to access? Person : Student, or person : teacher Is that correct?</p> </blockquote> <p>I'm currently trying to learn the ins and outs of object oriented programming. Currently I have a new object that's something like the following:</p> <pre><code>class student { int grade; //0 - 100 as opposed to the characters A, B, C, etc. string teacher; //For the teacher of the class the student is in. } class teacher { int courseAverage; string name; //teacher.name would correspond to the teacher in the student class. } class Program { student Jim; Jim = new student(); teacher John; John = new teacher(); } static void grades() { Jim.grade = 100; } static void teacher() { Jim.teacher = "John Smith"; } static void average() { int average; //For the sake of simplicity I'll leave the average at an int. average = (Jim.grade + Sue.grade + Sally.grade + Robert.grade) / 4; /*A loop would be set before the average variable to ensure that only students of John would be counted in this.*/ } static void teacheraverage() { John.courseAverage = average;//from the average method. } </code></pre> <blockquote> <p>EDIT:</p> <p>What I would like to do is modify the information from another class. However, I would like to modify the information from the Jim student in a method from within the program class. A method to compute the average of grades for the students who have the given teacher. </p> <p>Also, the only reason I use static in these is because that is the only way I have managed to access variables across methods. I tried using static methods to use the methods across classes with no success. Is there another way to do this?</p> </blockquote> <p>I would like to use the Jim student in multiple methods. One that will set Jim's grade, and another that will set the teacher. I would like to use different methods in this case so that I can learn how it is done. </p> <p>Okay, it looks like my understanding wasn't correct. I am going to try the methods within the class approach. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload