i cant get answer for this virtual function

#include<iostream>
class test
{
protected:
int rollno;
public:

void getroll(int a)
{
a=rollno;
}
void putroll(void)
{
cout<<"your rollnumber is:"<<rollno;
}
};
class subject1:virtual public test
{
protected:
int english;
public:
void engmarks(int b)
{
b=english;
}
void putengmarks(void)
{
cout<<"\n your english mark is:"<<english;
}
};

class subject2:public virtual test
{
protected:
int maths;
public:
void mathmarks(int c)
{
c=maths;
}
void putmathmarks(void)
{
cout<<"\n your maths mark is:"<<maths;
}
};

class total:public subject1,public subject2
{
protected:
int total1;
public:
void totalfull()
{
putroll();
putengmarks();
putmathmarks();
total1=maths+english;
cout<<"\nTotal marks:"<<total1;
}
};

int main()
{
total t1;
t1.getroll(32);
t1.engmarks(100);
t1.mathmarks(100);
t1.totalfull();
return 0;
}

i cant get correct answer for this please help
Last edited on
i cant get correct answer for this please help

We have no idea what sort of answer you expect, and you aren't using virtual functions here at all, so the subject isn't of much help.

Please read: http://www.cplusplus.com/articles/jEywvCM9/
and: http://www.cplusplus.com/forum/beginner/1/
Problem is in your ALL set functions. The member variable should be on LHS

void mathmarks(int c)
{
c=maths; //this is not correct
}

void mathmarks(int c)
{
maths=c; //this is correct
}

This will set your member variable value properly to give you desired results.
===========
Tips:
Also make a habit of giving meaningful names to functions and variables.
I would change it to
void SetMathMarks(int c)
{
m_iMathsMarks = c; //this is correct
}

If you see, your subject1 and subject2 classes are exactly same from programming point of view. When working in C++ one need to think about OO concepts.
From OO point of view, there should be a Subject class whereas Maths and English etc should be the instances (objects) of Subject. Total should take array of Subjects to give you the total result.

Topic archived. No new replies allowed.