How to make this nested class work?

  • MHB
  • Thread starter FallArk
  • Start date
  • Tags
    Class Work
In summary, the conversation is about creating a program to calculate one's own grade using subclasses inside a class. The main function cannot be changed and the individual is trying to find a general solution by creating a subclass that asks for user input and declaring it in the main class. However, they are having trouble with the for loop not exiting when the user inputs -1 and the program is not functioning as expected.
  • #1
FallArk
127
0
So, I was asked to make a program that will calculate my own grade, and it is required to use subclasses inside a class.
main is posted below and cannot be changed:
Code:
int main() {
Grade gradeMe;
gradeMe.getExercises();
gradeMe.getClickers();
gradeMe.getLabs();
gradeMe.getTraces();
gradeMe.getAssignments();
gradeMe.getTests();
gradeMe.getPrefinal();
gradeMe.getFinal();
gradeMe.displayTotals();
}

what I have done:
Code:
class Grade {
public:
    class Textbook{
public:
    int textbookPts;
    int i = 1;
    int bookPts() {
        cout << "Enter textbook exercise #" << i << " (out of 10 points): ";
        cin >> textbookPts;
        ++i;
        return textbookPts;
    }
    };
    Textbook Text[10];
    int getExercises() {
        for (int x = 0; x < 10; ++x){
            Text[x].bookPts();
            textbookSum += Text[x].bookPts();
            if (Text[x].bookPts() == -1)
                break;
            textbookTotal = x * 10;
        }
        cout << textbookSum << " out of " << textbookTotal << " points for textbook grade" << endl;
        return textbookSum;
    }
private:
    int textbookSum = 1;
    int textbookTotal = 0;
};

My idea is to find the general case of this program, then use it as a template to fill the others. I was thinking that create a class that will ask the user(myself in this case) to input the grade, and then declare this subclass in class Grade, the getexercises function then have a for loop to keep asking for input until the user input -1. But as of now, the loop will not exit when -1 is entered, and it keeps going #1... #2... #3... #1... etc, instead of going 1...2...3...4...5...6...
Help!:confused:
 
Technology news on Phys.org
  • #2
I also was trying to set the sum of what the user get on the textbook exercises equal to the sum of the inputs, and the total of textbook exercises points equal to how many time the loop executed times the points of one exercises. The program is not really doing what I think it would, I must have messed up somewhere.
 

Related to How to make this nested class work?

1. How do I access the nested class from the parent class?

To access the nested class from the parent class, you can use the dot notation. For example, if the nested class is named "Nested" and the parent class is named "Parent", you can access the nested class by writing "Parent.Nested" in your code.

2. Can I have a nested class within another nested class?

Yes, you can have a nested class within another nested class. This is known as a multi-level nested class and can be useful for organizing complex code.

3. How do I initialize an instance of a nested class?

To initialize an instance of a nested class, you first need to create an instance of the parent class. Then, using the dot notation, you can access the nested class and create an instance of it. For example, if the nested class is named "Nested" and the parent class is named "Parent", you can initialize an instance of the nested class by writing "Parent.Nested nestedObject = new Parent.Nested();" in your code.

4. Can I make a nested class private?

Yes, you can make a nested class private. This means that the nested class can only be accessed within the parent class and cannot be accessed from outside the parent class.

5. Is there a limit to the number of nested classes I can have?

There is no set limit to the number of nested classes you can have. However, having too many nested classes can make your code difficult to understand and maintain. It is recommended to use nested classes only when necessary.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
4K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
908
  • Programming and Computer Science
2
Replies
36
Views
2K
Back
Top