Interview Question






Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes. A pointer of type base class can point to derived class object. But the opposite is not true. Here's how:

base *bp;
derived d;
bp = &d; // base pointer points to derived object
bp->set_i(10); // access derived object using base pointer

- dmelloleslie March 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think interviewee want answer about type casting.

if base class pointer points derived class object, base class point can convert to derived class pointer with casting(static_cast, dynamic_cast and so on)

- Zaphod March 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dmelloleslie is right. The pointer is of base class, but can point to an object of derived class without giving an error message. In this case the compiler relaxes type checking. Taking the address of a derived class and treating it as the address of a base class is called "upcasting". The concept of virtual function is based on similar ideas. If the base class and the derived class both have a function defined with the same name (say foo) and you call bp->foo then the that in the base class will get invoked. However if you declare the base class foo as virtual then the function in the derived class will get invoked.

- Sumeet March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ dmelloleslie you Understood question wrongly
Derived *d;
base *b
d=b


@Zaphod answer is correct.

- nav March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dynamic_cast

- Anonymous April 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can convert base class pointer to derived class pointer using dynamic_cast only if base class is polymorphic.

I mean

class A {
public : virtual fun() { }

};
class B : public A { };


void main()
{
B *b = new B() ;
A *a = new A();

B* nb = dynamic_cast<B*> (a); // it is ok because A is polymorphic

}

- keyur April 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keyur
The code you have written will compile. But not run. Since in your case a is pointing to an object of class A, after the dynamic_cast, you will have Null in nb. So it is of no use. You try to access any thing using nb and it will give Segmentation Fault. So you can case base class pointer to derived for good only when base pointer is actually pointing to a derived object or object derived from "Derived"

- Sourav July 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

true cause derived class may have excess data which may be now null because cast is from Base class obj to a base class pointer

- unicorn August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below is an example where we can create base object using derived pointer

#include <map>
#include <iostream>

using namespace std;

class A
{
public:
	int i;
	virtual void show()
	{
		cout << "A::i = " << i << endl;
	}
	A() : i(10) {};
};

class B : public A
{
public:
	int j;
	void show()
	{
		cout << "A::j = " << j << endl;
	}
	B() : j(20) {};
};

int main() 
{
	A *a = new B();
	a->show();
	B *b = static_cast<B *>(new A());
	b->show();

	return 0;
}

- Anonymous December 13, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More