-9

In the output, there was an error when I attempted to display it. The output is displayed without any errors, but the total marks and percentages are not displayed. I would appreciate if anyone could provide an explanation.

  1. create a class student that stores roll_no,name.
  2. create a class test that stores marks obtained in five subjects.
  3. class result derived from student and test contains the total marks and percentage obtained in the test.
  4. input and display information of a student.

#include <iostream>
using namespace std;
class student {
    int roll_no;
    string name;

public:
    void studentdata()
    {
        cout << "\n ENTER STUDENT NAME:";
        cin >> name;
        cout << "\nENTER ROLL NUMBER:";
        cin >> roll_no;
    }
};
class test {
    int i;
    // int marks[5{}];
public:
    int marks[5];
    void getmarks()
    {
        cout << "\nENTER MARKS OF EACH SUBJECT:";
        for (i = 0; i < 5; i++) {
            cout << "\n subject " << (i + 1) << " :";
            cin >> marks[i];
        }
    }
};
class result : public student, public test {
    int i;
    float totalmarks = 0;
    float percent;

public:
    void percentage()
    {
        // int total_marks=0;
        for (i = 0; i < 5; i++) {
            totalmarks += totalmarks + marks[i];
        }
        cout << " \n TOTAL MARKS OUT OF 500 IS:" << totalmarks;
        percent = (totalmarks * 100) / 500;
        cout << " \n \nPERCENTAGE OBTAINED IN THE TEST:" << percent << " %";
    }
};
int main()
{
    student s;
    test t;
    result r;
    s.studentdata();
    t.getmarks();
    r.percentage();
    return 0;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    Please provide a [mcve] with hardcoded data. – jabaa Jul 11 '23 at 12:10
  • editorial note: no need to be overly polite. Others are here to answer questions. Dont ask if someone can answer your question, ask the question. – 463035818_is_not_an_ai Jul 11 '23 at 12:11
  • And please don't format any explaining text as code as well. Within code tags you don't need to escape HTML characters – if you do, the escape character will be included in the code. Both renders the question nearly unreadable. – Aconcagua Jul 11 '23 at 12:15
  • Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)… – Aconcagua Jul 11 '23 at 12:17
  • I don't like that functions I perceive as getters are printing at all. – sweenish Jul 11 '23 at 12:20
  • 5
    `s`, `t` and `r` are 3 separate objects. `marks` in `r` is never intialised – Alan Birtles Jul 11 '23 at 12:22
  • It is not really a good idea to intermix IO within your data classes (but you might have classed explicitly dedicated for IO). In general this renders your classes little reusable. Better clearly separate your data classes from IO; in given case: move `studentData` function out of the class and let it accept a `student` by reference, same for `test`. On the other hand `percentage` would be a fine member for `test`, but returning the percentage instead of doing any IO… – Aconcagua Jul 11 '23 at 12:26
  • Re: "class result derived from student and test" -- unfortunate that an assignment imposes such a horrible design. – Pete Becker Jul 11 '23 at 12:38
  • 3
    There are serious misunderstandings about how C++ works here. The main thing to understand is that `s`, `t` and `r` are separate objects, and that `t.marks` is not the same as `r.marks`, and `s.name` is not the same as `r.name`. It seems you think that because `result` inherits from `student` and `test` that this makes some kind of connection between `r` and `s` and `t` but this is a complete misunderstanding. The real way that you connect different parts of your code together is by **passing parameters to functions** and **return values from functions**. – john Jul 11 '23 at 12:39
  • 1
    I'm looking at your code and there is not a single instance where any of your functions have parameters or return values. So I think you need to revise those topics and then rewrite this code from scratch. – john Jul 11 '23 at 12:39
  • Voted to reopen. The problem is quite clear; there's no need for "details or clarity". – Pete Becker Jul 11 '23 at 12:40
  • 1
    I didn't notice the requirement that *'class result derived from student and test'*. I'm not surprised you are confused, you are being taught by someone who is incompetent. – john Jul 11 '23 at 12:42
  • 1
    With `r` you already get a `student` and `test` included, so you don't need `s` and `t` at all; try: `result r; r.studentdata(); r.getmarks(); r.percentage();`. – Aconcagua Jul 11 '23 at 12:47
  • @john There is one function with a return value, specifically `main`. – Mestkon Jul 11 '23 at 12:54
  • This is an exceptionally bad design for the problem described, by the way; while still not perfect a much better approach might look like [this](https://godbolt.org/z/4d6qPEPTn), considering some of the comments above (admitted, the sum of marks needs to be calculated twice, but that's another issue). I assume this all was invented up to illustrate how (multiple) inheritance works at a very basic level, but your instructor should really come up with a better example... – Aconcagua Jul 11 '23 at 12:59
  • Yet a design issue: `i`, `totalmarks` and `percent` are variables exclusively used within `percentage` function. They *really* should be local variables of the function then – I really hope the class members haven't been mandated by the task, otherwise you are learning bad practice :( – Aconcagua Jul 11 '23 at 13:03
  • 1
    @Aconcagua I would prefer `std::size(t.marks)` to `sizeof(t.marks)/sizeof(*t.marks)` – Mestkon Jul 11 '23 at 13:04
  • @Aconcagua From the 3rd requirement it looks like `totalmarks` and `percent` is mandated to be stored in `result`. – Mestkon Jul 11 '23 at 13:10
  • @Mestkon Good point – tend to fall back to old the old habits sometimes, especially as C coming into play for me again and again... – Aconcagua Jul 11 '23 at 13:12
  • @Mestkon Oh, indeed. Making the overall design yet much worse :( But at least the loop counters aren't mandated. At least these ones should be local variables (in both `test` and `result`). – Aconcagua Jul 11 '23 at 13:14
  • Hm... I really have some doubts about the down-votes to this questions really being justified. It's not the question author's fault that the design is that bad, its mandated by the task and QA has to live with *somehow*... – Aconcagua Jul 11 '23 at 15:22
  • *"class result derived from student and test"* -- This is one place you could simplify the [mre] *for this question*. Derive from just `student` **or** `test`. Don't worry about handling both until you can handle each individually. *For example, if you drop `student` then assume there is exactly one student, and if you need the student's name, use the string literal `"Krinesh Vasava"` instead. Keep things simple so that your question is applicable to more than just the other students in your class.* – JaMiT Jul 12 '23 at 02:46
  • Since there is some debate over whether or not this question should be closed: I am voting to close (as "Needs debugging details") because this is a problem dump with no attempt to isolate the error in a [mre]. Questions requesting debugging help should have "the shortest code necessary to reproduce the problem" and this question does not attempt to do that. – JaMiT Jul 12 '23 at 02:54

1 Answers1

-1

From your main function:

student s;
test t;
result r;
s.studentdata();
t.getmarks();
r.percentage();

You obviously missed the fact that a result instance, due to inheritance, already contains both a student and a test instance.

You now initialise separate student and test instances while those of your result instance r remain uninitialised.

To get meaningful results you need to initialise the instances of r – and as you don't use s or t any further you can drop both of them, what then remains is:

result r;
r.studentdata(); // r has inherited the function from student class!
r.getmarks();    // r has inherited the function from test class!
r.percentage();  // result's own function

Note, though, that this design, albeit mandated by your task, is exceptionally bad for several reasons; there is absolutely no reason why this result class should inherit from student and test, they are unrelated one from another, if at all result should aggregate the other two. Furthermore totalmarks and percent variables have no purpose for the result class, they are exclusively used within percentage function and thus should be local variables there – I know it's mandated by the task, but you should at least be aware of. But at very least you can make the loop counters (int i within both test and result classes) local variables of their respective functions: for(int i = 0; i < std::size(marks); ++i) – or even simpler, use range based for loops:

for (auto& m : marks)
//       ^ important here!
{
    std::cin >> m; // here you need the reference above!
}

for (auto m : marks)
{
    totalmarks += m;
}

The second loop doesn't need the reference and as it's a small type, it's irrelevant if you place it – but you might get used to principally adding it, by value is more efficient the compiler will optimise accordingly, though you can support it in its job by having a const reference:

for (auto const& m : marks)
{
    totalmarks += m;
}
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • 3
    Note: Even though this is the correct answer, it is still a horrible design which should never even be considered in real code. – Mestkon Jul 11 '23 at 12:56
  • @Mestkon I fully agree. I added this information as comments already. The design is mandated by the task, so I considered the information better going to there... – Aconcagua Jul 11 '23 at 13:05