please ..take out the logical error in this simple overloading program..

#include <iostream>
#include <conio.h>

using namespace std;

class studentmarks
{
private:
int physics;
int maths;
int english;
public:
studentmarks()
{
physics = 0;
maths = 0;
english = 0;
}
studentmarks(int p,int m,int e)
{
p = physics;
m = maths;
e = english;
}
void getmarks()
{
cout << "\nEnter physics marks: ";cin >> physics;
cout << "\nEnter maths marks: ";cin >> maths;
cout << "\nEnter english marks: ";cin >> english;
}
void showmarks()
{
cout << "Physics = " << physics;
cout << "Maths = " << maths;
cout << "English = " << english;
}
studentmarks operator + (studentmarks) const;
};

studentmarks studentmarks :: operator + (studentmarks s2) const
{
int phy = physics + s2.physics;
int math = maths + s2.maths;
int eng = english + s2.english;
return studentmarks(phy,math,eng);
}
int main()
{
studentmarks s1,s2,s3;
s1.getmarks();
s2.getmarks();
s3 = s1 + s2;
s3.showmarks();
getch();
return 0;
}
studentmarks(int p,int m,int e)
{
p = physics;
m = maths;
e = english;
}

it is wrong assignment, should look like this:

studentmarks(int p,int m,int e)
{
physics = p;
maths = m;
english = e;
}
you can also put those two constructors together using implicit zero values for parameters

studentmarks(int p = 0, int m = 0, int e = 0)
{
physics = p;
maths = m;
english = e;
}
Topic archived. No new replies allowed.