Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Base {
- public:
- int val;
- Base(int initialization);
- virtual void which();
- };
- Base::Base(int init) {
- val = init;
- }
- void Base::which() {
- std::cout << "Base" << "\n";
- }
- class Derived : public Base {
- public:
- Derived(int init);
- void which();
- };
- Derived::Derived(int init) : Base(init) {
- }
- void Derived::which() {
- std::cout << "Derived" << "\n";
- }
- int main() {
- Base b = Derived(5);
- b.which(); //prints base, but I want Derived
- Base *pb = new Derived(5);
- pb->which(); //prints derived
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement