class Result
{
public:
void menu(Wx w, Px p);
};
Jak widac klasa nie ma zadnych atrybutow, jak powinien wygladac do niej konstruktor?
Bo majac tak:
Result::Result()
Result::~Result()
dostaje error:
kopia\structors.cpp|37|error: expected initializer before 'Result'|
Drugie pytanie to dlaczego nie majac zdeklarowanej przyjazni w klasie Px (jest w Wx a Px dziedziczy od Wx) moge uzyskac dostep do Px::degree w funkcji menu skoro przyjazn nie jest dziedziczona? Kody:
polynomials.h:
#include <iostream>
using namespace std;
class Polynomial
{
public:
virtual void gather()=0;
virtual void show()=0;
};
class Px;
class Wx;
class Result
{
public:
void menu(Wx w, Px p);
};
class Wx :public Polynomial
{
protected:
string degreeStringNum;
int degree;
int helper;
float *coefficients;
public:
virtual void gather();
virtual void show();
void isInteger(string &s, int &i);
friend void Result::menu(Wx w, Px p);
Wx(string="0", int=0, int=0);
~Wx();
};
class Px :public Wx
{
public:
virtual void gather();
virtual void show();
Px(string="0", int=0, int=0);
~Px();
};
structors.cpp:
#include <iostream>
#include "polynomials.h"
using namespace std;
Wx::Wx(string dNS, int d, int h)
{
degreeStringNum=dNS;
degree=d;
helper=h;
coefficients=new float[d+1];
cout<<"constructor 1 called";
cout<<endl;
}
Wx::~Wx()
{
delete [] coefficients;
cout<<"destructor 1 called";
cout<<endl;
}
Px::Px(string dNS, int d, int h)
:Wx(dNS, d, h)
{
coefficients=new float[d+1];
cout<<"constructor 2 called";
cout<<endl;
}
Px::~Px()
{
delete [] coefficients;
cout<<"destructor 2 called";
cout<<endl;
}
//Result::Result()
//Result::~Result()
menu.cpp
#include <iostream>
#include "polynomials.h"
using namespace std;
void Result::menu(Wx w, Px p)
{
cout<<p.degree;
cout<<endl;
}
main.cpp:
#include <iostream>
#include "polynomials.h"
using namespace std;
int main()
{
Wx w;
Px p;
Result r;
Polynomial *pointerWx;
Polynomial *pointerPx;
pointerWx=&w;
pointerPx=&p;
pointerWx->gather();
pointerWx->show();
pointerPx->gather();
pointerPx->show();
r.menu(w, p);
}
No i trzecie pytanie to dlaczego z tego jest taki output:
constructor 1 called
constructor 1 called
constructor 2 called
Input degree of polynomial W(x): 3
Input coefficients and constant of polynomial W(x):
1
2
3
4
W(x)= x3+2x2+3x+4
Input degree of polynomial P(x): 2
Input coefficients and constant of polynomial P(x):
9
8
7
P(x)= 9x2+8x+7
2
destructor 1 called
destructor 2 called
destructor 1 called
destructor 2 called
destructor 1 called
destructor 1 called
mam na mysli te destruktory, cos ich za duzo :p, te liczby oraz napisy "Input degree and constant..." itp to zasluga innych funkcji w moim programie ale nie sa one kluczowe tutaj. Skąd się wziely te destruktory, przed dodaniem klasy Result oraz zaprzyjaznienia jej funkcji z klasami Px i Wx bylo tylko tak z tego co pamietam:
destructor 2 called
destructor 1 called
destructor 1 called