Answer to Question #221151 in C++ for Anushka

Question #221151
Write a, C++ program to use pointers for both base and derived classes and call the member function . Use virtual key words
1
Expert's answer
2021-07-29T03:06:12-0400
#include <iostream>

class Base
{
public:
    virtual void PrintName() { std::cout << "Base\n"; }
    virtual ~Base() {}
};

class Derived : public Base
{
public:
    virtual void PrintName() { std::cout << "Derived\n"; }
};

int main()
{
    Base* base    = new Base;
    Base* derived = new Derived;

    base->PrintName();
    derived->PrintName();

    delete base;
    delete derived;
    
    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