Thread: Can't Access the private member from base class

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Can't Access the private member from base class

    Hello guys here i got a question about accessing the private member from base class here is my code

    Code:
    using System;
    class emp
    {
    private int x;
    
    public void getdata()
    {
    y=int.parse(Console.ReadLine());
    }
    public int setdata()
    {
    return y;
    }
    
    }
    class person:emp
    {
    int x;
    public void calculate()
    {
    x=base.setdata();
    Console.WriteLine(x);
    }
    }
    public static void Main()
    {
    emp e=new emp();
    person p=new person();
    e.getdata();
    p.calculate
    }
    The derived class function can't access the value of base class returned function.What is the problem
    AbHHinaay

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Your code is broken on many levels...no compiler would touch that.

    Here's something that will work

    Code:
    using System;
    
    class emp
    {
        private int x;
    
        public void getdata()
        {
            x = Int32.Parse(Console.ReadLine());
        }
        public int setdata()
        {
            return x;
        }
    }
    
    class person : emp
    {    
        public void calculate()
        {
            Console.WriteLine(base.setdata());
        }
    }
    
    class MyMain
    {
        public static void Main()
        {
            person p=new person();
            p.getdata();
            p.calculate();
        }
    }

  3. #3
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    Thanks it works now!!!!
    AbHHinaay

  4. #4
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    i would also reccomend using get and set

    Code:
    class wutever
    {
        private int x;
        
        public int X
        {
            get
            {
                 return x
            }
            set
            {
                 x = value;
            }
        }
    }
    
    class Main
    {
         static void Main()
        {
            wutever w;
            w.X = 0;
            Console.Write(w.X);
            w.X = 16;
            Console.Write(w.X);
        }
    }
    sorry im in a rush ive just learnt get and set so i might be a bit off there
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 8
    Last Post: 12-02-2008, 12:45 PM
  3. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  4. Function in a private member of a class?
    By RealityFusion in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2005, 05:42 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM