Answer to Question #264429 in C++ for |=|\/|=_

Question #264429

(Pointers to derived classes) Write

a program to illustrate how pointers to a base class is used for both base and

derived class.


1
Expert's answer
2021-11-14T17:02:54-0500
#include <iostream>

struct Base {
    virtual ~Base() = default;
    
    virtual void Print() const {
        std::cout << "Base\n";
    }
};

struct Derived : public Base {
    void Print() const override {
        std::cout << "Derived\n";
    }
};

int main() {
    Base* arr[] = { new Base{}, new Derived{} };
    arr[0]->Print();
    arr[1]->Print();
    delete arr[0];
    delete arr[1];
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS