Object slicing
I have a program as bellow:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
A()
{
x=10;
}
};
class B:public A
{
protected:
int y;
public:
B()
{
y=30;
}
};
void main()
{
A obj1;
B obj2;
obj1=obj2;
cout<<obj1.x;
cout<<obj1.y;
getch();
}
The error message is "y is not member of A"
Can any one solve this problem?
View Answers
August 23, 2012 at 3:55 PM
There is nothing to solve here. The error message displays correct that "y is not member of A".
When you assign a derived class's instance into the base class only that part, which are present in the base class are copied, rest are not copied. this is called Object Sliced. because here obj2 get sliced and stored in obj1.
The main reson for this is, it is not possible (or meaningful) in any statically typed language
for a superclass instance to contain subclass member
variables.
Ads
Related Tutorials/Questions & Answers:
Object slicing
Object slicing I have a program as bellow:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
A()
{
x=10;
}
};
class B:public A
{
protected:
int y
object
object is it possible to create
object in the same class..?.
Yes, you can.
class CreateObject
{
CreateObject(){
System.out.println("I have an
object.");
}
public static void main(String[] args
Advertisements
object creation
object creation when will java
object is created? is it at runtime or compiletime
Object Oriented
Object Oriented C++ is Purely
object oriented or not .Then why java called purely
object oriented
ArrayList object
ArrayList object i have am ArrayList
object it containg data base records in it,
now i want to display this ArrayList
object data in my jsp programe,
plz help me
object as argument
object as argument what happens when we pass a
object into a method???if java is pass by value how does this makes a difference.....pllzzz give me more clarification on why we pass objects in a margument
Object
Object
An
object...). In an
object, variables store values for later use and methods are the unit... are the basic units
of the
object-oriented programming.
Objects are the part of our day
object array
object array Hi
i have array that is
object[] obj= { new string("hi"), new vector().add(10), new hashmap().setkey()}
display(obj);
display(
object{[] obj) {}
Now my question is what is the string length and how to retrieve
constructor or object
constructor or object
object or construct which create first?
A constructor is always created first.
Here is an example:
class Example
{
Example(String st){
System.out.println(st);
}
public
object and method
object and method a college would like to automate managing courses offered to students. initially only three courses are involved. each courses has... and an application that creates the necessary
object
Java Object
Java
Object
Object is the basic entity of
object oriented
programming language.
Object... properties of the class or its group. Java
object
is an instance of the class. It takes
Session Object from request
Session
Object from request Why are we getting session
object from request
object?
Why are we adding cookie
object into response
object?
Why are we getting cookie
object from request
object?
I know all methods are available its
Object class in java
Object class in java want an example and with an explaination about equals() method in
object class?
Is equals() is similar
ModuleNotFoundError: No module named 'object'
ModuleNotFoundError: No module named '
object' Hi,
My Python... '
object'
How to remove the ModuleNotFoundError: No module named '
object'... to install padas library.
You can install
object python with following command
Is Java object oriented?
Is Java
object oriented? Hi,
Is Java
object oriented?
Thanks
Hi,
Java is
object oriented programming language. It is supports OPPS concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
All
object oriented programming protocol
object oriented programming protocol What is
object oriented programming protocol? Is it different from objective c protocol programming??
Thanks
Object in java - Java Beginners
Object in java Hi deepak,
I am new to java, can u tell me what is class ,
object and instance with example. For example : class raja , raja r = new raja(); ,here raja is class and r is instance . which is
object.
Object
request object value
request
object value Hi Friends
I am developing a web application... request
object value for whole application. Problem is..envirement session,application
object are not supported by this envirement. then how can i maintain all
java is pure object oriented
java is pure
object oriented java is pure
object oriented or not.? reason.?
Java is not pure
object oriented language because... are not
object
2)It does not support operator overloading multiple inheritance.
3