Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
392 B
4
Indexable
Never
#include <iostream>

class A {
public:
    A() {
        someVirtualMethod();
    }
    void print() {
        std::cout << n;
    }
protected:
    virtual void someVirtualMethod() {
        n = 1;
    }
    int n;
};

class B : public A {
public:
    B() {}
private:
    void someVirtualMethod() override {
        n = 2;
    }
};

int main() {
    B b = B();

    b.print();
    return 0;
}
Leave a Comment