Answer Posted / p govind rao
if an object of a derived class is assigned to a base class
object,the compiler accepts it.but it copies only the base
portion of the object
#include<iostream>
using namespace std;
class base
{
public:
int i,j;
base(){i=2;j=3;}
virtual void show(){cout<<"i = "<<i<<endl<<"j = "<<j<<endl;}
};
class derived :public base
{
public :
int k;
derived(){k=4;}
void show() {
base::show();
cout<<"k = "<<k<<endl;
}
};
int main()
{
base b;
derived d;
d.show();
b=d;
b.show();
return 0;
}
OutPut is
i = 2
j = 3
k = 4
---------------
i = 2
j = 3
here b contains i and j where as d contains i,j&k.On
assignment only i and j of the d get copied into i and j of
b. k doesnot be copied. on the effect object d got sliced.
| Is This Answer Correct ? | 12 Yes | 1 No |
Post New Answer View All Answers
Can static class have constructor?
what are the realtime excercises in C++?
What is an example of genetic polymorphism?
Why it is called runtime polymorphism?
c++ program to swap the objects of two different classes
What do you mean by Encapsulation?
Why do we use inheritance?
What is interface? When and where is it used?
What is the purpose of polymorphism?
What is the difference between a constructor and a destructor?
Will I be able to get a picture in D drive to the c++ program? If so, help me out?
write a programe to calculate the simple intrest and compund intrest using by function overlading
What is the point of oop?
What is polymorphism programming?
What is encapsulation and abstraction? How are they implemented in C++?