please ..take out the logical error in this 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;
}
You didn't overload the assignment operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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;
} 
Last edited on
Topic archived. No new replies allowed.