#include <iostream>
using namespace std;
class Base {
int x;
public:
Base() : x() {}
Base(int x) : x(x) {}
virtual void print() const {
cout << x << endl;
}
};
on Derived(int x, int y) : x(x), y(y) {}, Compiler said must usd "default constructor", but i'd thought that already made, and wonder why default constructor is needed.
class Derived : public Base {
int x, y;
public:
Derived(int x, int y) : x(x), y(y) {}
void print() const override {
cout << x << ", " << x << endl;
}
};
int main() {
// All data members of Base and Derived classes must be declared
// as private access types
Base* p1 = new Derived(10, 20); // (x, y)
Base* p2 = new Base(5); // (x)
p1->print(); // prints 10, 20
p1->Base::print(); // prints 10
}
second in this problem,
p1->Base::print(); // prints 10
p1->Base::print() must print 10, but it didn't work. How can I print 10?