locked
beginer with class-someone can help-me RRS feed

  • Question

  • i'm beginning learn class and i done this simple code but i don't know where i went  wrong   someone can help-me

     
    using System; 
     
    public abstract class A 

     public string _a;
     string _b; 
     
     public string a 
     { 
      get { return _a; } 
     set { _a = value; } 
     } 
     
     public virtual void f1() 
     { 
     Console.WriteLine(_a); 
     } 
     
     public abstract void f2(); 

     
    public class B : A 

     string _b; 
     
     public string b 
     { 
      get { return _b; } 
      set { _b = value; } 
     } 
     
     public void B()
     {     
             base._b = "boo";
     } 
     
     public void f2() 
     { 
     Console.WriteLine(_b); 
     } 

     
    public class Program

     static void Main() 
     { 
     B objeto = new B(); 
     
     objeto.a = "aaa"; 
     objeto.b = objeto.a; 
     objeto.f1(); 
     objeto.f2(); 
     } 

     

    Monday, May 12, 2014 2:12 PM

Answers

  • Use the following code insted of your code

    Problems :

    1. In Class A you had neglected to provide access specifier "public" to _b

    2.In class B you should not add a return type to your constructor so remove the "void" keyword

    3. You did not overide f1 in class B

    4. You did not use keyword "override" while declaring f2() in class B

    public abstract class A
        {
            public string _a;
            public string _b;
    
            public string a
            {
                get { return _a; }
                set { _a = value; }
            }
    
            public virtual void f1()
            {
                Console.WriteLine(_a);
            }
    
            public abstract void f2();
        }
    
        public class B : A
        {
            string _b;
    
            public string b
            {
                get { return _b; }
                set { _b = value; }
            }
    
            public B()
            {
                base._b = "boo";
            }
    
            public override void f1()
            {
                base.f1();
            }
    
    
    
            public override void f2()
            {
                Console.WriteLine(_b);
            }
        }

    • Proposed as answer by Norkk Monday, May 12, 2014 2:32 PM
    • Marked as answer by Julio.Ismar Monday, May 12, 2014 2:50 PM
    Monday, May 12, 2014 2:29 PM

All replies

  • Use the following code insted of your code

    Problems :

    1. In Class A you had neglected to provide access specifier "public" to _b

    2.In class B you should not add a return type to your constructor so remove the "void" keyword

    3. You did not overide f1 in class B

    4. You did not use keyword "override" while declaring f2() in class B

    public abstract class A
        {
            public string _a;
            public string _b;
    
            public string a
            {
                get { return _a; }
                set { _a = value; }
            }
    
            public virtual void f1()
            {
                Console.WriteLine(_a);
            }
    
            public abstract void f2();
        }
    
        public class B : A
        {
            string _b;
    
            public string b
            {
                get { return _b; }
                set { _b = value; }
            }
    
            public B()
            {
                base._b = "boo";
            }
    
            public override void f1()
            {
                base.f1();
            }
    
    
    
            public override void f2()
            {
                Console.WriteLine(_b);
            }
        }

    • Proposed as answer by Norkk Monday, May 12, 2014 2:32 PM
    • Marked as answer by Julio.Ismar Monday, May 12, 2014 2:50 PM
    Monday, May 12, 2014 2:29 PM
  • In class A, change

    string _b;

    to

    public string _b;


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by Rohan W Monday, May 12, 2014 2:37 PM
    Monday, May 12, 2014 2:30 PM
  • You didn't clarify what your problem was.  The biggest thing I see is that you have defined a field (_b) in your base class but never use it.  Therefore it should be removed. 

    f2 in derived type B needs to be marked with override because you're overriding the abstract method in A.  You should have gotten a warning about defining a new method with the same name.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Monday, May 12, 2014 2:32 PM
  • thanks. i will remember of this in the next exercise.

    Monday, May 12, 2014 2:49 PM
  • thanks!

    Monday, May 12, 2014 2:51 PM
  • thanks!
    Monday, May 12, 2014 2:51 PM