-1

So, I was learning about inheritance and there was a question in last year's question set. It was just to create a diamond problem and inherit data member marks from base class to final class. So, I created a abstract base class for void setmarks() and override it in all inherited class. To resolve the ambiguity, I add virtual in inheritance declaration.

Here is my code:

#include <iostream>

using namespace std;

class Evalution

{

    public:
    float marks;

    virtual void setmarks()=0;
    
    void displaymarks() {}

};


class Theoretical :virtual protected Evalution
{
    public:
    float internal1, internal2;
    int assignments;
    
    void setmarks()
    {
        cout << "enter the marks in internal1, internal2, assignments" << endl;
        cin >> internal1 >> internal2 >> assignments;
    }
};


class Practical :virtual protected Evalution
{
    public:
    float lab, viva;
    
    void setmarks()
    {
        cout << "enter the marks in lab and viva" << endl;
        cin >> lab >> viva;
    }
};


class Final : public Theoretical, public Practical
{
    public:
     marks = internal1 + internal2 + assignments + lab + viva;
    
    void setmarks() {}
     void displaymarks()
    {
        cout << "the final marks is " << marks << endl;
    }
};

int main()
{
    Final f;
    f.Theoretical::setmarks();
    f.Practical::setmarks();
    f.displaymarks();
}

While running the program on online gcc compiler, there is error ‘marks’ does not name a type. What went wrong? Even if:

class Evalution
{
    public:
     //float marks;
     virtual void setmarks()=0;
     void displaymarks() {}
};

And:

class Final : public Theoretical, public Practical
{
    public:
    float marks = internal1 + internal2 + assignments + lab + viva;
    
    void setmarks() {}
     void displaymarks()
    {
        cout << "the final marks is " << marks << endl;
    }
};

The program still gives me a garbage value. Help please.

  • 2
    Seems like you expected the compiler to guess your intention and let `float marks = internal1 + internal2 + assignments + lab + viva;` execute only after entering input? – Lukas-T Dec 27 '20 at 10:32
  • Seems you have issue even with regular inheritance... – Jarod42 Dec 27 '20 at 11:28
  • This has nothing to do with inheritance. You'd have the same problem with a single class. – Pete Becker Dec 27 '20 at 14:56

2 Answers2

1

At this point, the compiler expects a member declaration

class Final : public Theoretical, public Practical {
public:
    marks = internal1 + internal2 + assignments + lab + viva;

Therefore it looks for a type. But marks is not a type, but a member variable in the base class. This is why the compiler complains about

'marks' does not name a type


When you change it to

float marks = ...;

the compiler sees the expected member declaration.

But this time the member marks is initialized at object construction time, not after the other members are assigned proper values.


For details, see Class declaration and Non-static data members, or maybe one of the books at The Definitive C++ Book Guide and List

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

You need to first set the variables internal1, internal2 etc. which you are doing through setmarks() base class methods before calculating final marks and then set final marks by calculating the sum of all.

#include <iostream>

using namespace std;

class Evalution

{

    public:
    float marks;

    virtual void setmarks()=0;
    
    void displaymarks() {}

};


class Theoretical :virtual protected Evalution
{
    public:
    float internal1, internal2;
    int assignments;
    
    void setmarks()
    {
        cout << "enter the marks in internal1, internal2, assignments" << endl;
        cin >> internal1 >> internal2 >> assignments;
        //internal1 = 10;
        //internal2 = 20;
        //assignments = 30;
    }
};


class Practical :virtual protected Evalution
{
    public:
    float lab, viva;
    
    void setmarks()
    {
        cout << "enter the marks in lab and viva" << endl;
        cin >> lab >> viva;
        //lab = 234;
        //viva = 45;
    }
};


class Final : public Theoretical, public Practical
{
    public:
    
    void setmarks() {
        this->Theoretical::setmarks();
        this->Practical::setmarks();
        marks = internal1 + internal2 + assignments + lab + viva;
    }
     void displaymarks()
    {
        cout << "the final marks is " << marks << endl;
    }
};

int main()
{
    Final f;
    f.setmarks();
    f.displaymarks();
}
Harry
  • 2,177
  • 1
  • 19
  • 33
  • 2
    Why should the OP "try this way"? What have you changed? Why did you make those changes? What is the problem with the code shown by the OP? Please refresh on [how to write good answers](https://stackoverflow.com/help/how-to-answer). – Some programmer dude Dec 27 '20 at 10:37
  • Thank you very much for that. But, I am still a beginner and how did `this->` helped here? I am still confused. – Ashal Adhikari Dec 27 '20 at 15:27