Results 1 to 6 of 6

Thread: what is object slicing in c++?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    what is object slicing in c++?

    Hi,
    I am F.Y.B.Sc.IT student. I Recently started learning C++ language which is in our syllabus. In our last tutorial there was one question what is object slicing in c++? I tried to find out answer of this question in C++ book but I unable to find answer. So I decided to get help of you. If you know anything about this question please share with me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: what is object slicing in c++?

    When your child class object is manually assigned to parent class, the parent class's contents in the child class object are copied to the parent class leaving behind the child class specific contents. This mechanism is referred as Object Slicing.



    Code:
    Class Parent
    {
    public:
      int p;
    };
    
    class Child : public Parent
    {
    public:
      int q;
    };
    
    int main()
    {
      Parent Bj;
      Child Dj;
      Bj = Dj;  //Here Dj contains both p and q. 
                   //But only p is copied to Bj.
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: what is object slicing in c++?

    When an parent class is assigned to its child class, the parent class takes up only the parent member data leaving the data members of child class.This process is called as object slicing.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: what is object slicing in c++?

    If an object of a child class is assigned to a parent class object,the compiler accepts only the parent portion of the object.


    Code:
    class parent
    {
       public:
               int p,q;
    };
    class child : public parent
    {
      private:
              int t;
    };
    int main()
    {
       parent x;
       child y;
       x=y;
    return o;
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: what is object slicing in c++?

    From this program you can get idea of object slicing

    Code:
    class parent 
    {
     public int p,q;
    }
    class child extends parent
    {
    public int t;
    }
    class main
    {
    public static void main(String[] s)
    {
    parent x = new parent();
    drived y  = new child();
    x.p=10;
    x.q=20;
    System.out.println("this is class parent p=10,q=20");
    System.out.println("p =" +x.p);
    System.out.println("j =" +x.q);
    System.out.println("this is class child p=100,q=200,t=300");
    y.=100;
    y.j=200;
    y.k=300;
    System.out.println("p =" +y.p);
    System.out.println("q =" +y.q);
    System.out.println("t =" +y.t);
    x=y;
    
    System.out.println("after the assignment");
    System.out.println("p =" +x.p);
    System.out.println("q =" +x.q);
    //System.out.println("t =" +x.t);
    System.out.println("now you know what is object slicing");
    }
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: what is object slicing in c++?

    Object slicing happened when we assign an instance of the
    child class to a variable of type parent class.

    Code:
    class Parent
    {
        int avariable;
    };
     
    class Child : public Parent
    {
        int bvariable;
    };
     
    Child x;
    Parent y = x; 
     
    Child x2;
    Parent &y = x2;
    y = x;

Similar Threads

  1. Replies: 6
    Last Post: 06-06-2011, 01:34 AM
  2. Replies: 3
    Last Post: 08-01-2011, 06:20 AM
  3. Object reference not set to an instance of an object
    By KAIRU26 in forum Software Development
    Replies: 3
    Last Post: 05-09-2009, 08:14 PM
  4. What is Program Slicing ?
    By Ananias in forum Software Development
    Replies: 4
    Last Post: 13-04-2009, 01:52 PM
  5. Object test = new Object() <-- Java, best way in C++
    By ADISH in forum Software Development
    Replies: 3
    Last Post: 25-10-2008, 02:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,679,827.13682 seconds with 17 queries