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

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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` ``````#include #include 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: "<set_values(3,4,4)<set_values(3,5,5)<set_values(4,5,5)<result(m,p,i)<
Last edited on
I would wager it's because `*ps` isn't pointing to anything.
Where should I point `*ps`? I have just begun to learn OOP.
Last edited on
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.
As I understood you, this program is bad at it purpose. Thanks, I will learn harder.
Topic archived. No new replies allowed.