Answer to Question #268200 in C++ for Amit

Question #268200

Write a program such that base class pointer or reference will always access/call the base version of the members available in derived class, do not have any access to the derived class members.

1
Expert's answer
2021-11-18T06:54:52-0500
#include <iostream>
using namespace std;
class base {
public:
	virtual void print()
	{
		cout << "print base class" << endl;
	}
	void show()
	{
		cout << "show base class" << endl;
	}
};

class derived : public base {
public:
	void print()
	{
		cout << "print derived class" << endl;
	}
	void show()
	{
		cout << "show derived class" << endl;
	}
};
int main()
{
	base* bptr;
	derived d;
	bptr = &d;
	bptr->print();
	bptr->show();
}

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