#include <iostream> #include <cmath> #include <iomanip> using namespace std; class Function { protected: double x, y; public: Function() { x = 0; y = 0; } Function(Function& p) { x = px; y = py; } virtual double Calculation(int) = 0; }; class sin_cos : public Function { sin_cos(): Function() {} virtual double Calculation(int t) { y = sin(t); x = cos(t); return x; } }; int main() { setlocale(LC_ALL, "rus"); Function* mas[5]; mas[1] = new sin_cos; cout << mas[i]->Calculation(10) << endl; system("pause"); return 0; } 2 answers
It would be necessary as a comment, but it does not fit there ...
VC ++ writes the following:
error C2248: sin_cos :: sin_cos: it is impossible to access the private member declared in the class "sin_cos"
error C2065: i: undeclared identifier
error C2227: the expression to the left of "-> Calculation" should indicate the type of a class, structure or union, or a universal type
What exactly is this incomprehensible to you?
GCC swears about the same :
prog.cpp: In function 'int main()': prog.cpp:40:18: error: 'sin_cos::sin_cos()' is private within this context mas[1] = new sin_cos; ^~~~~~~ prog.cpp:26:5: note: declared private here sin_cos(): Function() ^~~~~~~ prog.cpp:41:17: error: 'i' was not declared in this scope cout << mas[i]->Calculation(10) << endl; sin_cos - the constructor in the class is not declared as public , so create an object like this
mas[1] = new sin_cos; no - no access rights. And then you can not use
mas[i]->Calculation(10) because not only the value of i unknown, but this variable itself is not declared.
You forgot to specify public access control for the sin_cos class sin_cos and the Calculation virtual function
class sin_cos : public Function { public: ^^^^^^^ sin_cos(): Function() {} virtual double Calculation(int t) { y = sin(t); x = cos(t); return x; } }; Also in main, I think you meant index 1 instead of variable i, which is not declared
mas[1] = new sin_cos; cout << mas[1]->Calculation(10) << endl; ^^^ Keep in mind that array indices start at 0, not at 1.
public:) - Harry