Please, help me with this mistake. I'm totaly confused

Feb 8, 2013 at 2:50pm
Program is compiling well. But after execution the result is:
Average math: 3.67
Average phis: 4.33
Average inf: 4.33
Result: 4.67
Segmentation fault
The result is right, but where is segmantation fault happened?
Here is a program:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <iomanip>
using namespace std;

class cMath;
class cPhis;
class cInf;

class cStudent{
	protected:
		double math, phis, inf, res;
	public:
		double result(cMath, cPhis, cInf);	
		virtual double set_values(int, int, int)=0;
};

class cMath: public cStudent{
	private:
		int m1, m2, m3;
	public:
		double set_values(int, int, int);
};

class cPhis: public cStudent{
	private:
		int p1, p2, p3;
	public:
		double set_values(int, int, int);
};

class cInf: public cStudent{
	private:
		int i1, i2, i3;
	public:
		double set_values(int, int, int);
};

double cMath::set_values(int a, int b, int c)
{
	double d;
	
	m1=a;
	m2=b;
	m3=c;
	d=(double)(a+b+c)/3;
	math=d;

	return d;
}

double cPhis::set_values(int a, int b, int c)
{
	double d;

	p1=a;
	p2=b;
	p3=c;
	d=(double)(a+b+c)/3;
	phis=d;

	return d;
}

double cInf::set_values(int a, int b, int c)
{
	double d;
	
	i1=a;
	i2=b;
	i3=c;
	d=(double)(a+b+c)/3;
	inf=d;

	return d;
}

double cStudent::result(cMath m, cPhis p, cInf i)
{
	this->math=m.math;
	this->phis=p.phis;
	inf=i.inf;
	this->res=(double)(math+phis+this->inf)/3;	
	
	return(this->res);
}

int main()
{
	cMath m;
	cPhis p;
	cInf i;
	cStudent *ps, *ps1, *ps2, *ps3;

	ps1=&m;
	ps2=&p;
	ps3=&i;
	cout<<"Average math: "<<setprecision(3)<<ps1->set_values(3,4,4)<<endl;
	cout<<"Average phis: "<<setprecision(3)<<ps2->set_values(3,5,5)<<endl;
	cout<<"Average inf: "<<setprecision(3)<<ps3->set_values(4,5,5)<<endl;
	cout<<"Result: "<<setprecision(3)<<ps->result(m,p,i)<<endl;
		
	return 0;
}
Last edited on Feb 8, 2013 at 3:13pm
Feb 8, 2013 at 2:54pm
I would wager it's because *ps isn't pointing to anything.
Feb 8, 2013 at 3:11pm
Where should I point *ps? I have just begun to learn OOP.
Last edited on Feb 8, 2013 at 4:33pm
Feb 8, 2013 at 5:30pm
Technically pointing at at any of the classes you've implemented would work.

However, there's a few general OOP issues that don't make sense here. For example, all of the subclasses are, in everything other than name, the same. And, oddly, the superclass relies on the subclasses for one of it's functions.
Feb 8, 2013 at 5:42pm
As I understood you, this program is bad at it purpose. Thanks, I will learn harder.
Topic archived. No new replies allowed.