I have to understand why this program in C++ isn't working. There were many other errors but I corrected them.
#include <iostream>
using namespace std;
class X {
public :
X(int a) : a(a) {}
virtual void f(ostream& flot) const { flot << " X::f" << endl; }
virtual void g() const = 0;
private :
int a;
};
class Z : public X {
void h() const {cout << "Z::h" << endl;}
public :
Z() : X(0) {}
void f(ostream& flot) const override { flot << "Z::f" << endl;}
void g() const override {cout << "Z::g" << endl;}
};
void operator<< (ostream& flot, const Z& z ) { z.f(flot);}
int main() {
Z z;
cout << z << endl;
Z z2();
z2.h();
X* x1;
x1->f(cout);
z.h();
}
You can't write cout << z << endl; because z does not exist as a normal object, is it correct? it is a kind of imaginary object ? at the contrary of z2 which is constructed and does exists. Can you tell me if I am right?
And I don't understand why the two last lines:
x1->f(cout);
z.h();
are not problematic. I thought that x1 does not exists (as z). So why can you call a method for x1?
And for z.h(), the only problem is that h is, here, in this context, private. But if it would have been inside the public part, it wouldn't be a problem. Why ??? Isn't z imaginary so you can't use any method on it?